mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[rackspace|identity] added error handling to make service catalog more robust.
This commit is contained in:
parent
5e72c08bf5
commit
71f7c13efb
2 changed files with 87 additions and 45 deletions
|
@ -32,7 +32,7 @@ module Fog
|
|||
return endpoint if endpoint.is_a?(String) #There is only one endpoint for service
|
||||
|
||||
unless region
|
||||
raise "There are multiple endpoints avaliable for #{service_type}. Please specify one of the following regions: #{display_service_regions(service_type)}."
|
||||
raise "There are multiple endpoints available for #{service_type}. Please specify one of the following regions: #{display_service_regions(service_type)}."
|
||||
end
|
||||
region = region.is_a?(String) ? region.to_sym : region
|
||||
endpoint = get_endpoints(service_type)[region]
|
||||
|
@ -48,32 +48,49 @@ module Fog
|
|||
|
||||
def self.from_response(service, hash)
|
||||
service_catalog = ServiceCatalog.new :service => service
|
||||
begin
|
||||
services = hash["access"]["serviceCatalog"]
|
||||
services.each do |serv|
|
||||
name = serv["name"].to_sym
|
||||
name = serv["name"] ? serv["name"].to_sym : nil
|
||||
next unless name
|
||||
service_catalog.send(:add_endpoints, name, serv)
|
||||
end
|
||||
rescue => e
|
||||
Logger.warning "Exception occurred while loading service catalog - #{e.inspect}"
|
||||
Logger.warning "Unable to load service catalog. Please specify endpoints manually"
|
||||
end
|
||||
service_catalog
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def add_endpoints(service_name, hash)
|
||||
begin
|
||||
endpoints = hash["endpoints"]
|
||||
if endpoints.size == 1
|
||||
catalog[service_name] = endpoints[0]["publicURL"].freeze
|
||||
else
|
||||
catalog[service_name] = endpoints_from_array(endpoints)
|
||||
catalog[service_name] = endpoints_from_array(service_name, endpoints)
|
||||
end
|
||||
rescue => e
|
||||
Logger.warning "Exception occurred while loading #{service_name} service endpoints - #{e.inspect}"
|
||||
Logger.warning "Unable to load service endpoints for #{service_name}. Please specify #{service_name} endpoints manually."
|
||||
end
|
||||
end
|
||||
|
||||
def endpoints_from_array(endpoints)
|
||||
def endpoints_from_array(service_name, endpoints)
|
||||
hash = {}
|
||||
endpoints.each do |endpoint|
|
||||
begin
|
||||
region_name = endpoint["region"]
|
||||
region = region_name ? region_name.downcase.to_sym : :global
|
||||
url = endpoint["publicURL"].freeze
|
||||
hash[region] = url
|
||||
rescue => e
|
||||
Logger.warning "Exception occurred while loading #{service_name} service endpoints - #{e.inspect}"
|
||||
Logger.warning "Unable to parse #{service_name} endpoint - #{endpoint}"
|
||||
Logger.warning "You may need to specify #{service_name} endpoints manually."
|
||||
end
|
||||
end
|
||||
hash
|
||||
end
|
||||
|
|
|
@ -3,6 +3,30 @@ require 'fog/rackspace/models/identity/service_catalog'
|
|||
Shindo.tests('Fog::Rackspace::ServiceCatalog | users', ['rackspace']) do
|
||||
|
||||
tests('#from_response') do
|
||||
tests('missing serviceCatalog node') do
|
||||
before_hash = {"access"=>{"token"=>{"expires"=>"2013-02-20T10:31:00.000-06:00", "tenant"=>{"name"=>"777", "id"=>"777"}, "id"=>"6ca10877-7c50-4a5c-b58f-004d835c39c3"}}}
|
||||
after_hash = {}
|
||||
service_catalog = Fog::Rackspace::Identity::ServiceCatalog.from_response(nil, before_hash)
|
||||
returns(after_hash) { service_catalog.catalog }
|
||||
end
|
||||
|
||||
tests('missing serviceCatalog node') do
|
||||
before_hash = {"access"=>{"token"=>{"expires"=>"2013-02-20T10:31:00.000-06:00", "tenant"=>{"name"=>"777", "id"=>"777"}, "id"=>"6ca10877-7c50-4a5c-b58f-004d835c39c3"}, "serviceCatalog"=>[{"type"=>"volume", "name"=>"cloudBlockStorage", "endpoints" =>[{"region"=>"DFW", "tenantId"=>"777" }]}, {"type"=>"rax:load-balancer","name"=>"cloudLoadBalancers", "endpoints"=>[{"region"=>"ORD", "tenantId"=>"777", "publicURL"=>"https://ord.loadbalancers.api.rackspacecloud.com/v1.0/777"}] }] }}
|
||||
|
||||
after_hash = {:cloudBlockStorage=>nil, :cloudLoadBalancers=>"https://ord.loadbalancers.api.rackspacecloud.com/v1.0/777"}
|
||||
service_catalog = Fog::Rackspace::Identity::ServiceCatalog.from_response(nil, before_hash)
|
||||
returns(after_hash) { service_catalog.catalog }
|
||||
end
|
||||
|
||||
tests('missing region ') do
|
||||
before_hash = {"access"=>{"token"=>{"expires"=>"2013-02-20T10:31:00.000-06:00", "tenant"=>{"name"=>"777", "id"=>"777"}, "id"=>"6ca10877-7c50-4a5c-b58f-004d835c39c3"}, "serviceCatalog"=> [{"type"=>"rax:load-balancer","name"=>"cloudLoadBalancers", "endpoints"=>[{"region"=>"ORD", "tenantId"=>"777", "publicURL"=>"https://ord.loadbalancers.api.rackspacecloud.com/v1.0/777"}, {"tenantId"=>"777", "publicURL"=>"https://ord.loadbalancers.api.rackspacecloud.com/v1.0/777"}] }] }}
|
||||
|
||||
after_hash = {:cloudLoadBalancers=>{:ord=>"https://ord.loadbalancers.api.rackspacecloud.com/v1.0/777"}}
|
||||
service_catalog = Fog::Rackspace::Identity::ServiceCatalog.from_response(nil, before_hash)
|
||||
returns(after_hash) { service_catalog.catalog }
|
||||
end
|
||||
|
||||
tests('successful') do
|
||||
before_hash = {"access"=>{"token"=>{"expires"=>"2013-02-20T10:31:00.000-06:00", "tenant"=>{"name"=>"777", "id"=>"777"}, "id"=>"6ca10877-7c50-4a5c-b58f-004d835c39c3"},
|
||||
"serviceCatalog"=>[{"type"=>"volume", "endpoints"=>[{"region"=>"DFW", "tenantId"=>"777", "publicURL"=>"https://dfw.blockstorage.api.rackspacecloud.com/v1/777"},
|
||||
{"region"=>"ORD", "tenantId"=>"777", "publicURL"=>"https://ord.blockstorage.api.rackspacecloud.com/v1/777"}], "name"=>"cloudBlockStorage"},
|
||||
|
@ -33,6 +57,7 @@ Shindo.tests('Fog::Rackspace::ServiceCatalog | users', ['rackspace']) do
|
|||
@service_catalog = Fog::Rackspace::Identity::ServiceCatalog.from_response(nil, before_hash)
|
||||
returns(after_hash) { @service_catalog.catalog }
|
||||
end
|
||||
end
|
||||
|
||||
tests('services') do
|
||||
services = ["cloudBlockStorage", "cloudDNS", "cloudDatabases", "cloudFiles", "cloudFilesCDN", "cloudLoadBalancers", "cloudMonitoring", "cloudServers", "cloudServersOpenStack"]
|
||||
|
|
Loading…
Reference in a new issue