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

#configure, #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


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

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


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

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


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

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

#get(name) ⇒ Hash

Retrieves the properies 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


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

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


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

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


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

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

#parse_mode(config) ⇒ Object


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

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

#parse_trunk_allowed_vlans(config) ⇒ Object


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

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


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

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

  • :default (Boolean)

    The value should be set to default

Returns:

  • (Boolean)

    True if the commands succeed otherwise False


256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/rbeapi/api/switchports.rb', line 256

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
  configure(cmds)
end

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

Configures the switchport mode for the specified interafce. Valid modes are access (default) or 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 to set the mode to

  • :default (Boolean)

    The value should be set to default

Returns:

  • (Boolean)

    True if the commands succeed otherwise False


156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/rbeapi/api/switchports.rb', line 156

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
  configure(cmds)
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 value option is not provided, then the allowed trunks is configured using the no keyword. If the default keyword is provied then the allowed trunks is configured using the default keywork The default optio takes precedence over the value option if both are specified

Parameters:

  • [Array] (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options

Returns:

  • (Boolean)

    returns true if the commands complete successfully


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rbeapi/api/switchports.rb', line 194

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

  unless value.is_a?(Array)
    raise ArgumentError, 'value must be an Array'
  end

  value = value.map(&:inspect).join(',') if value

  cmds = ["interface #{name}"]
  case default
  when true
    cmds << 'default switchport trunk allowed vlan'
  when false
    if value.nil?
      cmds << 'no switchport trunk allowed vlan'
    else
      cmds << 'switchport trunk allowed vlan none'
      cmds << "switchport trunk allowed vlan #{value}"
    end
  end
  configure(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

Options Hash (opts):

  • :value (string)

    The value of the trunk native vlan

  • :default (Boolean)

    The value should be set to default

Returns:

  • (Boolean)

    True if the commands succeed otherwise False


230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/rbeapi/api/switchports.rb', line 230

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
  configure(cmds)
end