class PuppetX::Eos::Switchport

The Switchport class provides a base class instance for working with logical layer-2 interfaces.

Public Class Methods

new(api) click to toggle source

Initialize instance of Switchport

@param [PuppetX::Eos::Eapi] api An instance of Eapi

@return [PuppetX::Eos::Switchport]

# File lib/puppet_x/eos/modules/switchport.rb, line 50
def initialize(api)
  @api = api
end

Public Instance Methods

create(name) click to toggle source

Creates a new logical switchport interface in EOS

@param [String] name The name of the logical interface

@return [Boolean] True if it succeeds otherwise False

# File lib/puppet_x/eos/modules/switchport.rb, line 102
def create(name)
  @api.config(["interface #{name}", 'no ip address',
               'switchport']) == [{}, {}, {}]
end
default(name) click to toggle source

Defaults a logical switchport interface in the running-config

@param [String] name The name of the logical interface

@return [Boolean] True if it succeeds otherwise False

# File lib/puppet_x/eos/modules/switchport.rb, line 123
def default(name)
  @api.config(["interface #{name}",
               'default switchport']) == [{}, {}]
end
delete(name) click to toggle source

Deletes a logical switchport interface from the running-config

@param [String] name The name of the logical interface

@return [Boolean] True if it succeeds otherwise False

# File lib/puppet_x/eos/modules/switchport.rb, line 113
def delete(name)
  @api.config(["interface #{name}", 'no switchport']) == [{}, {}]
end
get(name) click to toggle source

Retrieves the properies for a logical switchport from the running-config using eAPI

Example

{
  "name": <String>,
  "mode": [access, trunk],
  "trunk_allowed_vlans": [],
  "trunk_native_vlan": <Integer>,
  "access_vlan": <Integer>
}

@param [String] name The full name of the interface to get. The

interface name must be the full interface (ie Ethernet, not Et)

@return [Hash] a hash that includes the switchport properties

# File lib/puppet_x/eos/modules/switchport.rb, line 71
def get(name)
  result = @api.enable("show interfaces #{name} switchport",
                       format: 'text')
  output = result.first['output']
  attr_hash = { name: name }
  attr_hash[:mode] = mode_to_value output
  attr_hash[:trunk_allowed_vlans] = trunk_vlans_to_value output
  attr_hash[:trunk_native_vlan] = trunk_native_to_value output
  attr_hash[:access_vlan] = access_vlan_to_value output
  attr_hash
end
getall() click to toggle source

Retrieves all switchport interfaces from the running-config

@return [Array] an array of switchport hashes

# File lib/puppet_x/eos/modules/switchport.rb, line 87
def getall
  result = @api.enable('show interfaces')
  switchports = []
  result.first['interfaces'].map do |name, attrs|
    switchports << get(name) if attrs['forwardingModel'] == 'bridged'
  end
  switchports
end
set_access_vlan(name, opts = {}) click to toggle source

Configures the access port vlan for the specified interface. This value is only valid if the switchport mode is configure in access mode.

@param [String] name The name of the interface to configure @param [Hash] opts The configuration parameters for the interface @option opts [string] :value The value of the access vlan @option opts [Boolean] :default The value should be set to default

@return [Boolean] True if the commands succeed otherwise False

# File lib/puppet_x/eos/modules/switchport.rb, line 216
def set_access_vlan(name, opts = {})
  value = opts[:value]
  default = opts[:default] || false

  cmds = ["interface #{name}"]
  case default
  when true
    cmds << 'default switchport access vlan'
  when false
    cmds << (value.nil? ? 'no switchport access vlan' : \
                          "switchport access vlan #{value}")
  end
  @api.config(cmds) == [{}, {}]
end
set_mode(name, opts = {}) click to toggle source

Configures the switchport mode for the specified interafce. Valid modes are access (default) or trunk

@param [String] name The name of the interface to configure @param [Hash] opts The configuration parameters for the interface @option opts [string] :value The value to set the mode to @option opts [Boolean] :default The value should be set to default

@return [Boolean] True if the commands succeed otherwise False

# File lib/puppet_x/eos/modules/switchport.rb, line 138
def set_mode(name, opts = {})
  value = opts[:value]
  default = opts[:default] || false

  cmds = ["interface #{name}"]
  case default
  when true
    cmds << 'default switchport mode'
  when false
    cmds << (value.nil? ? 'no switchport mode' : \
                          "switchport mode #{value}")
  end
  @api.config(cmds) == [{}, {}]
end
set_trunk_allowed_vlans(name, opts = {}) click to toggle source

Configures the trunk port allowed vlans for the specified interface. This value is only valid if the switchport mode is configure as trunk.

@param [String] name The name of the interface to configure @param [Hash] opts The configuration parameters for the interface @option opts [string] :value The list of vlans to allow @option opts [Boolean] :default The value should be set to default

@return [Boolean] True if the commands succeed otherwise False

# File lib/puppet_x/eos/modules/switchport.rb, line 164
def set_trunk_allowed_vlans(name, opts = {})
  value = opts[:value]
  default = opts[:default] || false

  cmds = ["interface #{name}"]
  case default
  when true
    cmds << 'default switchport trunk allowed vlan'
  when false
    cmds << (value.nil? ? 'no switchport trunk allowed vlan' : \
                          "switchport trunk allowed vlan #{value}")
  end
  @api.config(cmds) == [{}, {}]
end
set_trunk_native_vlan(name, opts = {}) click to toggle source

Configures the trunk port native vlan for the specified interface. This value is only valid if the switchport mode is configure as trunk.

@param [String] name The name of the interface to configure @param [Hash] opts The configuration parameters for the interface @option opts [string] :value The value of the trunk native vlan @option opts [Boolean] :default The value should be set to default

@return [Boolean] True if the commands succeed otherwise False

# File lib/puppet_x/eos/modules/switchport.rb, line 190
def set_trunk_native_vlan(name, opts = {})
  value = opts[:value]
  default = opts[:default] || false

  cmds = ["interface #{name}"]
  case default
  when true
    cmds << 'default switchport trunk native vlan'
  when false
    cmds << (value.nil? ? 'no switchport trunk native vlan' : \
                          "switchport trunk native vlan #{value}")
  end
  @api.config(cmds) == [{}, {}]
end

Private Instance Methods

access_vlan_to_value(config) click to toggle source
# File lib/puppet_x/eos/modules/switchport.rb, line 249
def access_vlan_to_value(config)
  m = /(?<=Access Mode VLAN:\s)(?<access_vlan>\d+)/.match(config)
  return m['access_vlan'] unless m.nil?
end
mode_to_value(config) click to toggle source
# File lib/puppet_x/eos/modules/switchport.rb, line 233
def mode_to_value(config)
  m = /(?<=Operational Mode:\s)(?<mode>[[:alnum:]|\s]+)\n/.match(config)
  m['mode'] == 'static access' ? 'access' : 'trunk'
end
trunk_native_to_value(config) click to toggle source
# File lib/puppet_x/eos/modules/switchport.rb, line 244
def trunk_native_to_value(config)
  m = /(?<=Trunking Native Mode VLAN:\s)(?<trunk_vlan>\d+)/.match(config)
  return m['trunk_vlan'] unless m.nil?
end
trunk_vlans_to_value(config) click to toggle source
# File lib/puppet_x/eos/modules/switchport.rb, line 238
def trunk_vlans_to_value(config)
  m = /(?<=Trunking VLANs Enabled:\s)(?<vlans>[[[:alnum:]]+|ALL])/
      .match(config)
  return m['vlans'] unless m.nil?
end