Revision 347968ad src/vmm_mad/remotes/lib/vcenter_driver/datacenter.rb

View differences:

src/vmm_mad/remotes/lib/vcenter_driver/datacenter.rb
33 33

  
34 34
        @items[ref.to_sym]
35 35
    end
36

  
37
    def get_vcenter_instance_uuid
38
        @vi_client.vim.serviceContent.about.instanceUuid
39
    end
40

  
41
    def get_clusters
42

  
43
        clusters = {}
44

  
45
        vcenter_uuid = get_vcenter_instance_uuid
46

  
47
        pool = VCenterDriver::VIHelper.one_pool(OpenNebula::HostPool, false)
48
        if pool.respond_to?(:message)
49
            raise "Could not get OpenNebula Pool: #{pool.message}"
50
        end
51

  
52
        fetch! if @items.empty? #Get datacenters
53

  
54
        # Add datacenter to hash and store in an array all clusters
55
        @items.values.each do |dc|
56
            dc_name = dc.item.name
57
            clusters[dc_name] = []
58

  
59
            host_folder = dc.host_folder
60
            host_folder.fetch_clusters!
61

  
62
            host_folder.items.values.each do |ccr|
63
                cluster = {}
64
                cluster[:ref]  = ccr['_ref']
65
                cluster[:name] = ccr['name']
66
                attribute = "TEMPLATE/VCENTER_CCR_REF"
67
                one_host = VCenterDriver::VIHelper.find_by_ref(OpenNebula::HostPool,
68
                                                               attribute,
69
                                                               ccr['_ref'],
70
                                                               vcenter_uuid,
71
                                                               pool)
72

  
73
                next if one_host.nil? #Cluster hasn't been imported'
74

  
75
                cluster[:host_id] = one_host['ID']
76
                clusters[dc_name] << cluster
77
            end
78
        end
79

  
80
        clusters
81
    end
82

  
83
    def get_unimported_datastores
84
        ds_objects = {}
85

  
86
        vcenter_uuid = get_vcenter_instance_uuid
87

  
88
        pool = VCenterDriver::VIHelper.one_pool(OpenNebula::DatastorePool, false)
89

  
90
        if pool.respond_to?(:message)
91
            raise "Could not get OpenNebula DatastorePool: #{pool.message}"
92
        end
93

  
94
        fetch! if @items.empty? #Get datacenters
95

  
96
        one_clusters = get_clusters
97

  
98
        @items.values.each do |dc|
99
            dc_name = dc.item.name
100
            ds_objects[dc_name] = []
101

  
102
            datastore_folder = dc.datastore_folder
103
            datastore_folder.fetch!
104
            datastore_folder.items.values.each do |ds|
105

  
106
                if ds.instance_of? VCenterDriver::Datastore
107
                    hosts_in_ds = ds['host']
108
                    clusters_in_ds = {}
109

  
110
                    hosts_in_ds.each do |host|
111
                        if !clusters_in_ds[host.key.parent._ref.to_s]
112
                            clusters_in_ds[host.key.parent._ref.to_s] = host.key.parent.name
113
                        end
114
                    end
115

  
116
                    clusters_in_ds.each do |ccr_ref, ccr_name|
117
                        already_image_ds = VCenterDriver::Storage.exists_one_by_ref_ccr_and_type?(ds["_ref"], ccr_ref, vcenter_uuid, "IMAGE_DS", pool)
118

  
119
                        if !already_image_ds
120
                            object = ds.to_one_template(one_clusters[dc_name], ccr_ref, ccr_name, "IMAGE_DS", vcenter_uuid)
121
                            ds_objects[dc_name] << object if !object.nil?
122
                        end
123

  
124
                        already_system_ds = VCenterDriver::Storage.exists_one_by_ref_ccr_and_type?(ds["_ref"], ccr_ref, vcenter_uuid, "SYSTEM_DS", pool)
125

  
126
                        if !already_system_ds
127
                            object = ds.to_one_template(one_clusters[dc_name], ccr_ref, ccr_name, "SYSTEM_DS", vcenter_uuid)
128
                            ds_objects[dc_name] << object if !object.nil?
129
                        end
130
                    end
131
                end
132

  
133
                if ds.instance_of? VCenterDriver::StoragePod
134
                    clusters_in_spod = {}
135
                    ds_in_spod = ds['children']
136

  
137
                    ds_in_spod.each do |sp_ds|
138
                        hosts_in_ds = sp_ds.host
139
                        hosts_in_ds.each do |host|
140
                            if !clusters_in_spod[host.key.parent._ref.to_s]
141
                                clusters_in_spod[host.key.parent._ref.to_s] = host.key.parent.name
142
                            end
143
                        end
144
                    end
145

  
146
                    clusters_in_spod.each do |ccr_ref, ccr_name|
147
                        already_system_ds = VCenterDriver::Storage.exists_one_by_ref_ccr_and_type?(ds["_ref"], ccr_ref, vcenter_uuid, "SYSTEM_DS", pool)
148

  
149
                        if !already_system_ds
150
                            object = ds.to_one_template(one_clusters[dc_name], ccr_ref, ccr_name, "SYSTEM_DS", vcenter_uuid)
151
                            ds_objects[dc_name] << object if !object.nil?
152
                        end
153
                    end
154
                end
155
            end
156
        end
157

  
158
        ds_objects
159
    end
160

  
161
    def get_unimported_templates(vi_client)
162
        template_objects = {}
163
        vcenter_uuid = get_vcenter_instance_uuid
164
        tpool = VCenterDriver::VIHelper.one_pool(OpenNebula::TemplatePool, false)
165

  
166
        if tpool.respond_to?(:message)
167
            raise "Could not get OpenNebula TemplatePool: #{tpool.message}"
168
        end
169

  
170
        fetch! if @items.empty? #Get datacenters
171

  
172
        @items.values.each do |dc|
173

  
174
            dc_name = dc.item.name
175
            template_objects[dc_name] = []
176

  
177
            #Get datastores available in a datacenter
178
            ds_list = []
179
            datastore_folder = dc.datastore_folder
180
            datastore_folder.fetch!
181
            datastore_folder.items.values.each do |ds|
182
                ds_hash = {}
183
                ds_hash[:name] = ds["name"]
184
                ds_hash[:ref] = ds["_ref"]
185
                ds_list << ds_hash
186
            end
187

  
188
            #Get templates defined in a datacenter
189
            vm_folder = dc.vm_folder
190
            vm_folder.fetch_templates!
191
            vm_folder.items.values.each do |template|
192
                one_template = VCenterDriver::VIHelper.find_by_ref(OpenNebula::TemplatePool,
193
                                                                   "TEMPLATE/VCENTER_TEMPLATE_REF",
194
                                                                   template['_ref'],
195
                                                                   vcenter_uuid,
196
                                                                   tpool)
197
                next if one_template #If the template has been already imported
198

  
199
                template_name = template['name']
200
                template_ref  = template['_ref']
201
                template_ccr  = template['runtime.host.parent']
202
                cluster_name  = template['runtime.host.parent.name']
203

  
204
                #Get DS list
205
                ds = ""
206
                default_ds = nil
207
                if !ds_list.empty?
208
                    ds_name_list = []
209
                    ds_list.each do |ds_hash|
210
                        ds_name_list << ds_hash[:name]
211
                    end
212
                    ds =  "M|list|Which datastore you want this VM to run in? "
213
                    ds << "|#{ds_name_list.join(",")}" #List of DS
214
                    ds << "|#{ds_name_list.first}" #Default DS
215
                    default_ds = ds_name_list.first
216
                end
217

  
218
                #Get resource pools
219
                rp_cache = {}
220
                if !rp_cache[template_ccr.name.to_s]
221
                    tmp_cluster = VCenterDriver::ClusterComputeResource.new_from_ref(template_ccr._ref, vi_client)
222
                    rp_list = tmp_cluster.get_resource_pool_list
223
                    rp = ""
224
                    if !rp_list.empty?
225
                        rp_name_list = []
226
                        rp_list.each do |rp_hash|
227
                            rp_name_list << rp_hash[:name]
228
                        end
229
                        rp =  "M|list|Which resource pool you want this VM to run in? "
230
                        rp << "|#{rp_name_list.join(",")}" #List of RP
231
                        rp << "|#{rp_name_list.first}" #Default RP
232
                    end
233
                    rp_cache[template_ccr.name.to_s] = rp
234
                end
235
                rp = rp_cache[template_ccr.name.to_s]
236

  
237
                object = template.to_one_template(template_name,
238
                                                  template_ref,
239
                                                  template_ccr._ref,
240
                                                  cluster_name,
241
                                                  ds,
242
                                                  ds_list,
243
                                                  default_ds,
244
                                                  rp,
245
                                                  rp_list,
246
                                                  vcenter_uuid)
247

  
248
                template_objects[dc_name] << object if !object.nil?
249
            end #template loop
250
        end #datacenter loop
251
        return template_objects
252
    end
253

  
254
    def get_unimported_networks
255

  
256
        network_objects = {}
257
        vcenter_uuid = get_vcenter_instance_uuid
258
        npool = VCenterDriver::VIHelper.one_pool(OpenNebula::VirtualNetworkPool, false)
259

  
260
        if npool.respond_to?(:message)
261
            raise "Could not get OpenNebula VirtualNetworkPool: #{npool.message}"
262
        end
263

  
264
        fetch! if @items.empty? #Get datacenters
265

  
266
        @items.values.each do |dc|
267

  
268
            dc_name = dc.item.name
269
            network_objects[dc_name] = []
270

  
271
            #Get networks defined in a datacenter
272
            network_folder = dc.network_folder
273
            network_folder.fetch!
274
            network_folder.items.values.each do |network|
275

  
276
                one_network = VCenterDriver::VIHelper.find_by_ref(OpenNebula::VirtualNetworkPool,
277
                                                                  "TEMPLATE/VCENTER_NET_REF",
278
                                                                  network['_ref'],
279
                                                                  vcenter_uuid,
280
                                                                  npool)
281
                next if one_network #If the network has been already imported
282

  
283
                network_name = network['name']
284
                network_ref  = network['_ref']
285

  
286
                # TODO slow VLAN_ID retrieve for portgroups! set to nil
287
                vlan_id = ""
288
                if network.class == VCenterDriver::DistributedPortGroup
289
                    vlan_id = network.vlan_id
290
                end
291

  
292
                network.clusters.each do |ccr_ref, ccr_name|
293
                    one_vnet = VCenterDriver::Network.to_one_template(network_name,
294
                                                                      network_ref,
295
                                                                      network.network_type,
296
                                                                      vlan_id,
297
                                                                      ccr_ref,
298
                                                                      ccr_name,
299
                                                                      vcenter_uuid)
300
                    network_objects[dc_name] << one_vnet
301
                end #network clusters loop
302
            end # network loop
303
        end #datacenters loop
304

  
305
        return network_objects
306

  
307
    end
308

  
36 309
end # class DatatacenterFolder
37 310

  
38 311
class Datacenter

Also available in: Unified diff