2013-02-19 15:43:06 -05:00
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 = { }
end
def services
catalog . keys
end
def get_endpoints ( service_type )
service_type = service_type . is_a? ( String ) ? service_type . to_sym : service_type
2013-02-20 16:48:19 -05:00
catalog [ service_type ]
end
def display_service_regions ( service_type )
endpoints = get_endpoints ( service_type )
2013-08-07 15:23:01 -04:00
endpoints . collect { | k , v | " : #{ k } " } . join ( " , " )
2013-02-19 15:43:06 -05:00
end
def get_endpoint ( service_type , region = nil )
endpoint = get_endpoints ( service_type )
raise " Unable to locate endpoint for service #{ service_type } " unless endpoint
2013-02-20 16:48:19 -05:00
return endpoint if endpoint . is_a? ( String ) #There is only one endpoint for service
2013-02-19 15:43:06 -05:00
unless region
2013-08-07 15:23:01 -04:00
raise " There are multiple endpoints available for #{ service_type } . Please specify one of the following regions: #{ display_service_regions ( service_type ) } . "
2013-02-19 15:43:06 -05:00
end
region = region . is_a? ( String ) ? region . to_sym : region
2013-02-20 16:48:19 -05:00
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
2013-02-19 15:43:06 -05:00
end
def reload
@service . authenticate
2013-02-21 14:07:29 -05:00
@catalog = @service . service_catalog . catalog
self
2013-02-19 15:43:06 -05:00
end
def self . from_response ( service , hash )
service_catalog = ServiceCatalog . new :service = > service
2013-08-07 15:23:01 -04:00
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 "
2013-02-19 15:43:06 -05:00
end
service_catalog
2013-03-01 17:47:22 -05:00
end
private
2013-02-19 15:43:06 -05:00
def add_endpoints ( service_name , hash )
2013-08-07 15:23:01 -04:00
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. "
2013-02-19 15:43:06 -05:00
end
end
2013-08-07 15:23:01 -04:00
def endpoints_from_array ( service_name , endpoints )
2013-02-19 15:43:06 -05:00
hash = { }
endpoints . each do | endpoint |
2013-08-07 15:23:01 -04:00
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
2013-02-19 15:43:06 -05:00
end
hash
end
end
end
end
2013-08-07 10:16:30 -04:00
end