diff --git a/lib/fog/rackspace/compute.rb b/lib/fog/rackspace/compute.rb index e6fb697d5..c4b408f41 100644 --- a/lib/fog/rackspace/compute.rb +++ b/lib/fog/rackspace/compute.rb @@ -29,8 +29,10 @@ module Fog request :list_images_detail request :list_servers request :list_servers_detail - request :reboot_server request :update_server + request :action + request :reboot_server + request :resize_server class Mock diff --git a/lib/fog/rackspace/requests/compute/action.rb b/lib/fog/rackspace/requests/compute/action.rb new file mode 100644 index 000000000..09f6ef3bd --- /dev/null +++ b/lib/fog/rackspace/requests/compute/action.rb @@ -0,0 +1,33 @@ +module Fog + module Rackspace + class Compute + class Real + + # Reboot an existing server + # + # ==== Parameters + # * server_id<~Integer> - Id of server to reboot + # * body<~.to_json object> - Body of the request, describes the action (see reboot_server as an example) + # * expect<~Integer> - expected return, 202 except for confirm resize (204) + # + def action(server_id, body, expects=202) + request( + :body => body.to_json, + :expects => expects, + :method => 'POST', + :path => "servers/#{server_id}/action.json" + ) + end + + end + + class Mock + + def action(server_id, body) + Fog::Mock.not_implemented + end + + end + end + end +end diff --git a/lib/fog/rackspace/requests/compute/reboot_server.rb b/lib/fog/rackspace/requests/compute/reboot_server.rb index 90aea77f8..477170b4d 100644 --- a/lib/fog/rackspace/requests/compute/reboot_server.rb +++ b/lib/fog/rackspace/requests/compute/reboot_server.rb @@ -10,12 +10,8 @@ module Fog # * type<~String> - Type of reboot, must be in ['HARD', 'SOFT'] # def reboot_server(server_id, type = 'SOFT') - request( - :body => { 'reboot' => { 'type' => type }}.to_json, - :expects => 202, - :method => 'POST', - :path => "servers/#{server_id}/action.json" - ) + body = { 'reboot' => { 'type' => type }} + action(server_id, body) end end @@ -23,7 +19,9 @@ module Fog class Mock def reboot_server(server_id, type = 'SOFT') - Fog::Mock.not_implemented + response = Excon::Response.new + response.status = 202 + response end end diff --git a/lib/fog/rackspace/requests/compute/resize_server.rb b/lib/fog/rackspace/requests/compute/resize_server.rb new file mode 100644 index 000000000..2e53b5ad0 --- /dev/null +++ b/lib/fog/rackspace/requests/compute/resize_server.rb @@ -0,0 +1,80 @@ +module Fog + module Rackspace + class Compute + class Real + + # Reboot an existing server + # + # ==== Parameters + # * server_id<~Integer> - Id of server to resize + # * size<~String> - new size. call list_flavors to get available flavors + # + def resize_server(server_id, flavor_id) + body = { 'resize' => { 'flavorId' => flavor_id }} + action(server_id, body) + end + + # Confirm resizing + # + # ==== Parameters + # * server_id<~Integer> - Id of server to confirm + # + def confirm_resize(server_id) + body = { 'confirmResize' => nil } + action(server_id, body, 204) + end + + # Revert resizing + # + # ==== Parameters + # * server_id<~Integer> - Id of server to revert + # + def revert_resize(server_id) + body = { 'revertResize' => nil } + action(server_id, body) + end + + end + + class Mock + + def resize_server(server_id, flavor_id) + response = Excon::Response.new + response.status = 202 + + #I know this is weird... but I got to keep track of it + @data[:servers][server_id]['old_flavorId'] = @data[:servers][server_id]['flavorId'] + + @data[:servers][server_id]['flavorId'] = flavor_id + @data[:last_modified][:servers][server_id] = Time.now + @data[:servers][server_id]['status'] = 'VERIFY_RESIZE' + + response + end + + def confirm_resize(server_id) + response = Excon::Response.new + response.status = 204 + + @data[:last_modified][:servers][server_id] = Time.now + @data[:servers][server_id]['status'] = 'ACTIVE' + + response + end + + def revert_resize(server_id) + response = Excon::Response.new + response.status = 202 + + @data[:servers][server_id]['flavorId'] = @data[:servers][server_id]['old_flavorId'] + @data[:servers][server_id].delete('old_flavorId') + @data[:last_modified][:servers][server_id] = Time.now + @data[:servers][server_id]['status'] = 'ACTIVE' + + response + end + + end + end + end +end diff --git a/tests/rackspace/requests/compute/reboot_tests.rb b/tests/rackspace/requests/compute/reboot_tests.rb new file mode 100644 index 000000000..71b134412 --- /dev/null +++ b/tests/rackspace/requests/compute/reboot_tests.rb @@ -0,0 +1,17 @@ +Shindo.tests('Rackspace::Compute | reboot request', ['rackspace']) do + + tests('success') do + + @server = Rackspace[:compute].servers.create(:flavor_id => 1, :image_id => 19, :name => 'fogactions') + + @server.wait_for { ready? } + + tests("#reboot_server(#{@server.id})") do + returns(Rackspace[:compute].reboot_server(@server.id).status) { 202 } + end + + @server.destroy + + end + +end diff --git a/tests/rackspace/requests/compute/resize_tests.rb b/tests/rackspace/requests/compute/resize_tests.rb new file mode 100644 index 000000000..287fedacb --- /dev/null +++ b/tests/rackspace/requests/compute/resize_tests.rb @@ -0,0 +1,43 @@ +Shindo.tests('Rackspace::Compute | resize request', ['rackspace']) do + + tests('success_confirm') do + + @server = Rackspace[:compute].servers.create(:flavor_id => 1, :image_id => 19, :name => 'fogresize') + + @server.wait_for { ready? } + + tests("#resize_server(#{@server.id}, 2)") do + returns(Rackspace[:compute].resize_server(@server.id, 2).status) { 202 } + returns(Rackspace[:compute].get_server_details(@server.id).body['server']['status']) { 'VERIFY_RESIZE' } + end + + tests("#confirm_resize(#{@server.id})") do + returns(Rackspace[:compute].confirm_resize(@server.id).status) { 204 } + returns(Rackspace[:compute].get_server_details(@server.id).body['server']['status']) { 'ACTIVE' } + end + + @server.destroy + + end + + tests('success_revert') do + + @server = Rackspace[:compute].servers.create(:flavor_id => 1, :image_id => 19, :name => 'fogresize') + + @server.wait_for { ready? } + + tests("#resize_server(#{@server.id}, 2)") do + returns(Rackspace[:compute].resize_server(@server.id, 2).status) { 202 } + returns(Rackspace[:compute].get_server_details(@server.id).body['server']['status']) { 'VERIFY_RESIZE' } + end + + tests("#revert_resize(#{@server.id})") do + returns(Rackspace[:compute].revert_resize(@server.id).status) { 202 } + returns(Rackspace[:compute].get_server_details(@server.id).body['server']['status']) { 'ACTIVE' } + end + + @server.destroy + + end + +end