Class: RubyNsxCli::NSXVirtualWire

Inherits:
NSXObject
  • Object
show all
Defined in:
lib/ruby_nsx_cli/nsx_objects/nsx_virtualwire.rb

Constant Summary collapse

CONTROL_PLANE_MODES =
%w[
    UNICAST_MODE
    MULTICAST_MODE
    HYBRID_MODE
]

Constants inherited from NSXObject

RubyNsxCli::NSXObject::XML_HEADER

Instance Method Summary collapse

Methods inherited from NSXObject

#create_new_request, #get, #get_attr_text_from_xml, #initialize, #inject_xml, #post, #put, #render_template, #strip_xml_root_pi

Constructor Details

This class inherits a constructor from RubyNsxCli::NSXObject

Instance Method Details

#check_virtual_wire_exists(name, scope_id) ⇒ String

Checks if a virtaul wire exists by querying the API for a list of virtual wires for the specified scope

Parameters:

  • name (String)

    Name of the virtualwire

  • scope_id (String)

    vdnscope to check for the virtualwire

Returns:

  • (String)

    virtualwire id if it exists; otherwise nil



65
66
67
68
69
70
71
72
# File 'lib/ruby_nsx_cli/nsx_objects/nsx_virtualwire.rb', line 65

def check_virtual_wire_exists(name, scope_id)
  api_endpoint = "/api/2.0/vdn/scopes/#{scope_id}/virtualwires?startindex=0&pagesize=1000"
  xml = get(api_endpoint)
  doc = Nokogiri::XML(xml)
  virtualwires = doc.css('virtualWire')
  virtualwires.map {|vwire| return vwire.at('objectId').text if vwire.at('name').text == name}
  return nil
end

#create(name:, scope_id:, description:, tenant_id:, control_plane_mode:) ⇒ String

Creates an NSX virtualwire with the provided arguments

Parameters:

  • name (String)

    Name of the virtualwire

  • scope_id (String)

    vdnscope on which the virtualwire will be added to

  • description (String)

    Optional description for the virtualwire

  • tenant_id (String)
  • control_plane_mode (String)

    one of ‘UNICAST_MODE’, ‘MULTICAST_MODE’, ‘HYBRID_MODE’. Defaults to ‘UNICAST_MODE’

Returns:

  • (String)

    virtualwire ID.



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/ruby_nsx_cli/nsx_objects/nsx_virtualwire.rb', line 22

def create(name:, scope_id:, description:, tenant_id:, control_plane_mode:)

  @logger.info("Attempting to create new virtualwire (name: #{name}, scopeId: #{scope_id})")

  virtual_wire_hash = {
      :name => name,
      :description => description,
      :tenant_id => tenant_id || 'virtual wire tenant',
      :control_plane_mode => control_plane_mode || 'UNICAST_MODE'
  }

  validate_create_args(virtual_wire_hash, scope_id)

  @logger.info("Checking if virtualwire already exists (name: #{name}, scopeId: #{scope_id})")
  if (vwire_id = check_virtual_wire_exists(name, scope_id))
    @logger.info("Skipping virtualwire creation - virtual wire already exists (name: #{name}, scopeId: #{scope_id})")
    return vwire_id
  end

  @logger.info("Adding virtual wire (name: #{name}, scopeId: #{scope_id})")

  virtual_wire_obj = OpenStruct.new(virtual_wire_hash)
  api_endpoint = "/api/2.0/vdn/scopes/#{scope_id}/virtualwires"
  payload = render_template('/templates/virtualwire/virtualwire.xml.erb', virtual_wire_obj)

  post(:api_endpoint => api_endpoint, :payload => payload)
end

#delete(virtualwire_id) ⇒ String

Deletes a virtualwire

Parameters:

  • virtualwire_id (String)

    ID of the virtualwire

Returns:

  • (String)

    virtualwire ID if it exists; otherwise nil



54
55
56
57
58
# File 'lib/ruby_nsx_cli/nsx_objects/nsx_virtualwire.rb', line 54

def delete(virtualwire_id)
  raise 'You must specify the virtual wire id to be deleted' if !virtualwire_id
  api_endpoint = "/api/2.0/vdn/virtualwires/#{virtualwire_id}"
  super(:api_endpoint => api_endpoint)
end

#validate_create_args(virtual_wire_hash, scope_id) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/ruby_nsx_cli/nsx_objects/nsx_virtualwire.rb', line 75

def validate_create_args(virtual_wire_hash, scope_id)
  errors = []
  if !NSXVirtualWire::CONTROL_PLANE_MODES.include?(virtual_wire_hash[:control_plane_mode])
    errors << "Error: Control Plane Mode must be one of [#{NSXVirtualWire::CONTROL_PLANE_MODES.to_s}]"
  end

  errors << 'Error: Scope ID must be specified' if scope_id.nil?
  virtual_wire_hash.map {|k, v| errors << "Error: Argument: #{arg.to_s} must be specified" if v.nil?}
  raise errors.to_s if errors.length > 0
end