Skip to content

Commit

Permalink
Merge pull request #65 from opscode/ameya-service-refactor
Browse files Browse the repository at this point in the history
Refactored redundant code.
  • Loading branch information
Mukta Aphale committed May 23, 2014
2 parents 4e2e8da + 3b21d0a commit c4e7904
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
48 changes: 19 additions & 29 deletions lib/chef/knife/cloud/fog/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,43 +89,36 @@ def delete_server(server_name)
ui.error(error_message)
raise CloudExceptions::ServerDeleteError, error_message
rescue Excon::Errors::BadRequest => e
response = Chef::JSONCompat.from_json(e.response.body)
error_message = "Unknown server error (#{response['badRequest']['code']}): #{response['badRequest']['message']}"
ui.fatal(error_message)
raise CloudExceptions::ServerDeleteError, error_message
handle_excon_exception(CloudExceptions::ServerDeleteError, e)
end
end

def list_servers
begin
servers = connection.servers.all
rescue Excon::Errors::BadRequest => e
response = Chef::JSONCompat.from_json(e.response.body)
error_message = "Unknown server error (#{response['badRequest']['code']}): #{response['badRequest']['message']}"
ui.fatal(error_message)
raise CloudExceptions::CloudAPIException, error_message
["servers", "images"].each do |iterator|
define_method("list_#{iterator}") do
begin
connection.method(iterator).call.all
rescue Excon::Errors::BadRequest => e
handle_excon_exception(CloudExceptions::CloudAPIException, e)
end
end
end

def list_images
begin
images = connection.images.all
rescue Excon::Errors::BadRequest => e
response = Chef::JSONCompat.from_json(e.response.body)
error_message = "Unknown server error (#{response['badRequest']['code']}): #{response['badRequest']['message']}"
ui.fatal(error_message)
raise CloudExceptions::CloudAPIException, error_message
end
def handle_excon_exception(exception_class, e)
error_message = if e.response
response = Chef::JSONCompat.from_json(e.response.body)
"Unknown server error (#{response['badRequest']['code']}): #{response['badRequest']['message']}"
else
"Unknown server error : #{e.message}"
end
ui.fatal(error_message)
raise exception_class, error_message
end

def list_resource_configurations
begin
flavors = connection.flavors.all
rescue Excon::Errors::BadRequest => e
response = Chef::JSONCompat.from_json(e.response.body)
error_message = "Unknown server error (#{response['badRequest']['code']}): #{response['badRequest']['message']}"
ui.fatal(error_message)
raise CloudExceptions::CloudAPIException, error_message
handle_excon_exception(CloudExceptions::CloudAPIException, e)
end
end

Expand All @@ -145,10 +138,7 @@ def get_server(instance_id)
begin
server = connection.servers.get(instance_id)
rescue Excon::Errors::BadRequest => e
response = Chef::JSONCompat.from_json(e.response.body)
error_message = "Unknown server error (#{response['badRequest']['code']}): #{response['badRequest']['message']}"
ui.fatal(error_message)
raise CloudExceptions::KnifeCloudError, error_message
handle_excon_exception(CloudExceptions::KnifeCloudError, e)
end
end

Expand Down
17 changes: 17 additions & 0 deletions spec/unit/fog_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,21 @@
expect(@server_def[:state] == "Inactive").to be true
end
end

["servers", "images"].each do |iterator|
context "list #{iterator}" do

it "lists #{iterator}." do
instance.stub_chain(:connection, "#{iterator}".to_sym, :all)
instance.method("list_#{iterator}").call
end

it "handles Excon::Errors::BadRequest exception." do
instance.stub(:ui).and_return(Object.new)
instance.ui.should_receive(:fatal)
instance.stub_chain(:connection, "#{iterator}".to_sym, :all).and_raise Excon::Errors::BadRequest.new("Invalid Server")
expect {instance.method("list_#{iterator}").call}.to raise_error(Chef::Knife::Cloud::CloudExceptions::CloudAPIException)
end
end
end
end

0 comments on commit c4e7904

Please sign in to comment.