From d3e4b889a43c68cca1a872aba27fb1404b88d1f1 Mon Sep 17 00:00:00 2001 From: Tzu-Mainn Chen Date: Tue, 26 May 2015 22:01:34 -0400 Subject: [PATCH] [openstack|baremetal] added support for setting node power/provision states --- lib/fog/openstack/baremetal.rb | 4 +- lib/fog/openstack/models/baremetal/node.rb | 10 +++++ .../baremetal/set_node_power_state.rb | 35 +++++++++++++++++ .../baremetal/set_node_provision_state.rb | 38 +++++++++++++++++++ .../requests/baremetal/node_tests.rb | 10 +++++ 5 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 lib/fog/openstack/requests/baremetal/set_node_power_state.rb create mode 100644 lib/fog/openstack/requests/baremetal/set_node_provision_state.rb diff --git a/lib/fog/openstack/baremetal.rb b/lib/fog/openstack/baremetal.rb index 5efe4f35f..dc22d9576 100644 --- a/lib/fog/openstack/baremetal.rb +++ b/lib/fog/openstack/baremetal.rb @@ -34,6 +34,8 @@ module Fog request :list_nodes request :list_nodes_detailed request :patch_node + request :set_node_power_state + request :set_node_provision_state # Chassis requests request :create_chassis @@ -69,8 +71,6 @@ module Fog # request :set_boot_device # request :list_supported_boot_devices # request :list_node_states - # request :set_power_state - # request :provision_node # request :get_console_info # request :change_console_state # request :get_vendor_passthru_methods diff --git a/lib/fog/openstack/models/baremetal/node.rb b/lib/fog/openstack/models/baremetal/node.rb index f0833b7a2..1cb89941f 100644 --- a/lib/fog/openstack/models/baremetal/node.rb +++ b/lib/fog/openstack/models/baremetal/node.rb @@ -79,6 +79,16 @@ module Fog requires :uuid service.get_node(self.uuid).headers end + + def set_power_state(power_state) + requires :uuid + service.set_node_power_state(self.uuid, power_state) + end + + def set_provision_state(provision_state) + requires :uuid + service.set_node_provision_state(self.uuid, provision_state) + end end end end diff --git a/lib/fog/openstack/requests/baremetal/set_node_power_state.rb b/lib/fog/openstack/requests/baremetal/set_node_power_state.rb new file mode 100644 index 000000000..7e33ca06f --- /dev/null +++ b/lib/fog/openstack/requests/baremetal/set_node_power_state.rb @@ -0,0 +1,35 @@ +module Fog + module Baremetal + class OpenStack + class Real + def set_node_power_state(node_id, power_state) + data = { + 'target' => power_state + } + + request( + :body => Fog::JSON.encode(data), + :expects => 202, + :method => 'PUT', + :path => "nodes/#{node_id}/states/power" + ) + end + end + + class Mock + def set_node_power_state(node_id, power_state) + response = Excon::Response.new + response.status = 202 + response.headers = { + "X-Compute-Request-Id" => "req-fdc6f99e-55a2-4ab1-8904-0892753828cf", + "Content-Type" => "application/json", + "Content-Length" => "356", + "Date" => Date.new + } + response.body = self.data[:nodes].first + response + end + end # mock + end # openstack + end # baremetal +end # fog diff --git a/lib/fog/openstack/requests/baremetal/set_node_provision_state.rb b/lib/fog/openstack/requests/baremetal/set_node_provision_state.rb new file mode 100644 index 000000000..9530fd053 --- /dev/null +++ b/lib/fog/openstack/requests/baremetal/set_node_provision_state.rb @@ -0,0 +1,38 @@ +module Fog + module Baremetal + class OpenStack + class Real + def set_node_provision_state(node_id, provision_state) + data = { + 'target' => provision_state + } + + request( + :body => Fog::JSON.encode(data), + :expects => 202, + :method => 'PUT', + :path => "nodes/#{node_id}/states/provision", + :headers => { + :'X-OpenStack-Ironic-API-Version' => 'latest' + } + ) + end + end + + class Mock + def set_node_provision_state(node_id, provision_state) + response = Excon::Response.new + response.status = 202 + response.headers = { + "X-Compute-Request-Id" => "req-fdc6f99e-55a2-4ab1-8904-0892753828cf", + "Content-Type" => "application/json", + "Content-Length" => "356", + "Date" => Date.new + } + response.body = self.data[:nodes].first + response + end + end # mock + end # openstack + end # baremetal +end # fog diff --git a/tests/openstack/requests/baremetal/node_tests.rb b/tests/openstack/requests/baremetal/node_tests.rb index 4c3554efa..9a8e362db 100644 --- a/tests/openstack/requests/baremetal/node_tests.rb +++ b/tests/openstack/requests/baremetal/node_tests.rb @@ -58,6 +58,16 @@ Shindo.tests('Fog::Baremetal[:openstack] | Baremetal node requests', ['openstack [{'op' => 'replace', 'path' => '/driver', 'value' => 'pxe_ssh'}]).body end + tests('#set_node_power_state').data_matches_schema(@detailed_node_format) do + Fog::Baremetal[:openstack].set_node_power_state( + @instance['uuid'], 'power off').body + end + + tests('#set_node_provision_state').data_matches_schema(@detailed_node_format) do + Fog::Baremetal[:openstack].set_node_provision_state( + @instance['uuid'], 'manage').body + end + tests('#delete_node').succeeds do Fog::Baremetal[:openstack].delete_node(@instance['uuid']) end