Class: NSXDriver::VirtualWire

Inherits:
LogicalSwitch show all
Defined in:
lib/virtual_wire.rb

Overview

Class VirtualWire NSX-V Network

Instance Attribute Summary

Attributes inherited from LogicalSwitch

#description, #display_name, #ls_id, #replication_mode, #tz_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from LogicalSwitch

nsx_nics, #update_logical_switch

Constructor Details

#initialize(nsx_client, ls_id = nil, tz_id = nil, ls_data = nil) ⇒ VirtualWire

CONSTRUCTOR



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

Class Method Details

.new_from_name(nsx_client, ls_name) ⇒ Object

Creates a VirtualWire from its name



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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

Instance Method Details

#delete_logical_switchObject

Delete a logical switch



152
153
154
# File 'lib/virtual_wire.rb', line 152

def delete_logical_switch
    @nsx_client.delete(@url_ls)
end

#initialize_with_id(ls_id) ⇒ Object

Creates a VirtualWire from its id



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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?Boolean

Check if logical switch exists

Returns:

  • (Boolean)


108
109
110
# File 'lib/virtual_wire.rb', line 108

def ls?
    @nsx_client.get(@url_ls) ? true : false
end

#ls_dvs_refObject

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



138
139
140
141
142
# 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) ⇒ Object

Get the logical switch id from its name



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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_nameObject

Get logical switch’s name



113
114
115
116
# File 'lib/virtual_wire.rb', line 113

def ls_name
    @nsx_client.get(@url_ls)
               .xpath(NSXConstants::NSXV_LS_NAME_XPATH).text
end

#ls_tzObject

Get the Transport Zone of the logical switch



125
126
127
128
# File 'lib/virtual_wire.rb', line 125

def ls_tz
    @nsx_client.get(@url_ls)
               .xpath(NSXConstants::NSXV_TZ_XPATH).text
end

#ls_vcenter_refObject

Get the logical switch reference into vcenter



131
132
133
134
135
# 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_vniObject

Get logical switch’s vni



119
120
121
122
# 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) ⇒ Object

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



145
146
147
148
149
# 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