File: catalog.rb

package info (click to toggle)
ruby-fog-openstack 1.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,784 kB
  • sloc: ruby: 47,937; makefile: 5; sh: 4
file content (64 lines) | stat: -rw-r--r-- 1,878 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module Fog
  module OpenStack
    module Auth
      module Catalog
        attr_reader :payload

        class CatalogError < RuntimeError; end
        class EndpointError < RuntimeError; end
        class ServiceTypeError < RuntimeError; end

        def initialize(payload)
          @payload = payload
        end

        def get_endpoint_url(names, interfaces, region = nil)
          # TODO: Inject OpenStack Service Types Authority
          names_list = if names.kind_of?(String)
                         [names]
                       else
                         names
                       end
          entries = get_by_type(names_list)
          raise ServiceTypeError, 'No endpoint match' if entries.empty?

          interfaces_list = if interfaces.kind_of?(String)
                              [interfaces]
                            else
                              interfaces
                            end

          list = []
          interfaces_list.each do |interface|
            val = get_endpoint(entries, interface, region)
            list << val if val
          end

          raise EndpointError, 'No endpoint found' if list.empty?
          list[0]
        end

        private

        def get_by_type(names)
          raise CatalogError, 'Empty content' unless @payload
          @payload.select do |e|
            names.include?(e['type'])
          end
        end

        def get_endpoint(entries, interface, region)
          list = []
          entries.each do |type|
            next unless type.key?('endpoints')
            type['endpoints'].each do |endpoint|
              list << endpoint_url(endpoint, interface) if endpoint_match?(endpoint, interface, region)
            end
          end
          raise EndpointError, 'Multiple endpoints found' if list.size > 1
          list[0]
        end
      end
    end
  end
end