class Rbeapi::Api::Entity

The Entity class provides a base class implementation for building API modules. The Entity class is typically not instantiated directly but serves as a super class with convenience methods used to work with the node.

Attributes

config[R]
error[R]
node[R]

Public Class Methods

instance(node) click to toggle source

Construct the node.

@param node [Node] An instance of Rbeapi::Client::Node used to

send and receive eAPI messages.
# File lib/rbeapi/api.rb, line 55
def self.instance(node)
  new(node)
end
new(node) click to toggle source

The Entity class provides a base class implementation for building API modules. The Entity class is typically not instantiated directly but serves as a super class with convenience methods used to work with the node.

@param node [Node] This should be an instance of Rbeapi::Client::Node

that is used to send and receive eAPI messages.
# File lib/rbeapi/api.rb, line 68
def initialize(node)
  @node = node
end

Public Instance Methods

command_builder(cmd, opts = {}) click to toggle source

Method called to build the correct version of a command based on the modifier options. If value option is set then it is appended to the command. If the enable option is false then the 'no' keyword is prefixed to the command. If the default value is provided and set to true, then the default keyword is used. If both options are provided, then default option takes precedence.

@param cmd [String, Array] The commands to send to the node

over the API connection to configure the system.

@param opts [Hash] The options for the command.

@option opts value [String] Optional value that is

appended to the command if set.

@option opts enable [Boolean] Prefix the command with 'no'.

Default is true.

@option opts default [Boolean] Configure the command using

the default keyword. Default is false.

@return [String] Returns built command string.

# File lib/rbeapi/api.rb, line 158
def command_builder(cmd, opts = {})
  enable = opts.fetch(:enable, true)
  default = opts.fetch(:default, false)
  cmd << " #{opts[:value]}" if opts[:value]
  return "default #{cmd}" if default
  return "no #{cmd}" unless enable
  cmd
end
configure(commands) click to toggle source

Method called to send configuration commands to the node. This method will send the commands to the node and rescue from CommandError or ConnectionError.

@param commands [String, Array] The commands to send to the node over

the API connection to configure the system.

@return [Boolean] Returns True if the commands were successful or

returns False if there was an error issuing the commands on the
node.  Use error to further investigate the cause of any errors.
# File lib/rbeapi/api.rb, line 128
def configure(commands)
  @node.config(commands)
  return true
rescue Rbeapi::Eapilib::CommandError, Rbeapi::Eapilib::ConnectionError
  return false
end
configure_interface(name, commands) click to toggle source

configure_interface sends the commands over eAPI to the destination node to configure a specific interface.

@param name [String] The interface name to apply the configuration

to. The name value must be the full interface identifier.

@param commands [Array] The list of commands to configure the

interface.

@return [Boolean] Returns true if the commands complete successfully.

# File lib/rbeapi/api.rb, line 178
def configure_interface(name, commands)
  commands = [*commands]
  commands.insert(0, "interface #{name}")
  configure commands
end
get_block(text) click to toggle source

Returns a block of configuration from the current running config as a string. The argument is used to search the config and return the text along with any child configuration statements.

@param text [String] The text to be used to find the parent line

in the nodes configuration.

@return [nil, String] Returns the block of configuration based on

the supplied argument. If the argument is not found in the
configuration, nil is returned.
# File lib/rbeapi/api.rb, line 103
def get_block(text)
  mdata = /^#{text}$/.match(config)
  return nil unless mdata
  block_start, line_end = mdata.offset(0)

  mdata = /^[^\s]/.match(config, line_end)
  return nil unless mdata

  _, block_end = mdata.offset(0)
  block_end -= block_start

  config[block_start, block_end]
end