Class: NSXDriver::NSXVLogicalPort

Inherits:
LogicalPort show all
Defined in:
lib/nsxv_logical_port.rb

Overview

The NSXVLogicalPort class represents a LogicalPort in NSXv

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from LogicalPort

#lp_id, new_child

Constructor Details

#initialize(nsx_client, id = nil, data = nil) ⇒ NSXVLogicalPort

CONSTRUCTOR Logical port class variables:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nsxv_logical_port.rb', line 30

def initialize(nsx_client, id = nil, data = nil)
    super(nsx_client)
    # lpid can be:
    #   - Logical port ID
    #   - Logical port attach ID
    if id
        initialize_with_id(id)
    else
        if data
            begin
                @id = new_logical_port(data)
            rescue NSXError::IncorrectResponseCodeError => e
                raise 'Logical Port not created in ' \
                "NSX Manager: #{e.message}"
            end
            unless @id
                raise 'Logical Port not created in NSX Manager: '\
                      'generic error'
            end
            # Construct logical port class variables
            @url = NSXConstants::NSXT_LP_BASE + @id
            @name = lp_name
            @type = lp_type
        end
    end
end

Instance Attribute Details

#idObject (readonly)

ATTRIBUTES



22
23
24
# File 'lib/nsxv_logical_port.rb', line 22

def id
  @id
end

#nameObject (readonly)

ATTRIBUTES



22
23
24
# File 'lib/nsxv_logical_port.rb', line 22

def name
  @name
end

#typeObject (readonly)

ATTRIBUTES



22
23
24
# File 'lib/nsxv_logical_port.rb', line 22

def type
  @type
end

#urlObject (readonly)

ATTRIBUTES



22
23
24
# File 'lib/nsxv_logical_port.rb', line 22

def url
  @url
end

Instance Method Details

#initialize_with_id(id) ⇒ Object

Creates a NSXTLogicalPort from its id



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nsxv_logical_port.rb', line 58

def initialize_with_id(id)
    # First try lpid as logical port id
    @id = id
    # Construct URL of the created logical switch
    @url = NSXConstants::NSXV_LP_BASE + @id
    if lp?
        @name = lp_name
        @type = lp_type
    else
        # Second try with lpid as logical port attach id
        @id = lp_with_attachid(id)
        if @id.nil?
            error_msg = "Logical port with id: #{id} not found"
            error = NSXError::ObjectNotFound
                    .new(error_msg)
            raise error
        else
            @url = NSXConstants::NSXT_LP_BASE + @id
            @name = lp_name
            @type = lp_type
        end
    end
end

#lp?Boolean

Check if logical port exists

Returns:

  • (Boolean)


83
84
85
# File 'lib/nsxv_logical_port.rb', line 83

def lp?
    @nsx_client.get(@url) ? true : false
end

#lp_nameObject

Get logical port display name



96
97
98
# File 'lib/nsxv_logical_port.rb', line 96

def lp_name
    @nsx_client.get(@url)['display_name']
end

#lp_typeObject

Get resource type



101
102
103
# File 'lib/nsxv_logical_port.rb', line 101

def lp_type
    @nsx_client.get(@url)['resource_type']
end

#lp_with_attachid(attach_id) ⇒ Object

Get logical port id from attach id



88
89
90
91
92
93
# File 'lib/nsxv_logical_port.rb', line 88

def lp_with_attachid(attach_id)
    lps = @nsx_client.get(NSXConstants::NSXT_LP_BASE)
    lps['results'].each do |lp|
        return lp['id'] if lp['attachment']['id'] == attach_id
    end
end