Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust network calls to hcloud 1.2 #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

source ENV['GEM_SOURCE'] || 'https://rubygems.org'

gem 'hcloud', git: 'https://github.com/bastelfreak/hcloud-ruby', branch: 'dep'

gemspec

group :coverage, optional: ENV['COVERAGE'] != 'yes' do
Expand Down
2 changes: 1 addition & 1 deletion beaker-hcloud.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'bcrypt_pbkdf', '~> 1.0'
s.add_runtime_dependency 'beaker', '~> 5.4'
s.add_runtime_dependency 'ed25519', '~> 1.2'
s.add_runtime_dependency 'hcloud', '~> 1.1'
s.add_runtime_dependency 'hcloud', '~> 1.2'
s.add_runtime_dependency 'ssh_data', '~> 1.3'
end
23 changes: 15 additions & 8 deletions lib/beaker/hypervisor/hcloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +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 = @client.servers.create(
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']
host[:ip] = server.public_net['ipv4'].ip
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mhm I'm not sure how to mock .ip properly

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

irb(main):013:0> server.public_net['ipv4']
=> #<Hcloud::PrimaryIP:0xd5c {"assignee_id"=>38840073, "assignee_type"=>"server", "auto_delete"=>true, "blocked"=>false, "created"=>2023-11-01 15:10:39 UTC, "datacenter"=>#<Hcloud::Datacenter:0xd70 {"description"=>"Nuremberg 1 virtual DC 3", "id"=>2, "location"=>#<Hcloud::Location:0xd84 {"city"=>"Nuremberg", "country"=>"DE", "description"=>"Nuremberg DC Park 1", "id"=>2, "latitude"=>49.452102, "longitude"=>11.076665, "name"=>"nbg1", "network_zone"=>"eu-central"}>, "name"=>"nbg1-dc3", "server_types"=>{"available"=>[1, 3, 5, 7, 9, 22, 23, 24, 25, 26, 45, 93, 94, 95, 96, 97, 98, 99, 100, 101], "available_for_migration"=>[1, 3, 5, 7, 9, 22, 23, 24, 25, 26, 45, 93, 94, 95, 96, 97, 98, 99, 100, 101], "supported"=>[9, 7, 5, 3, 1, 22, 23, 24, 25, 26, 45, 93, 94, 95, 96, 97, 98, 99, 100, 101]}}>, "dns_ptr"=>[{"ip"=>"128.140.40.189", "dns_ptr"=>"static.189.40.140.128.clients.your-server.de"}], "id"=>42635150, "ip"=>"128.140.40.189", "labels"=>{}, "name"=>"primary_ip-42635150", "protection"=>{"delete"=>false}, "type"=>"ipv4"}>
irb(main):014:0> server.public_net['ipv4'].ip
=> "128.140.40.189"
irb(main):015:0> server.public_net['ipv4'].dns_ptr
=> [{"ip"=>"128.140.40.189", "dns_ptr"=>"static.189.40.140.128.clients.your-server.de"}]
irb(main):016:0> server.public_net['ipv4'].dns_ptr.first
=> {"ip"=>"128.140.40.189", "dns_ptr"=>"static.189.40.140.128.clients.your-server.de"}
irb(main):017:0>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

irb(main):015:0> server.public_net['ipv4'].dns_ptr.find { |hash| hash['ip'] == server.public_net['ipv4'].ip }
=> {"ip"=>"128.140.40.189", "dns_ptr"=>"static.189.40.140.128.clients.your-server.de"}
irb(main):016:0> server.public_net['ipv4'].dns_ptr.find { |hash| hash['ip'] == server.public_net['ipv4'].ip }['dns_ptr']
=> "static.189.40.140.128.clients.your-server.de"
irb(main):017:0>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd mock the client. So

let(:mock_client) { double(::Hcloud::Client) }
before do
  allow(::Hcloud::Client).to receive(new).and_return(mock_client)
end

A tricky part is that you need to mock all responses on mock_client then. It may be easier to create another helper method:

def self.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)
    server = client.servers.find(server.id)
  end

  server
end

Then you already reduce it a lot: you now can use:

result = Mock(...)
allow(Beaker::Hypervisor::Hcloud).to receive(create_hcloud_server).with(....).and_return(result)

And if you then look at create_hcloud_server, you can also consider if this is also sufficient:

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

  client.servers.find(server.id)
end

It saves a find action on every call.

host[:vmhostname] = server.public_net['ipv4'].dns_ptr.find { |hash| hash['ip'] == host[:ip] }['dns_ptr']
host[:hcloud_id] = server.id
host.options[:ssh][:keys] = [@key_file.path]
server
Expand Down
14 changes: 12 additions & 2 deletions spec/beaker/hypervisor/hcloud_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@
public_net: {
'ipv4' => {
'ip' => '192.168.0.1',
'dns_ptr' => 'server1.example.com',
'dns_ptr' => [
{
'ip' => '192.168.0.1',
'dns_ptr' => 'server1.example.com',
},
],
},
},
destroy: true,
Expand All @@ -54,7 +59,12 @@
public_net: {
'ipv4' => {
'ip' => '192.168.0.2',
'dns_ptr' => 'server2.example.com',
'dns_ptr' => [
{
'ip' => '192.168.0.2',
'dns_ptr' => 'server2.example.com',
},
],
},
},
destroy: true,
Expand Down
Loading