Skip to content

Commit

Permalink
Implemented review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
ameyavarade committed May 29, 2014
1 parent bac4c2a commit b352b40
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lib/chef/knife/cloud/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ServerCreateDependenciesError < KnifeCloudError; end
class BootstrapError < KnifeCloudError; end
class ServerShowError < KnifeCloudError; end
class ChefServerError < KnifeCloudError; end
class ServiceNetworkError < KnifeCloudError; end
class NetworkNotFoundError < KnifeCloudError; end
end
end
end
Expand Down
40 changes: 20 additions & 20 deletions lib/chef/knife/cloud/fog/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def load_fog_gem
def connection
add_api_endpoint
@connection ||= begin
connection = Fog::Compute.new(@auth_params)
connection = Fog::Compute.new(@auth_params)
rescue Excon::Errors::Unauthorized => e
error_message = "Connection failure, please check your username and password."
ui.fatal(error_message)
Expand All @@ -46,20 +46,20 @@ def connection

def network
@network ||= begin
network = Fog::Network.new(@auth_params)
rescue Excon::Errors::Unauthorized => e
error_message = "Connection failure, please check your username and password."
ui.fatal(error_message)
raise CloudExceptions::ServiceConnectionError, "#{e.message}. #{error_message}"
rescue Excon::Errors::SocketError => e
error_message = "Connection failure, please check your authentication URL."
ui.fatal(error_message)
raise CloudExceptions::ServiceConnectionError, "#{e.message}. #{error_message}"
rescue Fog::Errors::NotFound => e
error_message = "No Network service found. This command is unavailable with current cloud."
ui.fatal(error_message)
raise CloudExceptions::ServiceNetworkError, "#{e.message}. #{error_message}"
end
network = Fog::Network.new(@auth_params)
rescue Excon::Errors::Unauthorized => e
error_message = "Connection failure, please check your username and password."
ui.fatal(error_message)
raise CloudExceptions::ServiceConnectionError, "#{e.message}. #{error_message}"
rescue Excon::Errors::SocketError => e
error_message = "Connection failure, please check your authentication URL."
ui.fatal(error_message)
raise CloudExceptions::ServiceConnectionError, "#{e.message}. #{error_message}"
rescue Fog::Errors::NotFound => e
error_message = "No Network service found. This command is unavailable with current cloud."
ui.fatal(error_message)
raise CloudExceptions::NetworkNotFoundError, "#{e.message}. #{error_message}"
end
end

# cloud server specific implementation methods for commands.
Expand Down Expand Up @@ -111,14 +111,14 @@ def delete_server(server_name)
end
end

["servers", "images", "networks"].each do |iterator|
define_method("list_#{iterator}") do
["servers", "images", "networks"].each do |resource_type|
define_method("list_#{resource_type}") do
begin
case iterator
case resource_type
when "networks"
network.method(iterator).call.all
network.method(resource_type).call.all
else
connection.method(iterator).call.all
connection.method(resource_type).call.all
end
rescue Excon::Errors::BadRequest => e
handle_excon_exception(CloudExceptions::CloudAPIException, e)
Expand Down
68 changes: 42 additions & 26 deletions spec/unit/fog_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@
Fog::Network.should_receive(:new).with({:provider => 'Any Cloud Provider'})
instance.network
end

context "connection to fog" do
before do
instance.stub(:exit)
instance.stub(:ui).and_return(Object.new)
instance.ui.should_receive(:fatal)
end

it "handles Unauthorized exception." do
Fog::Network.should_receive(:new).with({:provider => 'Any Cloud Provider'}).and_raise Excon::Errors::Unauthorized.new("Unauthorized")
expect {instance.network}.to raise_error(Chef::Knife::Cloud::CloudExceptions::ServiceConnectionError)
end

it "handles SocketError or any other connection exception." do
socket_error = Excon::Errors::SocketError.new(Exception.new "Mock Error")
Fog::Network.should_receive(:new).with({:provider => 'Any Cloud Provider'}).and_raise socket_error
expect {instance.network}.to raise_error(Chef::Knife::Cloud::CloudExceptions::ServiceConnectionError)
end

it "handles NetworkNotFoundError exception." do
Fog::Network.should_receive(:new).with({:provider => 'Any Cloud Provider'}).and_raise Fog::Errors::NotFound.new("NotFound")
expect {instance.network}.to raise_error(Chef::Knife::Cloud::CloudExceptions::NetworkNotFoundError)
end
end
end

context "add_custom_attributes" do
Expand All @@ -45,33 +69,25 @@
end
end

["servers", "images", "networks"].each do |iterator|
context "list #{iterator}" do
case iterator
when "networks"
it "lists #{iterator}." do
instance.stub_chain(:network, "#{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(:network, "#{iterator}".to_sym, :all).and_raise Excon::Errors::BadRequest.new("Invalid Network")
expect {instance.method("list_#{iterator}").call}.to raise_error(Chef::Knife::Cloud::CloudExceptions::CloudAPIException)
end
else
it "lists #{iterator}." do
instance.stub_chain(:connection, "#{iterator}".to_sym, :all)
instance.method("list_#{iterator}").call
end
["servers", "images", "networks"].each do |resource_type|
resource = case resource_type
when "networks"
:network
else
:connection
end
context "list #{resource_type}" do

it "lists #{resource_type} of the current cloud service provider account." do
instance.stub_chain(resource.to_sym, "#{resource_type}".to_sym, :all)
instance.method("list_#{resource_type}").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
it "handles Excon::Errors::BadRequest exception." do
instance.stub(:ui).and_return(Object.new)
instance.ui.should_receive(:fatal)
instance.stub_chain(resource.to_sym, "#{resource_type}".to_sym, :all).and_raise Excon::Errors::BadRequest.new("Invalid Resource")
expect {instance.method("list_#{resource_type}").call}.to raise_error(Chef::Knife::Cloud::CloudExceptions::CloudAPIException)
end
end
end
Expand Down

0 comments on commit b352b40

Please sign in to comment.