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 )
2013-10-25 10:29:28 -04:00
@catalog = attributes . delete ( :catalog ) || { }
2013-02-19 15:43:06 -05:00
end
def services
2013-10-25 10:29:28 -04:00
catalog . collect { | s | s [ " name " ] }
2013-02-19 15:43:06 -05:00
end
def get_endpoints ( service_type )
2013-10-25 10:29:28 -04:00
h = catalog . find { | service | service [ " name " ] == service_type . to_s }
h ? h [ " endpoints " ] : { }
2013-02-20 16:48:19 -05:00
end
def display_service_regions ( service_type )
endpoints = get_endpoints ( service_type )
2013-10-25 10:29:28 -04:00
regions = endpoints . collect do | e |
e [ " region " ] ? " : #{ e [ " region " ] . downcase } " : " :global "
end
regions . join ( " , " )
2013-02-19 15:43:06 -05:00
end
2013-10-25 10:29:28 -04:00
def get_endpoint ( service_type , region = nil , service_net = nil )
service_region = region_key ( region )
network_type = service_net ? " internalURL " : " publicURL "
2013-02-19 15:43:06 -05:00
2013-10-25 10:29:28 -04:00
endpoints = get_endpoints ( service_type )
raise " Unable to locate endpoint for service #{ service_type } " if endpoints . empty?
2013-02-19 15:43:06 -05:00
2013-10-25 10:29:28 -04:00
if endpoints . size > 1 && region . nil?
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
2013-10-25 10:29:28 -04:00
# 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 ) } "
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 )
2013-10-25 10:29:28 -04:00
ServiceCatalog . new :service = > service , :catalog = > hash [ " access " ] [ " serviceCatalog " ]
2013-03-01 17:47:22 -05:00
end
private
2013-02-19 15:43:06 -05:00
2013-10-25 10:29:28 -04:00
def matching_region? ( h , region )
region_key ( h [ " region " ] ) == region
2013-02-19 15:43:06 -05:00
end
2013-10-25 10:29:28 -04:00
def region_key ( region )
( region . nil? || region . empty? ) ? " GLOBAL " : region . to_s . upcase
2013-02-19 15:43:06 -05:00
end
end
end
end
2013-08-07 10:16:30 -04:00
end