mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[rackspace|identity] re-implementing service catalog in hopes of providing a faster more flexible solution
This commit is contained in:
parent
7ca8017d64
commit
49debe9146
2 changed files with 78 additions and 94 deletions
|
@ -8,36 +8,48 @@ module Fog
|
|||
|
||||
def initialize(attributes)
|
||||
@service = attributes.delete(:service)
|
||||
@catalog = {}
|
||||
@catalog = attributes.delete(:catalog) || {}
|
||||
end
|
||||
|
||||
def services
|
||||
catalog.keys
|
||||
catalog.collect {|s| s["name"]}
|
||||
end
|
||||
|
||||
def get_endpoints(service_type)
|
||||
service_type = service_type.is_a?(String) ? service_type.to_sym : service_type
|
||||
catalog[service_type]
|
||||
h = catalog.find {|service| service["name"] == service_type.to_s}
|
||||
h ? h["endpoints"] : {}
|
||||
end
|
||||
|
||||
def display_service_regions(service_type)
|
||||
endpoints = get_endpoints(service_type)
|
||||
endpoints.collect { |k,v| ":#{k}" }.join(", ")
|
||||
regions = endpoints.collect do |e|
|
||||
e["region"] ? ":#{e["region"].downcase}" : ":global"
|
||||
end
|
||||
regions.join(", ")
|
||||
end
|
||||
|
||||
def get_endpoint(service_type, region=nil)
|
||||
endpoint = get_endpoints(service_type)
|
||||
raise "Unable to locate endpoint for service #{service_type}" unless endpoint
|
||||
def get_endpoint(service_type, region=nil, service_net=nil)
|
||||
service_region = region_key(region)
|
||||
|
||||
return endpoint if endpoint.is_a?(String) #There is only one endpoint for service
|
||||
network_type = service_net ? "internalURL" : "publicURL"
|
||||
|
||||
unless region
|
||||
endpoints = get_endpoints(service_type)
|
||||
raise "Unable to locate endpoint for service #{service_type}" if endpoints.empty?
|
||||
|
||||
if endpoints.size > 1 && region.nil?
|
||||
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]
|
||||
raise "Unknown region :#{region} for service #{service_type}. Please use one of the following regions: #{display_service_regions(service_type)}" unless endpoint
|
||||
endpoint
|
||||
|
||||
# select multiple endpoints
|
||||
endpoint = endpoints.find {|e| matching_region?(e, service_region) }
|
||||
return endpoint[network_type] if endpoint && endpoint[network_type]
|
||||
|
||||
# endpoint doesnt have region
|
||||
if endpoints.size == 1 && matching_region?(endpoints[0], "GLOBAL")
|
||||
return endpoints[0][network_type]
|
||||
end
|
||||
|
||||
raise "Unknown region :#{region} for service #{service_type}. Please use one of the following regions: #{display_service_regions(service_type)}"
|
||||
end
|
||||
|
||||
def reload
|
||||
|
@ -47,52 +59,17 @@ module Fog
|
|||
end
|
||||
|
||||
def self.from_response(service, hash)
|
||||
service_catalog = ServiceCatalog.new :service => service
|
||||
begin
|
||||
services = hash["access"]["serviceCatalog"]
|
||||
services.each do |serv|
|
||||
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
|
||||
ServiceCatalog.new :service => service, :catalog => hash["access"]["serviceCatalog"]
|
||||
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(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
|
||||
def matching_region?(h, region)
|
||||
region_key(h["region"]) == region
|
||||
end
|
||||
|
||||
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
|
||||
def region_key(region)
|
||||
(region.nil? || region.empty?) ? "GLOBAL" : region.to_s.upcase
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -2,30 +2,6 @@ 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", :global=>"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"},
|
||||
|
@ -46,47 +22,75 @@ Shindo.tests('Fog::Rackspace::ServiceCatalog | users', ['rackspace']) do
|
|||
"tenantId"=>"777", "versionList"=>"https://dfw.servers.api.rackspacecloud.com/", "versionInfo"=>"https://dfw.servers.api.rackspacecloud.com/v2",
|
||||
"publicURL"=>"https://dfw.servers.api.rackspacecloud.com/v2/777"}, {"region"=>"ORD", "versionId"=>"2", "tenantId"=>"777",
|
||||
"versionList"=>"https://ord.servers.api.rackspacecloud.com/", "versionInfo"=>"https://ord.servers.api.rackspacecloud.com/v2",
|
||||
"publicURL"=>"https://ord.servers.api.rackspacecloud.com/v2/777"}], "name"=>"cloudServersOpenStack"}, {"type"=>"rax:monitor", "endpoints"=>[{"tenantId"=>"777",
|
||||
"publicURL"=>"https://ord.servers.api.rackspacecloud.com/v2/777"}, {"versionId"=>"2", "tenantId"=>"777", "versionList"=>"https://servers.api.rackspacecloud.com/", "versionInfo"=>"https://servers.api.rackspacecloud.com/v2", "publicURL"=>"https://servers.api.rackspacecloud.com/v2/777"}], "name"=>"cloudServersOpenStack"}, {"type"=>"rax:monitor", "endpoints"=>[{"tenantId"=>"777",
|
||||
"publicURL"=>"https://monitoring.api.rackspacecloud.com/v1.0/777"}], "name"=>"cloudMonitoring"}, {"type"=>"rax:object-cdn", "endpoints"=>[{"region"=>"DFW",
|
||||
"tenantId"=>"Mosso777", "publicURL"=>"https://cdn1.clouddrive.com/v1/Mosso777"},
|
||||
{"region"=>"ORD", "tenantId"=>"Mosso777",
|
||||
"publicURL"=>"https://cdn2.clouddrive.com/v1/Mosso777"}], "name"=>"cloudFilesCDN"}], "user"=>{"roles"=>[{"description"=>"User Admin
|
||||
"publicURL"=>"https://cdn2.clouddrive.com/v1/Mosso777"}], "name"=>"cloudFilesCDN"},
|
||||
|
||||
{"type"=>"not_here", "name" => "not_here", "endpoints"=>[{"tenantId"=>"777", "region" => "LON",
|
||||
"publicURL"=>"https://monitoring.api.rackspacecloud.com/v1.0/777"}]}
|
||||
],
|
||||
|
||||
"user"=>{"roles"=>[{"description"=>"User Admin
|
||||
Role.", "name"=>"identity:user-admin", "id"=>"3"}], "name"=>"joe-racker", "RAX-AUTH:defaultRegion"=>"", "id"=>"TK421"}}}
|
||||
|
||||
after_hash = {:cloudServers=>"https://servers.api.rackspacecloud.com/v1.0/777", :cloudServersOpenStack=>{:dfw=>"https://dfw.servers.api.rackspacecloud.com/v2/777", :ord=>"https://ord.servers.api.rackspacecloud.com/v2/777"}, :cloudFiles=>{:dfw=>"https://storage101.dfw1.clouddrive.com/v1/Mosso777", :ord=>"https://storage101.ord1.clouddrive.com/v1/Mosso777"}, :cloudBlockStorage=>{:dfw=>"https://dfw.blockstorage.api.rackspacecloud.com/v1/777", :ord=>"https://ord.blockstorage.api.rackspacecloud.com/v1/777"}, :cloudMonitoring=>"https://monitoring.api.rackspacecloud.com/v1.0/777", :cloudLoadBalancers=>{:dfw=>"https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/777", :ord=>"https://ord.loadbalancers.api.rackspacecloud.com/v1.0/777"}, :cloudFilesCDN=>{:dfw=>"https://cdn1.clouddrive.com/v1/Mosso777", :ord=>"https://cdn2.clouddrive.com/v1/Mosso777"}, :cloudDatabases=>{:dfw=>"https://dfw.databases.api.rackspacecloud.com/v1.0/777", :ord=>"https://ord.databases.api.rackspacecloud.com/v1.0/777"}, :cloudDNS=>"https://dns.api.rackspacecloud.com/v1.0/777"}
|
||||
@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"]
|
||||
returns(services) { @service_catalog.services.collect {|s| s.to_s }.sort }
|
||||
end
|
||||
tests('services') do
|
||||
services = ["cloudBlockStorage", "cloudDNS", "cloudDatabases", "cloudFiles", "cloudFilesCDN", "cloudLoadBalancers", "cloudMonitoring", "cloudServers", "cloudServersOpenStack", "not_here"]
|
||||
|
||||
returns(services) { @service_catalog.services.collect {|s| s.to_s }.sort }
|
||||
end
|
||||
|
||||
tests('get_endpoints') do
|
||||
endpoints = {:dfw=>"https://dfw.servers.api.rackspacecloud.com/v2/777", :ord=>"https://ord.servers.api.rackspacecloud.com/v2/777"}
|
||||
endpoints = [{"region"=>"DFW", "versionId"=>"2", "tenantId"=>"777", "versionList"=>"https://dfw.servers.api.rackspacecloud.com/", "versionInfo"=>"https://dfw.servers.api.rackspacecloud.com/v2", "publicURL"=>"https://dfw.servers.api.rackspacecloud.com/v2/777"}, {"region"=>"ORD", "versionId"=>"2", "tenantId"=>"777", "versionList"=>"https://ord.servers.api.rackspacecloud.com/", "versionInfo"=>"https://ord.servers.api.rackspacecloud.com/v2", "publicURL"=>"https://ord.servers.api.rackspacecloud.com/v2/777"}, {"versionId"=>"2", "tenantId"=>"777", "versionList"=>"https://servers.api.rackspacecloud.com/", "versionInfo"=>"https://servers.api.rackspacecloud.com/v2", "publicURL"=>"https://servers.api.rackspacecloud.com/v2/777"}]
|
||||
returns(endpoints) { @service_catalog.get_endpoints(:cloudServersOpenStack) }
|
||||
returns(endpoints) { @service_catalog.get_endpoints('cloudServersOpenStack') }
|
||||
returns(nil) { @service_catalog.get_endpoints('non-existent') }
|
||||
returns({}) { @service_catalog.get_endpoints('non-existent') }
|
||||
end
|
||||
|
||||
tests('get_endpoint') do
|
||||
tests('service with mulitple endpoints') do
|
||||
returns("https://dfw.servers.api.rackspacecloud.com/v2/777") { @service_catalog.get_endpoint(:cloudServersOpenStack, :dfw) }
|
||||
returns("https://ord.servers.api.rackspacecloud.com/v2/777") { @service_catalog.get_endpoint(:cloudServersOpenStack, :ord) }
|
||||
returns("https://ord.servers.api.rackspacecloud.com/v2/777") { @service_catalog.get_endpoint(:cloudServersOpenStack, :ord) }
|
||||
returns("https://dfw.servers.api.rackspacecloud.com/v2/777") { @service_catalog.get_endpoint(:cloudServersOpenStack, 'dfw') }
|
||||
returns("https://dfw.servers.api.rackspacecloud.com/v2/777") { @service_catalog.get_endpoint('cloudServersOpenStack', 'dfw') }
|
||||
returns("https://servers.api.rackspacecloud.com/v2/777") { @service_catalog.get_endpoint('cloudServersOpenStack', :global) }
|
||||
end
|
||||
|
||||
tests('with one endpoint') do
|
||||
returns("https://monitoring.api.rackspacecloud.com/v1.0/777") { @service_catalog.get_endpoint(:cloudMonitoring, 'dfw') }
|
||||
tests('no region specified').returns("https://blockstorage.api.rackspacecloud.com/v1/777") do
|
||||
list = [{"type"=>"volume", "endpoints"=>[{"tenantId"=>"777", "publicURL"=>"https://blockstorage.api.rackspacecloud.com/v1/777"}], "name"=>"cloudBlockStorage"}]
|
||||
service_catalog = Fog::Rackspace::Identity::ServiceCatalog.new(:service => nil, :catalog => list)
|
||||
service_catalog.get_endpoint(:cloudBlockStorage)
|
||||
end
|
||||
tests('specify region as :global').returns("https://blockstorage.api.rackspacecloud.com/v1/777") do
|
||||
list = [{"type"=>"volume", "endpoints"=>[{"tenantId"=>"777", "publicURL"=>"https://blockstorage.api.rackspacecloud.com/v1/777"}], "name"=>"cloudBlockStorage"}]
|
||||
service_catalog = Fog::Rackspace::Identity::ServiceCatalog.new(:service => nil, :catalog => list)
|
||||
service_catalog.get_endpoint(:cloudBlockStorage, :global)
|
||||
end
|
||||
tests('wrong region') do
|
||||
list = [{"type"=>"volume", "endpoints"=>[{"region" => "ORD", "tenantId"=>"777", "publicURL"=>"https://ord.blockstorage.api.rackspacecloud.com/v1/777"}], "name"=>"cloudBlockStorage"}]
|
||||
service_catalog = Fog::Rackspace::Identity::ServiceCatalog.new(:service => nil, :catalog => list)
|
||||
raises(RuntimeError) { service_catalog.get_endpoint(:cloudBlockStorage, :dfw) }
|
||||
end
|
||||
end
|
||||
|
||||
tests('endpoint type') do
|
||||
returns("https://storage101.ord1.clouddrive.com/v1/Mosso777") { @service_catalog.get_endpoint(:cloudFiles, :ord) }
|
||||
returns("https://snet-storage101.ord1.clouddrive.com/v1/Mosso777") { @service_catalog.get_endpoint(:cloudFiles, :ord, true) }
|
||||
returns("https://storage101.ord1.clouddrive.com/v1/Mosso777") { @service_catalog.get_endpoint(:cloudFiles, :ord, false) }
|
||||
end
|
||||
|
||||
tests('error conditions') do
|
||||
raises(RuntimeError) { @service_catalog.get_endpoint(:cloudServersOpenStack) }
|
||||
raises(RuntimeError) { @service_catalog.get_endpoint(:cloudServersOpenStack, :sat) }
|
||||
raises(RuntimeError) { @service_catalog.get_endpoint(:cloudServersOpenStack, :sat, true) }
|
||||
raises(RuntimeError) { @service_catalog.get_endpoint(:not_here, :dfw) }
|
||||
raises(RuntimeError) { @service_catalog.get_endpoint('non-existent') }
|
||||
raises(RuntimeError) { @service_catalog.get_endpoint(:cloudServersOpenStack, :ord, true) }
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -95,12 +99,15 @@ Shindo.tests('Fog::Rackspace::ServiceCatalog | users', ['rackspace']) do
|
|||
|
||||
service = Fog::Identity[:rackspace]
|
||||
service_catalog = service.service_catalog
|
||||
service_catalog.catalog[:fakeService] = "http:///fake-endpoint.com"
|
||||
service_catalog.catalog << {"name"=>"fakeService", "endpoints"=>[{ "publicURL"=>"http:///fake-endpoint.com"}]}
|
||||
returns("http:///fake-endpoint.com") { service_catalog.get_endpoint :fakeService }
|
||||
returns("http:///fake-endpoint.com") { service.service_catalog.get_endpoint :fakeService }
|
||||
service_catalog.reload
|
||||
raises(RuntimeError) { service_catalog.get_endpoint :fakeService }
|
||||
raises(RuntimeError) { service.service_catalog.get_endpoint :fakeService }
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
tests('display_service_regions') do
|
||||
returns(":dfw, :ord, :global") { @service_catalog.display_service_regions(:cloudServersOpenStack) }
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue