class NSXDriver::VirtualWire

Class VirtualWire NSX-V Network

Public Class Methods

new(nsx_client, ls_id = nil, tz_id = nil, ls_data = nil) click to toggle source

CONSTRUCTOR

Calls superclass method
# File lib/virtual_wire.rb, line 22
def initialize(nsx_client, ls_id = nil, tz_id = nil, ls_data = nil)
    super(nsx_client)
    if ls_id
        initialize_with_id(ls_id)
    else
        if tz_id && ls_data
            begin
                @ls_id = new_logical_switch(ls_data, tz_id)
            rescue NSXError::IncorrectResponseCodeError => e
                raise 'VirtualWire not created in NSX Manager: ' \
                      "#{e.message}"
            end
            unless @ls_id
                raise 'Virtual Wire not created in NSX Manager: ' \
                      'generic error'
            end

            # Construct URL of the created logical switch
            @url_ls = NSXConstants::NSXV_LS_SECTION + @ls_id
            @ls_vni = ls_vni
            @ls_name = ls_name
            @tz_id = ls_tz
            @tenant_id = 'virtual wire tenant'
            @guest_vlan_allowed = false
        end
    end
end
new_from_name(nsx_client, ls_name) click to toggle source

Creates a VirtualWire from its name

# File lib/virtual_wire.rb, line 51
def self.new_from_name(nsx_client, ls_name)
    virtualwire = new(nsx_client)
    ls_id = virtualwire.ls_id_from_name(nsx_client, ls_name)
    unless ls_id
        error_msg = "VirtualWire with name: #{ls_name} not found"
        error = NSXError::ObjectNotFound
                .new(error_msg)
        raise error
    end

    # initialize_with_id(@ls_id)
    virtualwire.initialize_with_id(ls_id)
    virtualwire
end

Public Instance Methods

delete_logical_switch() click to toggle source

Delete a logical switch

# File lib/virtual_wire.rb, line 152
def delete_logical_switch
    @nsx_client.delete(@url_ls)
end
initialize_with_id(ls_id) click to toggle source

Creates a VirtualWire from its id

# File lib/virtual_wire.rb, line 67
def initialize_with_id(ls_id)
    @ls_id = ls_id
    # Construct URL of the created logical switch
    @url_ls = NSXConstants::NSXV_LS_SECTION + \
              @ls_id
    # Raise an error if VirtualWire id doesn't exists
    unless ls?
        error_msg = "VirtualWire with id: #{ls_id} not found"
        error = NSXError::ObjectNotFound
                .new(error_msg)
        raise error
    end

    @ls_vni =  ls_vni
    @ls_name = ls_name
    @tz_id = ls_tz
    @tenant_id = 'virtual wire tenant'
    @guest_vlan_allowed = false
end
ls?() click to toggle source

Check if logical switch exists

# File lib/virtual_wire.rb, line 108
def ls?
    @nsx_client.get(@url_ls) ? true : false
end
ls_dvs_ref() click to toggle source

Get the distributed virtual switch's ref associated to a LS

# File lib/virtual_wire.rb, line 138
def ls_dvs_ref
    @nsx_client.get(@url_ls)
               .xpath(NSXConstants::NSXV_LS_OBJECTID_XPATH)
               .text
end
ls_id_from_name(nsx_client, name) click to toggle source

Get the logical switch id from its name

# File lib/virtual_wire.rb, line 88
def ls_id_from_name(nsx_client, name)
    url = NSXConstants::NSXV_LS_SECTION
    virtualwires = nsx_client
                   .get(url)
                   .xpath(NSXConstants::NSXV_LS_XPATH)
    virtualwires.each do |virtualwire|
        lsname_arr = name.split(/-sid-/)
        lsname = lsname_arr[-1].split('-', 2)[-1]
        lsid = lsname_arr[0].split(/vxw-dvs-\w.-/)[-1]
        if virtualwire.xpath('name').text == lsname &&
           virtualwire.xpath('objectId').text == lsid
            return virtualwire.xpath('objectId').text
        end
    end
    nil
end
ls_name() click to toggle source

Get logical switch's name

# File lib/virtual_wire.rb, line 113
def ls_name
    @nsx_client.get(@url_ls)
               .xpath(NSXConstants::NSXV_LS_NAME_XPATH).text
end
ls_tz() click to toggle source

Get the Transport Zone of the logical switch

# File lib/virtual_wire.rb, line 125
def ls_tz
    @nsx_client.get(@url_ls)
               .xpath(NSXConstants::NSXV_TZ_XPATH).text
end
ls_vcenter_ref() click to toggle source

Get the logical switch reference into vcenter

# File lib/virtual_wire.rb, line 131
def ls_vcenter_ref
    @nsx_client.get(@url_ls)
               .xpath(NSXConstants::NSXV_LS_BACKING_XPATH)
               .text
end
ls_vni() click to toggle source

Get logical switch's vni

# File lib/virtual_wire.rb, line 119
def ls_vni
    @nsx_client.get(@url_ls)
               .xpath(NSXConstants::NSXV_LS_VNI_XPATH).text
end
new_logical_switch(ls_data, tz_id) click to toggle source

Create a new logical switch (NSX-V: virtualwire)

# File lib/virtual_wire.rb, line 145
def new_logical_switch(ls_data, tz_id)
    url = "#{NSXConstants::NSXV_TZ_SECTION}#{tz_id}" \
          '/virtualwires'
    @nsx_client.post(url, ls_data)
end