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



123
124
125
# File 'lib/rbeapi/api/switchports.rb', line 123

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



143
144
145
# File 'lib/rbeapi/api/switchports.rb', line 143

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



133
134
135
# File 'lib/rbeapi/api/switchports.rb', line 133

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
# 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
end

#getallArray

Retrieves all switchport interfaces from the running-config

Returns:

  • (Array)

    an array of switchport hashes



109
110
111
112
113
114
115
# File 'lib/rbeapi/api/switchports.rb', line 109

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

#parse_access_vlan(config) ⇒ Object



80
81
82
83
# File 'lib/rbeapi/api/switchports.rb', line 80

def parse_access_vlan(config)
  mdata = /(?<=access\svlan\s)(.+)$/.match(config)
  { access_vlan: mdata[1] }
end

#parse_mode(config) ⇒ Object



75
76
77
78
# File 'lib/rbeapi/api/switchports.rb', line 75

def parse_mode(config)
  mdata = /(?<=\s{3}switchport\smode\s)(.+)$/.match(config)
  { mode: mdata[1] }
end

#parse_trunk_allowed_vlans(config) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rbeapi/api/switchports.rb', line 90

def parse_trunk_allowed_vlans(config)
  mdata = /(?<=trunk\sallowed\svlan\s)(.+)$/.match(config)
  return { trunk_allowed_vlans: [] } unless mdata[1] != 'none'
  vlans = mdata[1].split(',')
  values = vlans.each_with_object([]) do |vlan, arry|
    if /-/ !~ vlan
      arry << vlan.to_i
    else
      range_start, range_end = vlan.split('-')
      arry.push(*Array(range_start.to_i..range_end.to_i))
    end
  end
  { trunk_allowed_vlans: values }
end

#parse_trunk_native_vlan(config) ⇒ Object



85
86
87
88
# File 'lib/rbeapi/api/switchports.rb', line 85

def parse_trunk_native_vlan(config)
  mdata = /(?<=trunk\snative\svlan\s)(.+)$/.match(config)
  { trunk_native_vlan: mdata[1] }
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



247
248
249
250
# File 'lib/rbeapi/api/switchports.rb', line 247

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



158
159
160
161
# File 'lib/rbeapi/api/switchports.rb', line 158

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



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rbeapi/api/switchports.rb', line 190

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



228
229
230
231
# File 'lib/rbeapi/api/switchports.rb', line 228

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