1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/rackspace/models/identity/service_catalog.rb

79 lines
2.5 KiB
Ruby
Raw Normal View History

require 'fog/core/model'
module Fog
module Rackspace
class Identity
class ServiceCatalog < Fog::Model
attr_reader :catalog
def initialize(attributes)
@service = attributes.delete(:service)
@catalog = attributes.delete(:catalog) || {}
end
def services
catalog.collect {|s| s["name"]}
end
def get_endpoints(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)
regions = endpoints.collect do |e|
e["region"] ? ":#{e["region"].downcase}" : ":global"
end
regions.join(", ")
end
def get_endpoint(service_type, region=nil, service_net=nil)
service_region = region_key(region)
network_type = service_net ? "internalURL" : "publicURL"
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
# 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
@service.authenticate
@catalog = @service.service_catalog.catalog
self
end
def self.from_response(service, hash)
ServiceCatalog.new :service => service, :catalog => hash["access"]["serviceCatalog"]
end
private
def matching_region?(h, region)
region_key(h["region"]) == region
end
def region_key(region)
(region.nil? || region.empty?) ? "GLOBAL" : region.to_s.upcase
end
end
end
end
end