From 7e663aaa0267431a2f023418988b765384c98339 Mon Sep 17 00:00:00 2001 From: Kyle Rames Date: Tue, 26 Mar 2013 09:14:34 -0500 Subject: [PATCH 1/3] [rackspace|compute_v2] added specific exception for invalid states in order to make it easier to rescue. --- lib/fog/rackspace/compute_v2.rb | 23 +++++++++ lib/fog/rackspace/models/compute_v2/image.rb | 4 +- lib/fog/rackspace/models/compute_v2/server.rb | 4 +- .../rackspace/models/compute_v2/servers.rb | 3 +- .../models/compute_v2/image_tests.rb | 51 +++++++++++++++++++ .../models/compute_v2/server_tests.rb | 51 +++++++++++++++++++ 6 files changed, 131 insertions(+), 5 deletions(-) diff --git a/lib/fog/rackspace/compute_v2.rb b/lib/fog/rackspace/compute_v2.rb index 897edf456..1ca1f9c25 100644 --- a/lib/fog/rackspace/compute_v2.rb +++ b/lib/fog/rackspace/compute_v2.rb @@ -8,6 +8,29 @@ module Fog class InternalServerError < Fog::Rackspace::Errors::InternalServerError; end class BadRequest < Fog::Rackspace::Errors::BadRequest; end + class InvalidStateException < ::RuntimeError + + attr_reader :desired_state + attr_reader :current_state + + def initialize(desired_state, current_state) + @desired_state = desired_state + @current_state = current_state + end + end + + class InvalidServerStateException < InvalidStateException + def to_s + "Server should have transitioned to '#{desired_state}' not '#{state}'" + end + end + + class InvalidImageStateException < InvalidStateException + def to_s + "Image should have transitioned to '#{desired_state}' not '#{state}'" + end + end + DFW_ENDPOINT = 'https://dfw.servers.api.rackspacecloud.com/v2' ORD_ENDPOINT = 'https://ord.servers.api.rackspacecloud.com/v2' LON_ENDPOINT = 'https://lon.servers.api.rackspacecloud.com/v2' diff --git a/lib/fog/rackspace/models/compute_v2/image.rb b/lib/fog/rackspace/models/compute_v2/image.rb index 4bf0df617..be5a1e594 100644 --- a/lib/fog/rackspace/models/compute_v2/image.rb +++ b/lib/fog/rackspace/models/compute_v2/image.rb @@ -89,11 +89,11 @@ module Fog # @param [String] ready_state By default state is ACTIVE # @param [Array,String] error_states By default state is ERROR # @return [Boolean] returns true if server is in a ready state - # @raise [RuntimeException] if server state is an error state + # @raise [Fog::Compute::RackspaceV2::InvalidImageStateException] if server state is an error state def ready?(ready_state = ACTIVE, error_states=[ERROR]) if error_states error_states = Array(error_states) - raise "Image should have transitioned to '#{ready_state}' not '#{state}'" if error_states.include?(state) + raise InvalidImageStateException.new(ready_state, state) if error_states.include?(state) end state == ready_state end diff --git a/lib/fog/rackspace/models/compute_v2/server.rb b/lib/fog/rackspace/models/compute_v2/server.rb index a682c3e2e..2c9abd235 100644 --- a/lib/fog/rackspace/models/compute_v2/server.rb +++ b/lib/fog/rackspace/models/compute_v2/server.rb @@ -311,11 +311,11 @@ module Fog # @param [String] ready_state By default state is ACTIVE # @param [Array,String] error_states By default state is ERROR # @return [Boolean] returns true if server is in a ready state - # @raise [RuntimeException] if server state is an error state + # @raise [Fog::Compute::RackspaceV2::InvalidServerStateException] if server state is an error state def ready?(ready_state = ACTIVE, error_states=[ERROR]) if error_states error_states = Array(error_states) - raise "Server should have transitioned to '#{ready_state}' not '#{state}'" if error_states.include?(state) + raise InvalidServerStateException.new(ready_state, state) if error_states.include?(state) end state == ready_state end diff --git a/lib/fog/rackspace/models/compute_v2/servers.rb b/lib/fog/rackspace/models/compute_v2/servers.rb index 0da199520..02a42597e 100644 --- a/lib/fog/rackspace/models/compute_v2/servers.rb +++ b/lib/fog/rackspace/models/compute_v2/servers.rb @@ -27,7 +27,8 @@ module Fog # :image_id => service.images.find {|img| img.name =~ /Ubuntu/}.id, # :public_key_path => '~/.ssh/fog_rsa.pub', # :private_key_path => '~/.ssh/fog_rsa' - # + # + # @raise [Fog::Compute::RackspaceV2::InvalidServerStateException] if server state is an error state def bootstrap(new_attributes = {}) server = create(new_attributes) server.wait_for(1500) { ready? && !public_ip_address.empty? } diff --git a/tests/rackspace/models/compute_v2/image_tests.rb b/tests/rackspace/models/compute_v2/image_tests.rb index b36e80610..f3c437184 100644 --- a/tests/rackspace/models/compute_v2/image_tests.rb +++ b/tests/rackspace/models/compute_v2/image_tests.rb @@ -8,6 +8,57 @@ Shindo.tests('Fog::Compute::RackspaceV2 | image', ['rackspace']) do :image_id => rackspace_test_image_id(service) } + tests('ready?') do + @server = Fog::Compute::RackspaceV2::Image.new + + tests('default in ready state').returns(true) do + @server.state = Fog::Compute::RackspaceV2::Image::ACTIVE + @server.ready? + end + + tests('custom ready state').returns(true) do + @server.state = Fog::Compute::RackspaceV2::Image::SAVING + @server.ready?(Fog::Compute::RackspaceV2::Image::SAVING) + end + + tests('default NOT in ready state').returns(false) do + @server.state = Fog::Compute::RackspaceV2::Image::SAVING + @server.ready? + end + + tests('custom NOT ready state').returns(false) do + @server.state = Fog::Compute::RackspaceV2::Image::UNKNOWN + @server.ready?(Fog::Compute::RackspaceV2::Image::SAVING) + end + + tests('default error state').returns(true) do + @server.state = Fog::Compute::RackspaceV2::Image::ERROR + exception_occurred = false + begin + @server.ready? + rescue Fog::Compute::RackspaceV2::InvalidImageStateException => e + exception_occurred = true + returns(true) {e.desired_state == Fog::Compute::RackspaceV2::Image::ACTIVE } + returns(true) {e.current_state == Fog::Compute::RackspaceV2::Image::ERROR } + end + exception_occurred + end + + tests('custom error state').returns(true) do + @server.state = Fog::Compute::RackspaceV2::Image::UNKNOWN + exception_occurred = false + begin + @server.ready?(Fog::Compute::RackspaceV2::Image::SAVING, Fog::Compute::RackspaceV2::Image::UNKNOWN) + rescue Fog::Compute::RackspaceV2::InvalidImageStateException => e + exception_occurred = true + returns(true) {e.desired_state == Fog::Compute::RackspaceV2::Image::SAVING } + returns(true) {e.current_state == Fog::Compute::RackspaceV2::Image::UNKNOWN } + end + exception_occurred + end + end + + tests("success") do begin server = service.servers.create(options) diff --git a/tests/rackspace/models/compute_v2/server_tests.rb b/tests/rackspace/models/compute_v2/server_tests.rb index f8657d32e..49f260d14 100644 --- a/tests/rackspace/models/compute_v2/server_tests.rb +++ b/tests/rackspace/models/compute_v2/server_tests.rb @@ -9,6 +9,57 @@ Shindo.tests('Fog::Compute::RackspaceV2 | server', ['rackspace']) do :metadata => { 'fog_test' => 'true' } } + tests('ready?') do + @server = Fog::Compute::RackspaceV2::Server.new + + tests('default in ready state').returns(true) do + @server.state = Fog::Compute::RackspaceV2::Server::ACTIVE + @server.ready? + end + + tests('custom ready state').returns(true) do + @server.state = Fog::Compute::RackspaceV2::Server::VERIFY_RESIZE + @server.ready?(Fog::Compute::RackspaceV2::Server::VERIFY_RESIZE) + end + + tests('default NOT in ready state').returns(false) do + @server.state = Fog::Compute::RackspaceV2::Server::REBOOT + @server.ready? + end + + tests('custom NOT ready state').returns(false) do + @server.state = Fog::Compute::RackspaceV2::Server::REBOOT + @server.ready?(Fog::Compute::RackspaceV2::Server::VERIFY_RESIZE) + end + + tests('default error state').returns(true) do + @server.state = Fog::Compute::RackspaceV2::Server::ERROR + exception_occurred = false + begin + @server.ready? + rescue Fog::Compute::RackspaceV2::InvalidServerStateException => e + exception_occurred = true + returns(true) {e.desired_state == Fog::Compute::RackspaceV2::Server::ACTIVE } + returns(true) {e.current_state == Fog::Compute::RackspaceV2::Server::ERROR } + end + exception_occurred + end + + tests('custom error state').returns(true) do + @server.state = Fog::Compute::RackspaceV2::Server::ACTIVE + exception_occurred = false + begin + @server.ready?(Fog::Compute::RackspaceV2::Server::VERIFY_RESIZE, Fog::Compute::RackspaceV2::Server::ACTIVE) + rescue Fog::Compute::RackspaceV2::InvalidServerStateException => e + exception_occurred = true + returns(true) {e.desired_state == Fog::Compute::RackspaceV2::Server::VERIFY_RESIZE } + returns(true) {e.current_state == Fog::Compute::RackspaceV2::Server::ACTIVE } + end + exception_occurred + end + + end + model_tests(service.servers, options, true) do @instance.wait_for(timeout=1500) { ready? } From c5637517bee1a571bb1358cd70d8318fabf411fe Mon Sep 17 00:00:00 2001 From: Kyle Rames Date: Wed, 27 Mar 2013 16:24:30 -0500 Subject: [PATCH 2/3] [rackspace] adding note about credentials in the getting started document --- lib/fog/rackspace/docs/getting_started.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/fog/rackspace/docs/getting_started.md b/lib/fog/rackspace/docs/getting_started.md index 2076ab2f9..8bd4a6d60 100644 --- a/lib/fog/rackspace/docs/getting_started.md +++ b/lib/fog/rackspace/docs/getting_started.md @@ -14,6 +14,8 @@ To obtain credentials for the US Rackspace Cloud, please sign up for an account Likewise, you can create an account on our UK Rackspace Open Cloud by going to [UK Rackspace Open Cloud](https://buyonline.rackspace.co.uk/cloud/userinfo?type=normal) and login into [Cloud Control Panel (UK)](https://mycloud.rackspace.co.uk/). +The credentials will be used when we further explore fog services in the [Next Steps](#next-steps) section. + ## Installation To install Fog via RubyGems run the following command: From a588d0d327375e6fe83e23e1103bd0cefaa0f51e Mon Sep 17 00:00:00 2001 From: Kyle Rames Date: Wed, 27 Mar 2013 16:27:38 -0500 Subject: [PATCH 3/3] Revert "[rackspace] adding note about credentials in the getting started document" This reverts commit c5637517bee1a571bb1358cd70d8318fabf411fe. --- lib/fog/rackspace/docs/getting_started.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/fog/rackspace/docs/getting_started.md b/lib/fog/rackspace/docs/getting_started.md index 8bd4a6d60..2076ab2f9 100644 --- a/lib/fog/rackspace/docs/getting_started.md +++ b/lib/fog/rackspace/docs/getting_started.md @@ -14,8 +14,6 @@ To obtain credentials for the US Rackspace Cloud, please sign up for an account Likewise, you can create an account on our UK Rackspace Open Cloud by going to [UK Rackspace Open Cloud](https://buyonline.rackspace.co.uk/cloud/userinfo?type=normal) and login into [Cloud Control Panel (UK)](https://mycloud.rackspace.co.uk/). -The credentials will be used when we further explore fog services in the [Next Steps](#next-steps) section. - ## Installation To install Fog via RubyGems run the following command: