Class: Rbeapi::Api::Switchports

Inherits:
Entity
  • Object
show all
Defined in:
lib/rbeapi/api/switchports.rb

Overview

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

Instance Attribute Summary

Attributes inherited from Entity

#config, #error, #node

Instance Method Summary collapse

Methods inherited from Entity

#command_builder, #configure, #configure_interface, #get_block, #initialize, instance

Constructor Details

This class inherits a constructor from Rbeapi::Api::Entity

Instance Method Details

#create(name) ⇒ Boolean

Creates a new logical switchport interface in EOS.

Parameters:

  • name (String)

    The name of the logical interface.

Returns:

  • (Boolean)

    Returns True if it succeeds otherwise False.



203
204
205
# File 'lib/rbeapi/api/switchports.rb', line 203

def create(name)
  configure ["interface #{name}", 'no ip address', 'switchport']
end

#default(name) ⇒ Boolean

Defaults a logical switchport interface in the running-config.

Parameters:

  • name (String)

    The name of the logical interface.

Returns:

  • (Boolean)

    Returns True if it succeeds otherwise False.



223
224
225
# File 'lib/rbeapi/api/switchports.rb', line 223

def default(name)
  configure ["interface #{name}", 'default switchport']
end

#delete(name) ⇒ Boolean

Deletes a logical switchport interface from the running-config.

Parameters:

  • name (String)

    The name of the logical interface.

Returns:

  • (Boolean)

    Returns True if it succeeds otherwise False.



213
214
215
# File 'lib/rbeapi/api/switchports.rb', line 213

def delete(name)
  configure ["interface #{name}", 'no switchport']
end

#get(name) ⇒ Hash

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

Example

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

Parameters:

  • name (String)

    The full name of the interface to get. The interface name must be the full interface (ie Ethernet, not Et).

Returns:

  • (Hash)

    Returns a hash that includes the switchport properties.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rbeapi/api/switchports.rb', line 63

def get(name)
  config = get_block("interface #{name}")
  return nil unless config
  return nil if /no\sswitchport$/ =~ config

  response = {}
  response.merge!(parse_mode(config))
  response.merge!(parse_access_vlan(config))
  response.merge!(parse_trunk_native_vlan(config))
  response.merge!(parse_trunk_allowed_vlans(config))
  response.merge!(parse_trunk_groups(config))
  response
end

#getallArray

Retrieves all switchport interfaces from the running-config.

Examples:

{
  <name>: {
    mode: <string>,
    access_vlan: <string>,
    trunk_native_vlan: <string>,
    trunk_allowed_vlans: <array>,
    trunk_groups: <array>
  },
  <name>: {
    mode: <string>,
    access_vlan: <string>,
    trunk_native_vlan: <string>,
    trunk_allowed_vlans: <array>,
    trunk_groups: <array>
  },
  ...
}

Returns:

  • (Array)

    Returns an array of switchport hashes.



189
190
191
192
193
194
195
# File 'lib/rbeapi/api/switchports.rb', line 189

def getall
  interfaces = config.scan(/(?<=^interface\s)([Et|Po].+)$/)
  interfaces.each_with_object({}) do |port, hsh|
    cfg = get port.first
    hsh[port.first] = cfg if cfg
  end
end

#set_access_vlan(name, opts = {}) ⇒ Boolean

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

Parameters:

  • name (String)

    The name of the interface to configure.

  • opts (Hash) (defaults to: {})

    The configuration parameters for the interface.

Options Hash (opts):

  • value (string)

    The value of the access vlan.

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    The value should be set to default Default takes precedence over enable.

Returns:

  • (Boolean)

    Returns True if the commands succeed otherwise False.



344
345
346
347
# File 'lib/rbeapi/api/switchports.rb', line 344

def set_access_vlan(name, opts = {})
  cmd = command_builder('switchport access vlan', opts)
  configure_interface(name, cmd)
end

#set_mode(name, opts = {}) ⇒ Boolean

Configures the switchport mode for the specified interface.

Parameters:

  • name (String)

    The name of the interface to configure.

  • opts (Hash) (defaults to: {})

    The configuration parameters for the interface.

Options Hash (opts):

  • value (string)

    The value to set the mode to.

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    The value should be set to default.

Returns:

  • (Boolean)

    Returns True if the commands succeed otherwise False.



242
243
244
245
# File 'lib/rbeapi/api/switchports.rb', line 242

def set_mode(name, opts = {})
  cmd = command_builder('switchport mode', opts)
  configure_interface(name, cmd)
end

#set_trunk_allowed_vlans(name, opts = {}) ⇒ Boolean

set_trunk_allowed_vlans configures the list of vlan ids that are allowed on the specified trunk port. If the enable option is set to false, then the allowed trunks is configured using the no keyword. If the default keyword is provided then the allowed trunks is configured using the default keyword. The default option takes precedence over the enable option if both are specified.

Commands

switchport trunk allowed vlan add <value>
no switchport trunk allowed vlan
default switchport trunk allowed vlan

Parameters:

  • name (String)

    The name of the interface to configure.

  • opts (Hash) (defaults to: {})

    The configuration parameters for the interface.

  • default (Hash)

    a customizable set of options

Options Hash (opts):

  • value (Array)

    The list of vlan ids to configure on the switchport to be allowed. This value must be an array of valid vlan ids or vlan ranges.

  • enable (Boolean)

    If false then the command is negated. Default is true.

Returns:

  • (Boolean)

    Returns true if the commands complete successfully.

Since:

  • eos_version 4.13.7M



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/rbeapi/api/switchports.rb', line 278

def set_trunk_allowed_vlans(name, opts = {})
  value = opts[:value]
  enable = opts.fetch(:enable, true)
  default = opts[:default] || false

  if value
    unless value.is_a?(Array)
      raise ArgumentError, 'value must be an Array'
    end
    value = value.map(&:inspect).join(',').tr('"', '')
  end

  case default
  when true
    cmds = 'default switchport trunk allowed vlan'
  when false
    cmds = if !enable
             'no switchport trunk allowed vlan'
           else
             ["switchport trunk allowed vlan #{value}"]
           end
  end
  configure_interface(name, cmds)
end

#set_trunk_groups(name, opts = {}) ⇒ Boolean

Configures the trunk group vlans for the specified interface. Trunk groups not currently set are added and trunk groups currently configured but not in the passed in value array are removed.

Parameters:

  • name (String)

    The name of the interface to configure.

  • opts (Hash) (defaults to: {})

    The configuration parameters for the interface.

Options Hash (opts):

  • value (string)

    Set of values to configure the trunk group.

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    The value should be set to default Default takes precedence over enable.

Returns:

  • (Boolean)

    Returns True if the commands succeed otherwise False.

Raises:

  • (ArgumentError)


367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/rbeapi/api/switchports.rb', line 367

def set_trunk_groups(name, opts = {})
  default = opts.fetch(:default, false)
  if default
    return configure_interface(name, 'default switchport trunk group')
  end

  enable = opts.fetch(:enable, true)
  unless enable
    return configure_interface(name, 'no switchport trunk group')
  end

  value = opts.fetch(:value, [])
  raise ArgumentError, 'value must be an Array' unless value.is_a?(Array)

  value = Set.new value
  current_value = Set.new get(name)[:trunk_groups]

  cmds = []
  # Add trunk groups that are not currently in the list.
  value.difference(current_value).each do |group|
    cmds << "switchport trunk group #{group}"
  end

  # Remove trunk groups that are not in the new list.
  current_value.difference(value).each do |group|
    cmds << "no switchport trunk group #{group}"
  end
  configure_interface(name, cmds) unless cmds.empty?
end

#set_trunk_native_vlan(name, opts = {}) ⇒ Boolean

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

Parameters:

  • name (String)

    The name of the interface to configure.

  • opts (Hash) (defaults to: {})

    The configuration parameters for the interface.

Options Hash (opts):

  • value (string)

    The value of the trunk native vlan.

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    The value should be set to default. Default takes precedence over enable.

Returns:

  • (Boolean)

    Returns True if the commands succeed otherwise False.



321
322
323
324
# File 'lib/rbeapi/api/switchports.rb', line 321

def set_trunk_native_vlan(name, opts = {})
  cmd = command_builder('switchport trunk native vlan', opts)
  configure_interface(name, cmd)
end