class Rbeapi::Api::StpInstances

The StpInstances class provides a class instance for working with spanning-tree instances in EOS

Constants

DEFAULT_STP_PRIORITY

Public Instance Methods

delete(inst) click to toggle source

Deletes a configured MST instance.

@param inst [String] The MST instance to delete.

@return [Boolean] True if the commands succeed otherwise False.

# File lib/rbeapi/api/stp.rb, line 237
def delete(inst)
  configure ['spanning-tree mst configuration', "no instance #{inst}",
             'exit']
end
get(inst) click to toggle source

get returns the specified stp instance config parsed from the nodes current running configuration.

@example

{
  priority: <string>
}

@param inst [String] The named stp instance to return.

@return [nil, Hash<Symbol, Object] Returns the stp instance config as

a resource hash. If the instances is not configured this method
will return a nil object.
# File lib/rbeapi/api/stp.rb, line 165
def get(inst)
  return nil unless parse_instances.include?(inst.to_s)
  response = {}
  response.merge!(parse_priority(inst))
  response
end
getall() click to toggle source

getall returns all configured stp instances parsed from the nodes running configuration. The return hash is keyed by the instance identifier value.

@example

{
  <inst>: {
    priority: <string>
  },
  <inst>: {
    priority: <string>
  },
  ...
}

@return [Hash<Symbol, Object>] Returns all configured stp instances

found in the nodes running configuration.
# File lib/rbeapi/api/stp.rb, line 190
def getall
  parse_instances.each_with_object({}) do |inst, hsh|
    values = get(inst)
    hsh[inst] = values if values
  end
end
set_priority(inst, opts = {}) click to toggle source

Configures the spanning-tree MST priority.

@param inst [String] The MST instance to configure.

@param opts [Hash] The configuration parameters for the priority.

@option opts value [string] The value to set the priority to.

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] The value should be set to default.

@return [Boolean] True if the commands succeed otherwise False.

# File lib/rbeapi/api/stp.rb, line 257
def set_priority(inst, opts = {})
  value = opts[:value]
  enable = opts.fetch(:enable, true)
  default = opts[:default] || false

  case default
  when true
    cmd = "default spanning-tree mst #{inst} priority"
  when false
    cmd = if enable
            "spanning-tree mst #{inst} priority #{value}"
          else
            "no spanning-tree mst #{inst} priority"
          end
  end
  configure cmd
end

Private Instance Methods

parse_instances() click to toggle source

parse_instances will scan the nodes current configuration and extract the list of configured mst instances. Instances 0 and 1 are defined by default in the switch config and are always returned, even if not visible in the 'spanning-tree mst configuration' config section.

@api private

@return [Array<String>] Returns an Array of configured stp instances.

# File lib/rbeapi/api/stp.rb, line 206
def parse_instances
  config = get_block('spanning-tree mst configuration')
  response = config.scan(/(?<=^\s{3}instance\s)\d+/)
  response.push('0', '1').uniq!
  response
end
parse_priority(inst) click to toggle source

parse_priority will scan the nodes current configuration and extract the stp priority value for the given stp instance. If the stp instance priority is not configured, the priority value will be set using DEFAULT_STP_PRIORITY. The returned hash is intended to be merged into the resource hash.

@api private

@return [Hash<Symbol, Object>] Resource hash attribute.

# File lib/rbeapi/api/stp.rb, line 224
def parse_priority(inst)
  priority_re = /(?<=^spanning-tree\smst\s#{inst}\spriority\s)(.+$)/x
  mdata = priority_re.match(config)
  { priority: mdata.nil? ? DEFAULT_STP_PRIORITY : mdata[1] }
end