Class: VCenterDriver::Network

Inherits:
Object
  • Object
show all
Includes:
Memoize
Defined in:
lib/network.rb

Overview

Class Network

Constant Summary collapse

NETWORK_TYPE_PG =
'Port Group'
NETWORK_TYPE_DPG =
'Distributed Port Group'
NETWORK_TYPE_NSXV =

“Virtual Wire”

'NSX-V'
NETWORK_TYPE_NSXT =
'Opaque Network'
NETWORK_TYPE_UNKNOWN =
'Unknown Network'
REQUIRED_ATTRS =
[:refs, :one_ids, :one_object]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Memoize

#[], #[]=, #clear, #clear_all

Constructor Details

#initialize(item, vi_client = nil, check = true) ⇒ Network

Returns a new instance of Network.



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/network.rb', line 103

def initialize(item, vi_client = nil, check = true)
    if check
        begin
            check_item(item, RbVmomi::VIM::Network)
        rescue StandardError
            check_item(item, RbVmomi::VIM::DistributedVirtualPortgroup)
        end
    end

    @vi_client = vi_client
    @item = item
end

Instance Attribute Details

#itemObject

Returns the value of attribute item.



93
94
95
# File 'lib/network.rb', line 93

def item
  @item
end

Class Method Details

.create_one_network(net_config) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/network.rb', line 273

def self.create_one_network(net_config)
    # mandatory parameters:
    REQUIRED_ATTRS.each do |attr|
        if net_config[attr].nil?
            raise "#{attr} required for importing nics operation!"
        end
    end

    one_vn = VCenterDriver::VIHelper
             .new_one_item(
                 OpenNebula::VirtualNetwork
             )

    done = []
    (0..net_config[:refs].size-1).each do |i|
        cl_id = net_config[:one_ids][i]
        next if cl_id == -1 || done.include?(cl_id)

        if done.empty?
            rc = one_vn.allocate(net_config[:one_object], cl_id.to_i)
            VCenterDriver::VIHelper.check_error(rc, 'create network')
            one_vn.info
        else
            one_cluster = VCenterDriver::VIHelper
                          .one_item(
                              OpenNebula::Cluster,
                              cl_id,
                              false
                          )
            rc = one_cluster.addvnet(one_vn['ID'].to_i)
            VCenterDriver::VIHelper
                .check_error(rc, 'addvnet to cluster')
        end
        done << cl_id
    end

    one_vn
end

.generate_name(name, opts = {}) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/network.rb', line 158

def self.generate_name(name, opts = {})
    vcenter_instance_name = opts[:vcenter_name] || nil
    dc_name               = opts[:dc_name] || nil

    hash_name = "#{name} - [#{vcenter_instance_name} - #{dc_name}]"
    sha256 = Digest::SHA256.new
    network_hash = sha256.hexdigest(hash_name)[0..11]
    "#{name} - [#{vcenter_instance_name} - #{dc_name}]_#{network_hash}"
end

.get_network_type(network, network_name) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/network.rb', line 312

def self.get_network_type(network, network_name)
    case network
    when RbVmomi::VIM::DistributedVirtualPortgroup
        if network_name
           .match(/^vxw-dvs-(.*)-virtualwire-(.*)-sid-(.*)/)
            VCenterDriver::Network::NETWORK_TYPE_NSXV
        else
            VCenterDriver::Network::NETWORK_TYPE_DPG
        end
    when RbVmomi::VIM::OpaqueNetwork
        VCenterDriver::Network::NETWORK_TYPE_NSXT
    when RbVmomi::VIM::Network
        VCenterDriver::Network::NETWORK_TYPE_PG
    else
        VCenterDriver::Network::NETWORK_TYPE_UNKNOWN
    end
end

.new_from_ref(ref, vi_client) ⇒ Object

This is never cached



367
368
369
# File 'lib/network.rb', line 367

def self.new_from_ref(ref, vi_client)
    new(RbVmomi::VIM::Network.new(vi_client.vim, ref), vi_client)
end

.nic?(device) ⇒ Boolean

Checks if a RbVmomi::VIM::VirtualDevice is a network interface

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
# File 'lib/network.rb', line 117

def self.nic?(device)
    !device
        .class
        .ancestors
        .index(
            RbVmomi::VIM::VirtualEthernetCard
        ).nil?
end

.remove_net_ref(network_id) ⇒ Object



352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/network.rb', line 352

def self.remove_net_ref(network_id)
    one_vnet = VCenterDriver::VIHelper
               .one_item(
                   OpenNebula::VirtualNetwork,
                   network_id
               )
    one_vnet.info
    one_vnet.delete_element('TEMPLATE/VCENTER_NET_REF')
    one_vnet.delete_element('TEMPLATE/VCENTER_INSTANCE_ID')
    tmp_str = one_vnet.template_str
    one_vnet.update(tmp_str)
    one_vnet.info
end

.retrieve_vlanid(network) ⇒ Object



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

def self.retrieve_vlanid(network)
    begin
        name = network.name
        id = network
             .host
             .first
             .configManager
             .networkSystem
             .networkConfig
             .portgroup
             .select do |p|
            p.spec.name == name
        end.first.spec.vlanId
    rescue StandardError
        id = -1
    end
    id
end

.to_one(opts) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/network.rb', line 228

def self.to_one(opts)
    template = "NAME=\"#{opts[:network_import_name]}\"\n"\
               "BRIDGE=\"#{opts[:bridge_name]}\"\n"\
               "VN_MAD=\"vcenter\"\n"\
               "VCENTER_PORTGROUP_TYPE=\"#{opts[:network_type]}\"\n"\
               "VCENTER_NET_REF=\"#{opts[:network_ref]}\"\n"\
               "VCENTER_INSTANCE_ID=\"#{opts[:vcenter_uuid]}\"\n"\
               "VCENTER_IMPORTED=\"YES\"\n"

    if opts[:unmanaged] == 'wild'
        template += "VCENTER_FROM_WILD=\"#{opts[:template_id]}\"\n"
    end

    if opts[:template_ref]
        template +=
            "VCENTER_TEMPLATE_REF=\"#{opts[:template_ref]}\"\n"
    end

    if opts[:sw_name]
        template +=
            "VCENTER_SWITCH_NAME=\"#{opts[:sw_name]}\"\n"
    end

    if opts[:nsx_id]
        template +=
            "NSX_ID=\"#{opts[:nsx_id]}\"\n"
    end
    if opts[:nsx_vni]
        template +=
            "NSX_VNI=\"#{opts[:nsx_vni]}\"\n"
    end
    if opts[:nsx_tz_id]
        template +=
            "NSX_TZ_ID=\"#{opts[:nsx_tz_id]}\"\n"
    end

    if opts[:vlanid]
        template +=
            "VCENTER_VLAN_ID=\"#{opts[:vlanid]}\"\n"
    end

    template
end

.to_one_template(opts = {}) ⇒ Object



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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/network.rb', line 168

def self.to_one_template(opts = {})
    one_tmp = {}
    network_name          = opts[:network_name]
    network_ref           = opts[:network_ref]
    network_type          = opts[:network_type]
    sw_name               = opts[:sw_name]

    vcenter_uuid          = opts[:vcenter_uuid]
    cluster_id            = opts[:cluster_id]

    unmanaged             = opts[:unmanaged] || nil
    template_ref          = opts[:template_ref] || nil
    dc_ref                = opts[:dc_ref] || nil
    template_id           = opts[:template_id] || nil

    nsx_id                = opts[:nsx_id] || nil
    nsx_vni               = opts[:nsx_vni] || nil
    nsx_tz_id             = opts[:nsx_tz_id] || nil

    vlanid                = opts[:vlanid] || nil

    bridge_name = network_name
    network_name = network_name.gsub('/', '_')

    network_import_name =
        VCenterDriver::VIHelper
        .one_name(
            OpenNebula::VirtualNetworkPool,
            network_name,
            network_ref+vcenter_uuid
        )

    one_tmp[:name]             = bridge_name
    one_tmp[:import_name]      = network_import_name
    one_tmp[:bridge]           = bridge_name
    one_tmp[:type]             = network_type
    one_tmp[:one_cluster_id]   = cluster_id
    one_tmp[:ref] = network_ref

    opts = {
        :network_import_name    => network_import_name,
        :bridge_name            => bridge_name,
        :network_ref            => network_ref,
        :network_type           => network_type,
        :vcenter_uuid           => vcenter_uuid,
        :unmanaged              => unmanaged,
        :template_ref           => template_ref,
        :dc_ref                 => dc_ref,
        :template_id            => template_id,
        :sw_name                => sw_name,
        :nsx_id                 => nsx_id,
        :nsx_vni                => nsx_vni,
        :nsx_tz_id              => nsx_tz_id,
        :vlanid                 => vlanid
    }

    one_tmp[:one] = to_one(opts)
    one_tmp
end

.virtual_switch(vc_pg) ⇒ Object

Get vSwitch of Standard PortGroup If there is differents vSwitches returns the first.



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/network.rb', line 332

def self.virtual_switch(vc_pg)
    vswitch = []
    vc_hosts = vc_pg.host
    vc_hosts.each do |vc_host|
        host_pgs = vc_host
                   .configManager
                   .networkSystem
                   .networkInfo
                   .portgroup
        host_pgs.each do |pg|
            if vc_pg.name == pg.spec.name
                vswitch << pg.spec.vswitchName
            end
        end
    end
    vswitch.uniq!
    vswitch << 'Invalid configuration' if vswitch.length > 1
    vswitch.join(' / ')
end

.vlanid(vid) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/network.rb', line 126

def self.vlanid(vid)
    case vid
    when -1
        'error'
    when 0
        'disabled'
    when 4095
        'VGT'
    else
        vid.to_s
    end
end