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

add missing features to existing resources #60

Merged
12 commits merged into from Oct 11, 2023
Next Next commit
lib: implement missing server actions
  • Loading branch information
Stefan Koch committed Jun 22, 2023
commit f612884711d349964066e02ec4b57a2a6343d6c1
22 changes: 21 additions & 1 deletion lib/hcloud/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,30 @@ def detach_from_network(network:)
prepare_request('actions/detach_from_network', j: { network: network })
end

def add_to_placement_group(placement_group:)
raise Hcloud::Error::InvalidInput, 'no placement_group given' if placement_group.nil?

prepare_request('actions/add_to_placement_group', j: COLLECT_ARGS.call(__method__, binding))
end

def change_alias_ips(alias_ips:, network:)
raise Hcloud::Error::InvalidInput, 'no alias_ips given' if alias_ips.to_a.count.zero?
raise Hcloud::Error::InvalidInput, 'no network given' if network.nil?

prepare_request('actions/change_alias_ips', j: COLLECT_ARGS.call(__method__, binding))
end

def change_dns_ptr(ip:, dns_ptr:)
raise Hcloud::Error::InvalidInput, 'no IP given' if ip.blank?
raise Hcloud::Error::InvalidInput, 'no dns_ptr given' if dns_ptr.blank?

prepare_request('actions/change_dns_ptr', j: COLLECT_ARGS.call(__method__, binding))
end

%w[
poweron poweroff shutdown reboot reset
disable_rescue disable_backup detach_iso
request_console
request_console remove_from_placement_group
].each do |action|
define_method(action) do
prepare_request("actions/#{action}", method: :post)
Expand Down
163 changes: 69 additions & 94 deletions spec/hcloud/server_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
require 'spec_helper'

describe Hcloud::Server, doubles: :server do
include_context 'test doubles'
include_context 'action tests'

let :servers do
Array.new(Faker::Number.within(range: 20..150)).map { new_server }
end
Expand All @@ -19,23 +22,6 @@
Hcloud::Client.new(token: 'secure')
end

def test_parameterless_action(action_name, command_name)
expectation = stub_action(:servers, server[:id], action_name) do |_req, _info|
{
action: build_action_resp(
command_name, :running,
resources: [{ id: server[:id], type: 'server' }]
)
}
end

action = server_obj.send(action_name)
expect(expectation.times_called).to eq(1)

expect(action).to be_a(Hcloud::Action)
expect(action.command).to eq(command_name.to_s)
end

context '#enable_rescue' do
it 'works' do
expectation = stub_action(:servers, server[:id], :enable_rescue) do |req, _info|
Expand Down Expand Up @@ -151,24 +137,10 @@ def test_parameterless_action(action_name, command_name)
end

it 'works' do
expectation = stub_action(:servers, server[:id], :change_type) do |req, _info|
expect(req).to have_body_params(
a_hash_including({ 'server_type' => 'cx11', 'upgrade_disk' => true })
)

{
action: build_action_resp(
:change_server_type, :running,
resources: [{ id: server[:id], type: 'server' }]
)
}
end

action = server_obj.change_type(server_type: 'cx11', upgrade_disk: true)
expect(expectation.times_called).to eq(1)

expect(action).to be_a(Hcloud::Action)
expect(action.command).to eq('change_server_type')
test_action(
:change_type, :change_server_type,
params: { server_type: 'cx11', upgrade_disk: true }
)
end
end

Expand All @@ -180,24 +152,7 @@ def test_parameterless_action(action_name, command_name)
end

it 'works' do
expectation = stub_action(:servers, server[:id], :attach_iso) do |req, _info|
expect(req).to have_body_params(
a_hash_including({ 'iso' => 'FreeBSD-11.0-RELEASE-amd64-dvd1' })
)

{
action: build_action_resp(
:attach_iso, :running,
resources: [{ id: server[:id], type: 'server' }]
)
}
end

action = server_obj.attach_iso(iso: 'FreeBSD-11.0-RELEASE-amd64-dvd1')
expect(expectation.times_called).to eq(1)

expect(action).to be_a(Hcloud::Action)
expect(action.command).to eq('attach_iso')
test_action(:attach_iso, params: { iso: 'FreeBSD-11.0-RELEASE-amd64-dvd1' })
end
end

Expand All @@ -209,53 +164,73 @@ def test_parameterless_action(action_name, command_name)
end

it 'works' do
expectation = stub_action(:servers, server[:id], :attach_to_network) do |req, _info|
expect(req).to have_body_params(
a_hash_including({ 'network' => 1, 'ip' => '10.0.0.10' })
)
test_action(:attach_to_network, params: { network: 1, ip: '10.0.0.10' })
end
end

{
action: build_action_resp(
:attach_to_network, :running,
resources: [{ id: server[:id], type: 'server' }, { id: 1, type: 'network' }]
)
}
end
context '#detach_from_network' do
it 'handles missing network' do
expect do
server_obj.detach_from_network(network: nil)
end.to raise_error Hcloud::Error::InvalidInput
end

action = server_obj.attach_to_network(network: 1, ip: '10.0.0.10')
expect(expectation.times_called).to eq(1)
it 'works' do
test_action(:detach_from_network, params: { network: 1 })
end
end

expect(action).to be_a(Hcloud::Action)
expect(action.command).to eq('attach_to_network')
context '#add_to_placement_group' do
it 'handles missing placement_group' do
expect do
server_obj.add_to_placement_group(placement_group: nil)
end.to raise_error Hcloud::Error::InvalidInput
end

it 'works' do
test_action(:add_to_placement_group, params: { placement_group: 42 })
end
end

context '#detach_from_network' do
context '#remove_from_placement_group' do
it 'works' do
test_action(:remove_from_placement_group)
end
end

context '#change_alias_ips' do
it 'handles missing alias_ips' do
expect do
server_obj.change_alias_ips(alias_ips: nil, network: 42)
end.to raise_error Hcloud::Error::InvalidInput
end

it 'handles missing network' do
expect do
server_obj.detach_from_network(network: nil)
server_obj.change_alias_ips(alias_ips: ['10.0.10.2'], network: nil)
end.to raise_error Hcloud::Error::InvalidInput
end

it 'works' do
expectation = stub_action(:servers, server[:id], :detach_from_network) do |req, _info|
expect(req).to have_body_params(
a_hash_including({ 'network' => 1 })
)
test_action(:change_alias_ips, params: { alias_ips: ['10.0.10.2'], network: 42 })
end
end

{
action: build_action_resp(
:detach_from_network, :running,
resources: [{ id: server[:id], type: 'server' }, { id: 1, type: 'network' }]
)
}
end
context '#change_dns_ptr' do
it 'handles missing dns_ptr' do
expect do
server_obj.change_dns_ptr(ip: '192.0.2.0', dns_ptr: nil)
end.to raise_error Hcloud::Error::InvalidInput
end

action = server_obj.detach_from_network(network: 1)
expect(expectation.times_called).to eq(1)
it 'handles missing ip' do
expect do
server_obj.change_dns_ptr(ip: nil, dns_ptr: 'example.com')
end.to raise_error Hcloud::Error::InvalidInput
end

expect(action).to be_a(Hcloud::Action)
expect(action.command).to eq('detach_from_network')
it 'works' do
test_action(:change_dns_ptr, params: { ip: '192.0.2.0', dns_ptr: 'example.com' })
end
end

Expand Down Expand Up @@ -284,38 +259,38 @@ def test_parameterless_action(action_name, command_name)
end

it '#enable_backup' do
test_parameterless_action(:enable_backup, :enable_backup)
test_action(:enable_backup)
end

it '#disable_backup' do
test_parameterless_action(:disable_backup, :disable_backup)
test_action(:disable_backup)
end

it '#poweron' do
test_parameterless_action(:poweron, :start_server)
test_action(:poweron, :start_server)
end

it '#poweroff' do
test_parameterless_action(:poweroff, :stop_server)
test_action(:poweroff, :stop_server)
end

it '#shutdown' do
test_parameterless_action(:shutdown, :shutdown_server)
test_action(:shutdown, :shutdown_server)
end

it '#reboot' do
test_parameterless_action(:reboot, :reboot_server)
test_action(:reboot, :reboot_server)
end

it '#reset' do
test_parameterless_action(:reset, :reset_server)
test_action(:reset, :reset_server)
end

it '#disable_rescue' do
test_parameterless_action(:disable_rescue, :disable_rescue)
test_action(:disable_rescue)
end

it '#detach_iso' do
test_parameterless_action(:detach_iso, :detach_iso)
test_action(:detach_iso)
end
end