Class: Puppet_X::EnterpriseModules::Oci::NameResolver

Inherits:
Object
  • Object
show all
Includes:
Config, Settings
Defined in:
lib/puppet_x/enterprisemodules/oci/name_resolver.rb

Overview

Docs

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Config

#client_for, #config_for_settings, #default_tenant, #determine_setting_for, #proxy_config, #retry_config, #settings_for, #tenant_config

Methods included from Settings

#configuration, included, #read_from_yaml, #setting_for, #settings, #settings_file

Constructor Details

#initializeNameResolver

Returns a new instance of NameResolver.



15
16
17
18
19
20
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 15

def initialize
  @clients      = {}
  @cache        = {}
  @cached_types = {}
  @tenant_ids   = {}
end

Class Method Details

.instance(tenant) ⇒ Object



37
38
39
40
41
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 37

def self.instance(tenant)
  @instance ||= new
  @instance.initialize_for_tenant(tenant)
  @instance
end

Instance Method Details

#add_to_cache(tenant, objects) ⇒ Object

Add objects to the cache.



75
76
77
78
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 75

def add_to_cache(tenant, objects)
  @cache[tenant] += Array(objects)
  @cache[tenant].uniq(&:id) # remove duplicate from cache
end

#compartment_for_ocid(tenant, ocid) ⇒ Object



47
48
49
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 47

def compartment_for_ocid(tenant, ocid)
  compartments(tenant).find { |e| e.id == ocid }
end

#compartment_ocid_to_name(tenant, ocid) ⇒ Object



158
159
160
161
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 158

def compartment_ocid_to_name(tenant, ocid)
  compartment = @cache[tenant].find { |e| e.id == ocid }
  compartment.name
end

#compartment_parent(tenant, ocid) ⇒ Object



163
164
165
166
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 163

def compartment_parent(tenant, ocid)
  compartment = @cache[tenant].find { |e| e.id == ocid }
  compartment.compartment_id
end

#compartments(tenant) ⇒ Object



43
44
45
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 43

def compartments(tenant)
  @cache[tenant].select { |o| o.compartment? && o.lifecycle_state == 'ACTIVE' }
end

#find_in_cache(tenant, id_type, name, compartment_id = nil) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 140

def find_in_cache(tenant, id_type, name, compartment_id = nil)
  if compartment_id && compartment_id == @tenant_ids[tenant]
    @cache[tenant].find { |o| o.id_type == id_type && o.puppet_name == name && (o.compartment_id == compartment_id || o.compartment_id.nil?) }
  else
    @cache[tenant].find do |object|
      if compartment_id && object.respond_to?(:compartment_id)
        object.id_type == id_type && object.puppet_name == name && object.compartment_id == compartment_id
      else
        #
        # TODO: Check if there is a more acurate way to select an object_id on an object that
        # doesn't respond to compartment_id
        #
        object.id_type == id_type && object.puppet_name == name
      end
    end
  end
end

#from_cache(tenant, ocid) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 80

def from_cache(tenant, ocid)
  ocid_type = id_type(ocid)
  object = @cache[tenant].find { |e| e.id == ocid && e.id_type == ocid_type }
  return object if object

  unless @cached_types[tenant].include?(ocid_type)
    #
    # Fetch all objects of specified type
    #
    object_class = ServiceInfo.id_to_class(ocid_type)
    lister = ResourceLister.new(tenant, object_class)
    add_to_cache(tenant, lister.resource_list.select(&:present?))
    mark_as_cached(tenant, ocid_type)
    object = @cache[tenant].find { |e| e.id == ocid && e.id_type == ocid_type }
  end
  fail "Object with #{ocid} not found." if object.nil?

  object
end

#id_type(ocid) ⇒ Object



51
52
53
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 51

def id_type(ocid)
  ocid.scan(/ocid1\.(\w*)\./).first.first.to_sym
end

#initialize_for_tenant(tenant) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 22

def initialize_for_tenant(tenant)
  return if @clients[tenant]

  @clients[tenant] = client_for(OCI::Identity::IdentityClient, tenant)
  #
  # If we are using an instance principal then use compartment_id as tenant
  #
  @tenant_ids[tenant] = @clients[tenant].api_client.config.tenancy || Facter.value(:oci_instance)['compartment_id']
  #
  # We now use a large limit, be we need to modify it to use multiple calls.
  #
  @cache[tenant] = @clients[tenant].list_compartments(@tenant_ids[tenant], :limit => 9_999_999, :access_level => 'ACCESSIBLE', :compartment_id_in_subtree => true).data
  @cached_types[tenant] = []
end

#invalidate(tenant, ocid_type) ⇒ Object

Allow refresh of resources for the specified ocid_type. This means a new search will re-fetch all resource names into the cache



60
61
62
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 60

def invalidate(tenant, ocid_type)
  @cached_types[tenant].delete(ocid_type)
end

#mark_as_cached(tenant, ocid_type) ⇒ Object

Mark the specified type in the specified tenant as available in the cache. New searches will only be resolved from the cache.



68
69
70
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 68

def mark_as_cached(tenant, ocid_type)
  @cached_types[tenant] << ocid_type # Mark the ocid type as cached
end

#name_to_ocid(tenant, full_name, id_type = :compartment) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 100

def name_to_ocid(tenant, full_name, id_type = :compartment)
  return full_name.collect { |n| name_to_ocid(tenant, n, id_type) } if full_name.is_a?(Array)

  #
  # For creation of a tag namespace or a tag, you can specify a compartment, but
  # when referencing it, you don't.
  #
  if [:tagnamespace, :tagdefinition].include?(id_type)
    name = full_name
  else
    full_name = full_name.gsub('//', '/') # Handle double seperators
    tenancy, relative_name = full_name.scan(%r{^(?:(.*) \(root\)/)?(.*)?}).first
    return relative_name if valid_ocid?(relative_name)
    fail "tenancy #{tenancy} different than tenancy in name #{full_name}" if tenancy && tenant != tenancy

    compartment_name, name = relative_name.scan(%r{^(?:(.*)/)?(.*)$}).first
    compartment_id = if compartment_name.nil?
                       @tenant_ids[tenant]
                     else
                       name_to_ocid(tenant, compartment_name, :compartment)
                     end
    return compartment_id if name.empty?
  end

  object = find_in_cache(tenant, id_type, name, compartment_id)
  if object.nil? # Not in cache, fetch it
    #
    # Fetch all objects of specified type
    #
    object_class = ServiceInfo.id_to_class(id_type)
    lister = ResourceLister.new(tenant, object_class)
    @cache[tenant] += lister.resource_list(compartment_id).select(&:present?)
    @cache[tenant].uniq(&:id) # remove duplicate from cache
    object = find_in_cache(tenant, id_type, name, compartment_id)
  end
  fail "No #{id_type} found with name #{full_name}" if object.nil?

  object.id
end

#ocid_to_full_name(tenant, ocid) ⇒ Object



168
169
170
171
172
173
174
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 168

def ocid_to_full_name(tenant, ocid)
  if ocid.nil? || tenant?(ocid)
    tenant_string(tenant)
  else
    "#{tenant_string(tenant)}/#{ocid_to_name(tenant, ocid)}"
  end
end

#ocid_to_name(tenant, ocid) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 176

def ocid_to_name(tenant, ocid)
  return ocid.collect { |i| ocid_to_name(tenant, i) } if ocid.is_a?(Array)
  return '/' if ocid.nil? || tenant?(ocid)

  object = from_cache(tenant, ocid)
  #
  # Because some objects from OCI don't have a name compartment, we check
  # first if the method exists.
  #
  compartment_id = object.respond_to?(:compartment_id) ? object.compartment_id : nil
  #
  # The name is prefarably fetched by the name attribute. If it doesn't exist,
  # we use the display_name.
  #
  name = object.respond_to?(:name) ? object.name : object.display_name
  #
  # Some resources originate a compartment, but don't actually reside in there
  # For those resources we return the base name
  #
  return name if compartment_id.nil? || ocid =~ /tagnamespace|tagdefinition/

  names = [name]
  loop do
    break if tenant?(compartment_id)

    names.unshift(compartment_ocid_to_name(tenant, compartment_id))
    compartment_id = compartment_parent(tenant, compartment_id)
  end
  names.join('/')
end

#tenant?(ocid) ⇒ Boolean

Returns:

  • (Boolean)


207
208
209
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 207

def tenant?(ocid)
  ocid.scan(/ocid1\.(\w*)\./).first.first == 'tenancy'
end

#tenant_string(tenant) ⇒ Object



215
216
217
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 215

def tenant_string(tenant)
  "#{tenant} (root)"
end

#valid_ocid?(ocid) ⇒ Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 211

def valid_ocid?(ocid)
  ocid.scan(/ocid1\..*/).any?
end