Class: Rbeapi::Api::StpInterfaces

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

Overview

The StpInterfaces class provides a class instance for working with spanning-tree interfaces in EOS.

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

#get(name) ⇒ nil, Hash<Symbol, Object>

get returns the configured stp interfaces from the nodes running configuration as a resource hash. If the specified interface is not configured as a switchport then this method will return nil.

Examples:

{
  portfast: <boolean>,
  portfast_type: <string>,
  bpduguard: <boolean>
}

Parameters:

  • name (String)

    The interface name to return a resource for from the nodes configuration.

Returns:

  • (nil, Hash<Symbol, Object>)

    Returns the stp interface as a resource hash.



298
299
300
301
302
303
304
305
306
307
# File 'lib/rbeapi/api/stp.rb', line 298

def get(name)
  config = get_block("interface #{name}")
  return nil unless config
  return nil if /no switchport$/ =~ config
  response = {}
  response.merge!(parse_portfast(config))
  response.merge!(parse_portfast_type(config))
  response.merge!(parse_bpduguard(config))
  response
end

#getallHash<Symbol, Object>

getall returns all of the configured stp interfaces parsed from the nodes current running configuration. The returned hash is keyed by the interface name.

Examples:

{
  <name>: {
    portfast: <boolean>,
    portfast_type: <string>,
    bpduguard: <boolean>
  },
  <name>: {
    portfast: <boolean>,
    portfast_type: <string>,
    bpduguard: <boolean>
  },
  ...
}

Returns:

  • (Hash<Symbol, Object>)

    Returns the stp interfaces config as a resource hash from the nodes running configuration.



331
332
333
334
335
336
337
338
# File 'lib/rbeapi/api/stp.rb', line 331

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

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

Configures the interface bpdu guard value

Parameters:

  • name (String)

    The name of the interface to configure.

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

    The configuration parameters for bpduguard.

Options Hash (opts):

  • value (Boolean)

    The value to set bpduguard.

  • enable (Boolean)

    If false then the bpduguard is disabled. If true then the bpduguard is enabled. Default is true.

  • default (Boolean)

    The value should be set to default.

Returns:

  • (Boolean)

    True if the commands succeed otherwise False.



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/rbeapi/api/stp.rb', line 458

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

  case default
  when true
    cmds = 'default spanning-tree bpduguard'
  when false
    cmds = if enable
             'spanning-tree bpduguard enable'
           else
             'spanning-tree bpduguard disable'
           end
  end
  configure_interface(name, cmds)
end

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

Configures the interface portfast value.

Parameters:

  • name (String)

    The name of the interface to configure.

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

    The configuration parameters for portfast.

Options Hash (opts):

  • value (Boolean)

    The value to set portfast.

  • 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.



403
404
405
406
# File 'lib/rbeapi/api/stp.rb', line 403

def set_portfast(name, opts = {})
  cmd = command_builder('spanning-tree portfast', opts)
  configure_interface(name, cmd)
end

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

Configures the interface portfast type value

Parameters:

  • name (String)

    The name of the interface to configure.

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

    The configuration parameters for portfast type.

Options Hash (opts):

  • value (String)

    The value to set portfast type to. The value must be set for calls to this method.

  • 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.

Raises:

  • (ArgumentError)


424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/rbeapi/api/stp.rb', line 424

def set_portfast_type(name, opts = {})
  value = opts[:value]
  raise ArgumentError, 'value must be set' unless value
  enable = opts.fetch(:enable, true)
  default = opts[:default] || false

  case default
  when true
    cmds = "default spanning-tree portfast #{value}"
  when false
    cmds = if enable
             "spanning-tree portfast #{value}"
           else
             "no spanning-tree portfast #{value}"
           end
  end
  configure_interface(name, cmds)
end