1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[rackspace|identity] updated service catalog to return global endpoint if specified region endpoint does not exist

This commit is contained in:
Kyle Rames 2013-10-28 14:04:34 -05:00
parent ff678b48ba
commit 4ea81036c4
3 changed files with 64 additions and 30 deletions

View file

@ -15,33 +15,32 @@ module Fog
catalog.collect {|s| s["name"]}
end
def get_endpoints(service_type, service_net=false)
h = catalog.find {|service| service["name"] == service_type.to_s}
def get_endpoints(service_name, service_net=false)
h = catalog.find {|service| service["name"] == service_name.to_s}
return {} unless h
key = network_type_key(service_net)
h["endpoints"].select {|e| e[key]}
end
def display_service_regions(service_type, service_net=false)
endpoints = get_endpoints(service_type, service_net)
def display_service_regions(service_name, service_net=false)
endpoints = get_endpoints(service_name, service_net)
regions = endpoints.collect do |e|
e["region"] ? ":#{e["region"].downcase}" : ":global"
end
regions.join(", ")
end
def get_endpoint(service_type, region=nil, service_net=false)
def get_endpoint(service_name, region=nil, service_net=false)
service_region = region_key(region)
network_type = network_type_key(service_net)
endpoints = get_endpoints(service_type, service_net)
raise "Unable to locate endpoint for service #{service_type}" if endpoints.empty?
endpoints = get_endpoints(service_name, service_net)
raise "Unable to locate endpoint for service #{service_name}" 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)}."
raise "There are multiple endpoints available for #{service_name}. Please specify one of the following regions: #{display_service_regions(service_name)}."
end
# select multiple endpoints
@ -53,7 +52,7 @@ module Fog
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)}"
raise "Unknown region :#{region} for service #{service_name}. Please use one of the following regions: #{display_service_regions(service_name)}"
end
def reload

View file

@ -81,11 +81,6 @@ Shindo.tests('Rackspace | Compute', ['rackspace']) do
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true, "uses custom endpoint") { (@service.instance_variable_get("@uri").host =~ /my-custom-endpoint\.com/) != nil }
end
tests('rackspace_servicenet') do
@service = Fog::Compute::Rackspace.new :rackspace_servicenet => true
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true, "uses custom endpoint") { (@service.instance_variable_get("@uri").host =~ /snet-/) != nil }
end
end
tests('reauthentication') do

View file

@ -61,24 +61,39 @@ Shindo.tests('Fog::Rackspace::ServiceCatalog | users', ['rackspace']) do
end
tests('with one endpoint') do
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)
tests('catalog contains global endpoint') do
catalog_hash = [{"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 => catalog_hash)
tests('no region specifed').returns("https://blockstorage.api.rackspacecloud.com/v1/777") do
@service_catalog.get_endpoint(:cloudBlockStorage)
end
tests('region specifed').returns("https://blockstorage.api.rackspacecloud.com/v1/777") do
@service_catalog.get_endpoint(:cloudBlockStorage, :ord)
end
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) }
tests('catalog does not contain global endpoint') do
catalog_hash = [{"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 => catalog_hash)
tests('non-existing region') do
raises(RuntimeError) { @service_catalog.get_endpoint(:cloudBlockStorage, :dfw) }
end
tests('existing region').returns("https://ord.blockstorage.api.rackspacecloud.com/v1/777") do
@service_catalog.get_endpoint(:cloudBlockStorage, :ord)
end
end
end
tests('endpoint type') do
catalog_hash = [{"type"=>"object-store", "endpoints"=>[{"internalURL"=>"https://snet-storage101.dfw1.clouddrive.com/v1/Mosso777", "region"=>"DFW",
"tenantId"=>"Mosso777",
"publicURL"=>"https://storage101.dfw1.clouddrive.com/v1/Mosso777"},
{"internalURL"=>"https://snet-storage101.ord1.clouddrive.com/v1/Mosso777", "region"=>"ORD",
"tenantId"=>"Mosso777",
"publicURL"=>"https://storage101.ord1.clouddrive.com/v1/Mosso777"}], "name"=>"cloudFiles"}]
@service_catalog = Fog::Rackspace::Identity::ServiceCatalog.new(:service => nil, :catalog => catalog_hash)
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) }
@ -108,7 +123,32 @@ Shindo.tests('Fog::Rackspace::ServiceCatalog | users', ['rackspace']) do
end
tests('display_service_regions') do
returns(":dfw, :ord, :global") { @service_catalog.display_service_regions(:cloudServersOpenStack) }
returns(":dfw, :ord") { @service_catalog.display_service_regions(:cloudFiles, true) }
tests('with global endpoint').returns(":dfw, :ord, :global") do
catalog_hash = [{"type"=>"compute", "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"}], "name"=>"cloudServersOpenStack"}]
@service_catalog = Fog::Rackspace::Identity::ServiceCatalog.new(:service => nil, :catalog => catalog_hash)
@service_catalog.display_service_regions(:cloudServersOpenStack)
end
tests('endpoint types') do
catalog_hash = [{"type"=>"object-store", "endpoints"=>[{"internalURL"=>"https://snet-storage101.dfw1.clouddrive.com/v1/Mosso777", "region"=>"DFW",
"tenantId"=>"Mosso777",
"publicURL"=>"https://storage101.dfw1.clouddrive.com/v1/Mosso777"},
{ "region"=>"ORD",
"tenantId"=>"Mosso777",
"publicURL"=>"https://storage101.ord1.clouddrive.com/v1/Mosso777"}], "name"=>"cloudFiles"},]
@service_catalog = Fog::Rackspace::Identity::ServiceCatalog.new(:service => nil, :catalog => catalog_hash)
tests('public').returns(":dfw, :ord") do
@service_catalog.display_service_regions(:cloudFiles)
end
tests('private').returns(":dfw") do
@service_catalog.display_service_regions(:cloudFiles, true)
end
end
end
end