Class: Openstack::Client::ServiceCatalog

Inherits:
Object
  • Object
show all
Defined in:
lib/openstack-client/service_catalog.rb

Overview

Helper class for dealing with a Keystone Service Catalog.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_dict) ⇒ ServiceCatalog

Returns a new instance of ServiceCatalog.



12
13
14
# File 'lib/openstack-client/service_catalog.rb', line 12

def initialize resource_dict
  self.catalog = resource_dict
end

Instance Attribute Details

#catalogObject

Returns the value of attribute catalog.



10
11
12
# File 'lib/openstack-client/service_catalog.rb', line 10

def catalog
  @catalog
end

Instance Method Details

#endpoints(options = {}) ⇒ Object

Fetch and filter endpoints for the specified service(s)

Returns endpoints for the specified service (or all) and that contain the specified type (or all).



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/openstack-client/service_catalog.rb', line 65

def endpoints options={}
  service_type  = options[:service_type]
  endpoint_type = options[:endpoint_type]

  return {}.tap do |rtn|
    catalog = self.catalog['serviceCatalog'] || []

    catalog.each do |service|
      next if service_type and service_type != service['type']

      rtn[service['type']] = []
      service['endpoints'].each do |endpoint|
        next if endpoint_type and endpoint.include?(endpoint_type)
        rtn[service['type']] << endpoint
      end
    end
  end
end

#tokenHash

Fetch token details fron service catalog

Returns:

  • (Hash)

    token



18
19
20
21
22
23
24
25
26
27
# File 'lib/openstack-client/service_catalog.rb', line 18

def token
  unless defined?(@token)
    @token = {
      'id' => self.catalog['token']['id'],
      'expires' => Time.parse(self.catalog['token']['expires'])
    }
    @token['tenant'] = self.catalog['token']['tenant']['id'] if self.catalog['token']['tenant']
  end
  @token
end

#url_for(options = {}) ⇒ Object

Fetch an endpoint from the service catalog.

Fetch the specified endpoint from the service catalog for a particular endpoint attribute. If no attribute is given, return the first endpoint of the specified type.

See tests for a sample service catalog.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :service_type (String)

    service type for url. (Default ‘identity’)

  • :endpoint_type (String)

    endpoint_type for url. (Default ‘publicURL’)

  • :attribute (String)

    attribute for filter.

  • :filter_value (String)

    filter_value for url.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/openstack-client/service_catalog.rb', line 42

def url_for options={}
  service_type  = options[:service_type] || 'identity'
  endpoint_type = options[:endpoint_type] || 'publicURL'
  attribute     = options[:attribute]
  filter_value  = options[:filter_value]

  catalog = self.catalog['serviceCatalog'] || []

  catalog.each do |service|
    next unless service['type'] == service_type

    service['endpoints'].each do |endpoint|
      return endpoint[endpoint_type] if filter_value.nil? || endpoint[attribute] == filter_value
    end
  end

  raise 'Endpoint not found.'
end