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)

    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)

    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)

    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>
}

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)

    a hash that includes the switchport properties



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

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)

    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)

    True if the commands succeed otherwise False



327
328
329
330
# File 'lib/rbeapi/api/switchports.rb', line 327

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)

    True if the commands succeed otherwise False



238
239
240
241
# File 'lib/rbeapi/api/switchports.rb', line 238

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

Parameters:

  • name (String)

    The name of the interface to configure

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

    The configuration parameters for the interface

  • pts (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options

Options Hash (opts):

  • :enable (Boolean)

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

Returns:

  • (Boolean)

    returns true if the commands complete successfully



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/rbeapi/api/switchports.rb', line 270

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

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

  case default
  when true
    cmds = 'default switchport trunk allowed vlan'
  when false
    if !enable
      cmds = 'no switchport trunk allowed vlan'
    else
      cmds = ['switchport trunk allowed vlan none',
              "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)

    True if the commands succeed otherwise False



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/rbeapi/api/switchports.rb', line 346

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, [])
  fail 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) if cmds.length > 0
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

  • :opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :value (string)

    The value of the trunk native vlan

  • :default (Boolean)

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

Returns:

  • (Boolean)

    True if the commands succeed otherwise False



308
309
310
311
# File 'lib/rbeapi/api/switchports.rb', line 308

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