opennebula 5.12.13 → 5.13.80.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. checksums.yaml +5 -5
  2. data/lib/ActionManager.rb +1 -1
  3. data/lib/CommandManager.rb +1 -1
  4. data/lib/DriverExecHelper.rb +44 -28
  5. data/lib/OpenNebulaDriver.rb +8 -4
  6. data/lib/VirtualMachineDriver.rb +9 -2
  7. data/lib/cloud/CloudClient.rb +3 -3
  8. data/lib/datacenter.rb +1258 -0
  9. data/lib/datastore.rb +1025 -0
  10. data/lib/distributed_firewall.rb +280 -0
  11. data/lib/file_helper.rb +370 -0
  12. data/lib/host.rb +1517 -0
  13. data/lib/logical_port.rb +50 -0
  14. data/lib/logical_switch.rb +77 -0
  15. data/lib/memoize.rb +74 -0
  16. data/lib/models/role.rb +39 -8
  17. data/lib/models/service.rb +92 -31
  18. data/lib/models.rb +5 -5
  19. data/lib/network.rb +635 -0
  20. data/lib/nsx_client.rb +144 -0
  21. data/lib/nsx_component.rb +28 -0
  22. data/lib/nsx_constants.rb +149 -0
  23. data/lib/nsx_driver.rb +78 -0
  24. data/lib/nsx_error.rb +77 -0
  25. data/lib/nsx_rule.rb +193 -0
  26. data/lib/nsxt_client.rb +176 -0
  27. data/lib/nsxt_dfw.rb +196 -0
  28. data/lib/nsxt_logical_port.rb +94 -0
  29. data/lib/nsxt_rule.rb +188 -0
  30. data/lib/nsxt_tz.rb +38 -0
  31. data/lib/nsxv_client.rb +176 -0
  32. data/lib/nsxv_dfw.rb +202 -0
  33. data/lib/nsxv_logical_port.rb +107 -0
  34. data/lib/nsxv_rule.rb +172 -0
  35. data/lib/nsxv_tz.rb +41 -0
  36. data/lib/opaque_network.rb +134 -0
  37. data/lib/opennebula/acl.rb +1 -1
  38. data/lib/opennebula/acl_pool.rb +1 -1
  39. data/lib/opennebula/client.rb +1 -1
  40. data/lib/opennebula/cluster.rb +1 -1
  41. data/lib/opennebula/cluster_pool.rb +1 -1
  42. data/lib/opennebula/datastore.rb +1 -1
  43. data/lib/opennebula/datastore_pool.rb +1 -1
  44. data/lib/opennebula/document.rb +8 -29
  45. data/lib/opennebula/document_json.rb +42 -12
  46. data/lib/opennebula/document_pool.rb +1 -1
  47. data/lib/opennebula/document_pool_json.rb +1 -1
  48. data/lib/opennebula/error.rb +4 -1
  49. data/lib/opennebula/flow/grammar.rb +1195 -0
  50. data/lib/{models → opennebula/flow}/service_pool.rb +26 -2
  51. data/lib/{models → opennebula/flow}/service_template.rb +86 -17
  52. data/lib/opennebula/flow/service_template_ext.rb +84 -0
  53. data/lib/{models → opennebula/flow}/service_template_pool.rb +1 -1
  54. data/lib/opennebula/flow/validator.rb +499 -0
  55. data/lib/opennebula/flow.rb +23 -0
  56. data/lib/opennebula/group.rb +1 -1
  57. data/lib/opennebula/group_pool.rb +1 -1
  58. data/lib/opennebula/hook.rb +5 -12
  59. data/lib/opennebula/hook_log.rb +1 -1
  60. data/lib/opennebula/hook_pool.rb +1 -1
  61. data/lib/opennebula/host.rb +1 -1
  62. data/lib/opennebula/host_pool.rb +1 -1
  63. data/lib/opennebula/image.rb +17 -14
  64. data/lib/opennebula/image_pool.rb +1 -1
  65. data/lib/opennebula/ldap_auth.rb +1 -1
  66. data/lib/opennebula/ldap_auth_spec.rb +1 -1
  67. data/lib/opennebula/lockable_ext.rb +163 -0
  68. data/lib/opennebula/marketplace.rb +1 -1
  69. data/lib/opennebula/marketplace_pool.rb +1 -1
  70. data/lib/opennebula/marketplaceapp.rb +9 -119
  71. data/lib/opennebula/marketplaceapp_ext.rb +522 -0
  72. data/lib/opennebula/marketplaceapp_pool.rb +1 -1
  73. data/lib/opennebula/oneflow_client.rb +4 -3
  74. data/lib/opennebula/pool.rb +4 -3
  75. data/lib/opennebula/pool_element.rb +1 -1
  76. data/lib/opennebula/security_group.rb +1 -1
  77. data/lib/opennebula/security_group_pool.rb +1 -1
  78. data/lib/opennebula/server_cipher_auth.rb +1 -1
  79. data/lib/opennebula/server_x509_auth.rb +1 -1
  80. data/lib/opennebula/ssh_auth.rb +1 -1
  81. data/lib/opennebula/system.rb +1 -1
  82. data/lib/opennebula/template.rb +4 -13
  83. data/lib/opennebula/template_ext.rb +325 -0
  84. data/lib/opennebula/template_pool.rb +1 -1
  85. data/lib/opennebula/user.rb +26 -2
  86. data/lib/opennebula/user_pool.rb +1 -1
  87. data/lib/opennebula/utils.rb +1 -1
  88. data/lib/opennebula/vdc.rb +1 -1
  89. data/lib/opennebula/vdc_pool.rb +1 -1
  90. data/lib/opennebula/virtual_machine.rb +25 -207
  91. data/lib/opennebula/virtual_machine_ext.rb +469 -0
  92. data/lib/opennebula/virtual_machine_pool.rb +1 -5
  93. data/lib/opennebula/virtual_network.rb +4 -10
  94. data/lib/opennebula/virtual_network_pool.rb +1 -1
  95. data/lib/opennebula/virtual_router.rb +4 -12
  96. data/lib/opennebula/virtual_router_pool.rb +1 -1
  97. data/lib/opennebula/vm_group.rb +4 -11
  98. data/lib/opennebula/vm_group_pool.rb +1 -1
  99. data/lib/opennebula/vntemplate.rb +4 -13
  100. data/lib/opennebula/vntemplate_pool.rb +1 -1
  101. data/lib/opennebula/wait_ext.rb +222 -0
  102. data/lib/opennebula/x509_auth.rb +1 -1
  103. data/lib/opennebula/xml_element.rb +1 -1
  104. data/lib/opennebula/xml_pool.rb +1 -1
  105. data/lib/opennebula/xml_utils.rb +1 -1
  106. data/lib/opennebula/zone.rb +1 -1
  107. data/lib/opennebula/zone_pool.rb +1 -1
  108. data/lib/opennebula.rb +5 -2
  109. data/lib/rest_client.rb +201 -0
  110. data/lib/scripts_common.rb +180 -0
  111. data/lib/transport_zone.rb +43 -0
  112. data/lib/vcenter_driver.rb +9 -22
  113. data/lib/vcenter_importer.rb +616 -0
  114. data/lib/vi_client.rb +281 -0
  115. data/lib/vi_helper.rb +312 -0
  116. data/lib/virtual_machine.rb +3477 -0
  117. data/lib/virtual_wire.rb +158 -0
  118. data/lib/vm_device.rb +80 -0
  119. data/lib/vm_disk.rb +202 -0
  120. data/lib/vm_folder.rb +69 -0
  121. data/lib/vm_helper.rb +30 -0
  122. data/lib/vm_monitor.rb +303 -0
  123. data/lib/vm_nic.rb +70 -0
  124. data/lib/vm_template.rb +1961 -0
  125. data/lib/vmm_importer.rb +121 -0
  126. metadata +101 -35
data/lib/vi_client.rb ADDED
@@ -0,0 +1,281 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2021, OpenNebula Project, OpenNebula Systems #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+ require 'openssl'
17
+ require 'yaml'
18
+
19
+ ##############################################################################
20
+ # Module VCenterDriver
21
+ ##############################################################################
22
+ module VCenterDriver
23
+
24
+ ##########################################################################
25
+ # Class VIClient
26
+ ##########################################################################
27
+ class VIClient
28
+
29
+ attr_accessor :vim
30
+ attr_accessor :rp
31
+ attr_accessor :vc_name
32
+ attr_accessor :ccr_ref
33
+
34
+ def initialize(opts, host_id = -1)
35
+ opts = { :insecure => true }.merge(opts)
36
+ @host_id = host_id
37
+ @vim = RbVmomi::VIM.connect(opts)
38
+ @vc_name = opts[:host] if opts[:host]
39
+
40
+ # Get ccr and get rp
41
+ @ccr_ref = opts.delete(:ccr)
42
+
43
+ return unless @ccr_ref
44
+
45
+ ccr = RbVmomi::VIM::ClusterComputeResource.new(@vim, @ccr_ref)
46
+
47
+ # Get ref for rp
48
+
49
+ return unless ccr
50
+
51
+ rp = opts.delete(:rp)
52
+
53
+ return unless rp
54
+
55
+ rp_list = get_resource_pools(ccr)
56
+ rp_ref =
57
+ rp_list
58
+ .select {|r| r[:name] == rp }.first[:ref] rescue nil
59
+ @rp = RbVmomi::VIM::ResourcePool(@vim, rp_ref) if rp_ref
60
+ end
61
+
62
+ def rp_confined?
63
+ !!@rp
64
+ end
65
+
66
+ def host_credentials(one_client)
67
+ raise 'no host id defined!' if @host_id == -1
68
+
69
+ host =
70
+ OpenNebula::Host
71
+ .new_with_id(
72
+ @host_id,
73
+ one_client
74
+ )
75
+ rc = host.info
76
+ if OpenNebula.is_error?(rc)
77
+ raise "Could not get host info \
78
+ for ID: #{@host_id} - #{rc.message}"
79
+ end
80
+
81
+ { :pass => host['TEMPLATE/VCENTER_PASSWORD'],
82
+ :user => host['TEMPLATE/VCENTER_USER'],
83
+ :host => @vc_name }
84
+ end
85
+
86
+ def get_resource_pools(ccr, rp = nil, parent_prefix = '', rp_array = [])
87
+ current_rp = ''
88
+
89
+ if !rp
90
+ rp = ccr.resourcePool
91
+ else
92
+ if !parent_prefix.empty?
93
+ current_rp << parent_prefix
94
+ current_rp << '/'
95
+ end
96
+ current_rp << rp.name
97
+ end
98
+
99
+ if rp.resourcePool.empty?
100
+ rp_info = {}
101
+ rp_info[:name] = current_rp
102
+ rp_info[:ref] = rp._ref
103
+ rp_array << rp_info
104
+ else
105
+ rp.resourcePool.each do |child_rp|
106
+ get_resource_pools(ccr, child_rp, current_rp, rp_array)
107
+ end
108
+ rp_info = {}
109
+ rp_info[:name] = current_rp
110
+ rp_info[:ref] = rp._ref
111
+ rp_array << rp_info unless current_rp.empty?
112
+ end
113
+
114
+ rp_array
115
+ end
116
+
117
+ def close_connection
118
+ @vim.close
119
+ end
120
+
121
+ # @return RbVmomi::VIM::<type> objects
122
+ def self.get_entities(folder, type, entities = [])
123
+ if folder == []
124
+ # rubocop:disable Style/ReturnNil
125
+ return nil
126
+ # rubocop:enable Style/ReturnNil
127
+ end
128
+
129
+ folder.childEntity.each do |child|
130
+ the_name, _junk = child.to_s.split('(')
131
+ case the_name
132
+ when 'Folder'
133
+ get_entities(child, type, entities)
134
+ when type
135
+ entities.push(child)
136
+ end
137
+ end
138
+
139
+ entities
140
+ end
141
+
142
+ def self.new_from_host(host_id, client = nil)
143
+ begin
144
+ client = OpenNebula::Client.new if client.nil?
145
+ host = OpenNebula::Host.new_with_id(host_id, client)
146
+ rc = host.info(true)
147
+ if OpenNebula.is_error?(rc)
148
+ raise "Could not get host info for \
149
+ ID: #{host_id} - #{rc.message}"
150
+ end
151
+
152
+ connection = {
153
+ :host => host['TEMPLATE/VCENTER_HOST'],
154
+ :user => host['TEMPLATE/VCENTER_USER'],
155
+ :rp => host['TEMPLATE/VCENTER_RESOURCE_POOL'],
156
+ :ccr => host['TEMPLATE/VCENTER_CCR_REF'],
157
+ :password => host['TEMPLATE/VCENTER_PASSWORD']
158
+ }
159
+
160
+ vc_port = host['TEMPLATE/VCENTER_PORT']
161
+ connection[:port] = vc_port unless vc_port.nil?
162
+
163
+ new(connection, host_id)
164
+ rescue StandardError => e
165
+ raise e
166
+ end
167
+ end
168
+
169
+ def self.new_from_datastore(datastore_id)
170
+ begin
171
+ client = OpenNebula::Client.new
172
+ datastore =
173
+ OpenNebula::Datastore
174
+ .new_with_id(
175
+ datastore_id,
176
+ client
177
+ )
178
+ rc = datastore.info
179
+ if OpenNebula.is_error?(rc)
180
+ raise "Could not get datastore info \
181
+ for ID: #{datastore_id} - #{rc.message}"
182
+ end
183
+
184
+ vcenter_id = datastore['TEMPLATE/VCENTER_INSTANCE_ID']
185
+
186
+ host_pool = OpenNebula::HostPool.new(client)
187
+ rc = host_pool.info
188
+ if OpenNebula.is_error?(rc)
189
+ raise "Could not get hosts information - #{rc.message}"
190
+ end
191
+
192
+ user = ''
193
+ password = ''
194
+ port = 0
195
+ host_pool.each do |host|
196
+ vc_instance_id = host['TEMPLATE/VCENTER_INSTANCE_ID']
197
+ next unless vc_instance_id == vcenter_id
198
+
199
+ host_decrypted =
200
+ OpenNebula::Host
201
+ .new_with_id(
202
+ host['ID'],
203
+ client
204
+ )
205
+ host_decrypted.info(true)
206
+ user = host_decrypted['TEMPLATE/VCENTER_USER']
207
+ password = host_decrypted['TEMPLATE/VCENTER_PASSWORD']
208
+ port = host_decrypted['TEMPLATE/VCENTER_PORT']
209
+ end
210
+ if password.empty? || user.empty?
211
+ raise "Error getting \
212
+ credentials for datastore #{datastore_id}"
213
+ end
214
+
215
+ connection = {
216
+ :host => datastore['TEMPLATE/VCENTER_HOST'],
217
+ :user => user,
218
+ :password => password
219
+ }
220
+
221
+ connection[:port] = port unless port.nil?
222
+
223
+ new(connection)
224
+ rescue StandardError => e
225
+ raise e
226
+ end
227
+ end
228
+
229
+ def self.decrypt(msg, token)
230
+ begin
231
+ cipher = OpenSSL::Cipher.new('aes-256-cbc')
232
+
233
+ cipher.decrypt
234
+
235
+ # Truncate for Ruby 2.4 (in previous versions this was being
236
+ # automatically truncated)
237
+ cipher.key = token[0..31]
238
+
239
+ msg = cipher.update(Base64.decode64(msg))
240
+ msg << cipher.final
241
+ rescue StandardError
242
+ raise 'Error decrypting secret.'
243
+ end
244
+ end
245
+
246
+ def self.in_silence
247
+ begin
248
+ orig_stderr = $stderr.clone
249
+ orig_stdout = $stdout.clone
250
+ $stderr.reopen File.new('/dev/null', 'w')
251
+ $stdout.reopen File.new('/dev/null', 'w')
252
+ retval = yield
253
+ rescue StandardError => e
254
+ $stdout.reopen orig_stdout
255
+ $stderr.reopen orig_stderr
256
+ raise e
257
+ ensure
258
+ $stdout.reopen orig_stdout
259
+ $stderr.reopen orig_stderr
260
+ end
261
+ retval
262
+ end
263
+
264
+ def self.in_stderr_silence
265
+ begin
266
+ orig_stderr = $stderr.clone
267
+ $stderr.reopen File.new('/dev/null', 'w')
268
+ retval = yield
269
+ rescue StandardError => e
270
+ $stderr.reopen orig_stderr
271
+ raise e
272
+ ensure
273
+ $stderr.reopen orig_stderr
274
+ end
275
+ retval
276
+ end
277
+
278
+ end
279
+
280
+ end
281
+ # module VCenterDriver
data/lib/vi_helper.rb ADDED
@@ -0,0 +1,312 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2021, OpenNebula Project, OpenNebula Systems #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+
17
+ ########################################################################
18
+ # Module VCenterDriver
19
+ ########################################################################
20
+ module VCenterDriver
21
+
22
+ ########################################################################
23
+ # Class VIHelper
24
+ ########################################################################
25
+ class VIHelper
26
+
27
+ ETC_LOCATION = '/etc/one/' unless defined?(ETC_LOCATION)
28
+ VCENTER_DRIVER_DEFAULT = "#{ETC_LOCATION}/vcenter_driver.default"
29
+ VM_PREFIX_DEFAULT = 'one-$i-'
30
+
31
+ # rubocop:disable Style/GlobalVars
32
+ # rubocop:disable Style/ClassVars
33
+ def self.client
34
+ if $conf.nil?
35
+ @@client ||= OpenNebula::Client.new # rubocop:disable Style/ClassVars
36
+ else
37
+ @@client ||= OpenNebula::Client.new(
38
+ nil,
39
+ $conf[:one_xmlrpc]
40
+ )
41
+ end
42
+ end
43
+ # rubocop:enable Style/GlobalVars
44
+
45
+ def self.set_client(options, client = nil)
46
+ if client.nil?
47
+ @@client=OpenNebulaHelper::OneHelper.get_client(options, true)
48
+ else
49
+ @@client = client
50
+ end
51
+ end
52
+ # rubocop:enable Style/ClassVars
53
+
54
+ def self.return_if_error(rc, item, exit_if_fail)
55
+ if OpenNebula.is_error?(rc)
56
+ raise rc.message unless exit_if_fail
57
+
58
+ STDERR.puts rc.message
59
+ exit 1
60
+ else
61
+ item
62
+ end
63
+ end
64
+
65
+ require 'scripts_common'
66
+ def self.check_error(rc, message, exit_condition = false)
67
+ return unless OpenNebula.is_error?(rc)
68
+
69
+ OpenNebula.error_message("\n Error #{message}: #{rc.message}\n")
70
+ exit 1 if exit_condition
71
+
72
+ raise rc.message
73
+ end
74
+
75
+ def self.get_cluster_id(clusters)
76
+ clusters.each do |id|
77
+ return id unless id == -1
78
+ end
79
+ -1
80
+ end
81
+
82
+ def self.one_managed?(object)
83
+ if object.class.ancestors.include?(OpenNebula::XMLElement)
84
+ managed =
85
+ object['TEMPLATE/OPENNEBULA_MANAGED'] ||
86
+ object['USER_TEMPLATE/OPENNEBULA_MANAGED']
87
+ return managed != 'NO'
88
+ end
89
+ false
90
+ end
91
+
92
+ def self.check_opts(opts, att_list)
93
+ att_list.each do |att|
94
+ raise "#{att} option is mandatory" if opts[att].nil?
95
+ end
96
+ end
97
+
98
+ def self.one_item(the_class, id, exit_if_fail = true)
99
+ item = the_class.new_with_id(id, client)
100
+ rc = item.info
101
+ return_if_error(rc, item, exit_if_fail)
102
+ end
103
+
104
+ def self.new_one_item(the_class)
105
+ the_class.new(the_class.build_xml, client)
106
+ end
107
+
108
+ def self.one_pool(the_class, exit_if_fail = true)
109
+ item = the_class.new(client)
110
+
111
+ rc = nil
112
+ begin
113
+ rc = item.info_all
114
+ rescue StandardError
115
+ rc = item.info
116
+ end
117
+
118
+ return_if_error(rc, item, exit_if_fail)
119
+ end
120
+
121
+ def self.find_by_name(the_class, name, pool = nil, raise_if_fail = true)
122
+ pool = one_pool(the_class, raise_if_fail) if pool.nil?
123
+ element = pool.find {|e| e['NAME'] == name.to_s }
124
+ if element.nil? && raise_if_fail
125
+ raise "Could not find element '#{name}' in pool '#{the_class}'"
126
+ end
127
+
128
+ element
129
+ end
130
+
131
+ def self.generate_name(opts, nbytes)
132
+ return opts[:name] if nbytes <= 0
133
+
134
+ @sha256 ||= Digest::SHA256.new
135
+ chain = opts[:key]
136
+ hash = @sha256.hexdigest(chain)[0..nbytes-1]
137
+
138
+ "#{opts[:name]}-#{hash}"
139
+ end
140
+
141
+ def self.one_name(the_class, name, key, pool = nil, bytes = 0)
142
+ # Remove \u007F character that comes from vcenter
143
+ name = name.tr("\u007F", '')
144
+ pool = one_pool(the_class) if pool.nil?
145
+
146
+ import_name = generate_name({ :name => name, :key => key }, bytes)
147
+
148
+ begin
149
+ find_by_name(the_class, import_name, pool)
150
+ rescue StandardError
151
+ return import_name
152
+ end
153
+
154
+ one_name(the_class, name, key, pool, bytes+2)
155
+ end
156
+
157
+ def self.get_ref_key(element, attribute, vcenter_uuid = nil)
158
+ key = element[attribute]
159
+
160
+ return if key.nil?
161
+
162
+ tvid = element['TEMPLATE/VCENTER_INSTANCE_ID']
163
+ uvid = element['USER_TEMPLATE/VCENTER_INSTANCE_ID']
164
+
165
+ if tvid
166
+ key += tvid
167
+ elsif uvid
168
+ key += uvid
169
+ elsif vcenter_uuid
170
+ key += vcenter_uuid
171
+ end
172
+
173
+ key
174
+ end
175
+
176
+ def self.create_ref_hash(attribute, pool, vcenter_uuid = nil)
177
+ hash = {}
178
+
179
+ pool.each_element(proc do |e|
180
+ refkey = get_ref_key(e, attribute, vcenter_uuid)
181
+ hash[refkey] = e
182
+ end)
183
+
184
+ hash
185
+ end
186
+
187
+ def self.clean_ref_hash
188
+ @ref_hash = {}
189
+ end
190
+
191
+ def self.add_ref_hash(attr, one_object)
192
+ raise 'cache is empty!' unless @ref_hash
193
+
194
+ refkey = get_ref_key(one_object, attr)
195
+
196
+ return unless @ref_hash[attr]
197
+
198
+ @ref_hash[attr][refkey] = one_object
199
+ end
200
+
201
+ def self.remove_ref_hash(attr, one_object)
202
+ raise 'cache is empty!' unless @ref_hash
203
+
204
+ refkey = get_ref_key(one_object, attr)
205
+
206
+ return unless @ref_hash[attr]
207
+
208
+ @ref_hash[attr].delete(refkey)
209
+ end
210
+
211
+ def self.find_by_ref(
212
+ the_class,
213
+ attribute,
214
+ ref,
215
+ vcenter_uuid,
216
+ pool = nil
217
+ )
218
+ pool = one_pool(the_class, false) if pool.nil?
219
+ @ref_hash ||= {}
220
+
221
+ if @ref_hash[attribute].nil? || @ref_hash[attribute] == {}
222
+ @ref_hash[attribute] = create_ref_hash(attribute,
223
+ pool,
224
+ vcenter_uuid)
225
+ end
226
+
227
+ refkey = ''
228
+ refkey = ref if ref
229
+ refkey += vcenter_uuid if vcenter_uuid
230
+
231
+ @ref_hash[attribute][refkey]
232
+ end
233
+
234
+ def self.find_image_by(att, the_class, path, ds_id, pool = nil)
235
+ pool = one_pool(the_class, false) if pool.nil?
236
+ pool.find do |e|
237
+ e[att] == path &&
238
+ e['DATASTORE_ID'] == ds_id
239
+ end
240
+ end
241
+
242
+ def self.find_persistent_image_by_source(source, pool)
243
+ pool.find do |e|
244
+ e['SOURCE'] == source &&
245
+ e['PERSISTENT'] == '1'
246
+ end
247
+ end
248
+
249
+ def self.find_vcenter_vm_by_name(one_vm, host, vi_client)
250
+ # Let's try to find the VM object only by its name
251
+ # Let's build the VM name
252
+ vm_prefix = host['TEMPLATE/VM_PREFIX']
253
+ vm_prefix = VM_PREFIX_DEFAULT if vm_prefix.nil? || vm_prefix.empty?
254
+ vm_prefix.gsub!('$i', one_vm['ID'])
255
+ vm_name = vm_prefix + one_vm['NAME']
256
+
257
+ # We have no DEPLOY_ID, the VM has never been deployed
258
+ # let's use a view to try to find the VM from the root folder
259
+ view = vi_client
260
+ .vim
261
+ .serviceContent
262
+ .viewManager
263
+ .CreateContainerView(
264
+ {
265
+ :container => vi_client.vim.rootFolder,
266
+ :type => ['VirtualMachine'],
267
+ :recursive => true
268
+ }
269
+ )
270
+
271
+ if !view.view.nil? && !view.view.empty?
272
+ vcenter_vm = view
273
+ .view
274
+ .find {|v| v.name == vm_name }
275
+ end
276
+
277
+ view.DestroyView # Destroy the view
278
+
279
+ vcenter_vm
280
+ end
281
+
282
+ def self.get_default(xpath)
283
+ begin
284
+ xml = OpenNebula::XMLElement.new
285
+ xml.initialize_xml(File.read(VCENTER_DRIVER_DEFAULT), 'VCENTER')
286
+ xml[xpath]
287
+ rescue StandardError
288
+ nil
289
+ end
290
+ end
291
+
292
+ def self.get_location(item)
293
+ folders = []
294
+ until item.instance_of? RbVmomi::VIM::Datacenter
295
+ item = item.parent
296
+ if !item.instance_of?(RbVmomi::VIM::Datacenter) &&
297
+ item.name != 'host'
298
+ folders << item.name
299
+ end
300
+ raise 'Could not find the location' if item.nil?
301
+ end
302
+ location = folders.reverse.join('/')
303
+ location = '/' if location.empty?
304
+
305
+ location
306
+ end
307
+
308
+ end
309
+ # class VIHelper
310
+
311
+ end
312
+ # module VCenterDriver