Class: Puppet_X::EnterpriseModules::Oci::NameResolver
- Inherits:
-
Object
- Object
- Puppet_X::EnterpriseModules::Oci::NameResolver
show all
- Includes:
- Config, Settings
- Defined in:
- lib/puppet_x/enterprisemodules/oci/name_resolver.rb
Overview
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_to_cache(tenant, objects, ocid_type) ⇒ Object
Add objects to the cache.
-
#compartment_for_ocid(tenant, ocid) ⇒ Object
-
#compartment_ocid_to_name(tenant, ocid) ⇒ Object
-
#compartment_parent(tenant, ocid) ⇒ Object
-
#compartments(tenant) ⇒ Object
-
#find_in_cache(tenant, id_type, name, compartment_id = nil) ⇒ Object
-
#from_cache(tenant, ocid) ⇒ Object
-
#id_type(ocid) ⇒ Object
-
#initialize ⇒ NameResolver
constructor
A new instance of NameResolver.
-
#initialize_for_tenant(tenant) ⇒ Object
-
#invalidate(tenant, ocid_type) ⇒ Object
Allow refresh of resources for the specified ocid_type.
-
#name_to_ocid(tenant, full_name, id_type = :compartment) ⇒ Object
-
#ocid_to_full_name(tenant, ocid) ⇒ Object
-
#ocid_to_name(tenant, ocid) ⇒ Object
-
#tenant?(ocid) ⇒ Boolean
-
#tenant_string(tenant) ⇒ Object
-
#valid_ocid?(ocid) ⇒ Boolean
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
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, ocid_type) ⇒ Object
65
66
67
68
69
|
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 65
def add_to_cache(tenant, objects, ocid_type)
@cache[tenant] += Array(objects)
@cache[tenant].uniq(&:id) @cached_types[tenant] << ocid_type
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
148
149
150
151
|
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 148
def compartment_ocid_to_name(tenant, ocid)
compartment = @cache[tenant].find { |e| e.id == ocid }
compartment.name
end
|
#compartment_parent(tenant, ocid) ⇒ Object
153
154
155
156
|
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 153
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 130
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
object.id_type == id_type && object.puppet_name == name
end
end
end
end
|
#from_cache(tenant, ocid) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 71
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)
object_class = ServiceInfo.id_to_class(ocid_type)
lister = ResourceLister.new(tenant, object_class)
add_to_cache(tenant, lister.resource_list.select(&:present?), 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)
@tenant_ids[tenant] = @clients[tenant].api_client.config.tenancy || Facter.value(:oci_instance)['compartment_id']
@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
58
59
60
|
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 58
def invalidate(tenant, ocid_type)
@cached_types[tenant].delete(ocid_type)
end
|
#name_to_ocid(tenant, full_name, id_type = :compartment) ⇒ Object
90
91
92
93
94
95
96
97
98
99
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
|
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 90
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)
if [:tagnamespace, :tagdefinition].include?(id_type)
name = full_name
else
full_name = full_name.gsub('//', '/') 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? 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) 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
158
159
160
161
162
163
164
|
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 158
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 166
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)
compartment_id = object.respond_to?(:compartment_id) ? object.compartment_id : nil
name = object.respond_to?(:name) ? object.name : object.display_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
197
198
199
|
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 197
def tenant?(ocid)
ocid.scan(/ocid1\.(\w*)\./).first.first == 'tenancy'
end
|
#tenant_string(tenant) ⇒ Object
205
206
207
|
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 205
def tenant_string(tenant)
"#{tenant} (root)"
end
|
#valid_ocid?(ocid) ⇒ Boolean
201
202
203
|
# File 'lib/puppet_x/enterprisemodules/oci/name_resolver.rb', line 201
def valid_ocid?(ocid)
ocid.scan(/ocid1\..*/).any?
end
|