From 05cf004e6a5924e0aabae27d2b698c96cc24de10 Mon Sep 17 00:00:00 2001 From: Tim Meusel Date: Wed, 1 Nov 2023 18:02:09 +0100 Subject: [PATCH] refactor create_server method for easier testing --- lib/beaker/hypervisor/hcloud.rb | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/beaker/hypervisor/hcloud.rb b/lib/beaker/hypervisor/hcloud.rb index 6843549..10f9d8e 100644 --- a/lib/beaker/hypervisor/hcloud.rb +++ b/lib/beaker/hypervisor/hcloud.rb @@ -81,23 +81,32 @@ def vm_deletion_date date end + def create_hcloud_server(client, **kwargs) + action, server, _password, _next_action = client.servers.create(**kwargs) + + while action.status == 'running' + sleep 5 + action = client.actions.find(action.id) + end + + server + end + def create_server(host) @logger.notify "provisioning #{host.name}" location = host[:location] || 'nbg1' server_type = host[:server_type] || 'cx11' - action, server, _password, _next_action = @client.servers.create( - name: host.hostname, - location: location, - server_type: server_type, - image: host[:image], - ssh_keys: [ssh_key_name], - labels: { delete_vm_after: vm_deletion_date }, + server = create_hcloud_server( + @client, + **{ + name: host.hostname, + location: location, + server_type: server_type, + image: host[:image], + ssh_keys: [ssh_key_name], + labels: { delete_vm_after: vm_deletion_date }, + }, ) - while action.status == 'running' - sleep 5 - action = @client.actions.find(action.id) - server = @client.servers.find(server.id) - end host[:ip] = server.public_net['ipv4'].ip host[:vmhostname] = server.public_net['ipv4'].dns_ptr.find { |hash| hash['ip'] == host[:ip] }['dns_ptr'] host[:hcloud_id] = server.id