class Rbeapi::Client::Node

The Node object provides an instance for sending and receiving messages with a specific EOS device. The methods provided in this class allow for handling both enable mode and config mode commands.

Attributes

connection[R]
dry_run[RW]

Public Class Methods

new(connection) click to toggle source

Save the connection and set autorefresh to true.

@param connection [Rbeapi::Eapilib::EapiConnection] An instance of

EapiConnection used to send and receive eAPI formatted messages
# File lib/rbeapi/client.rb, line 302
def initialize(connection)
  @connection = connection
  @autorefresh = true
  @dry_run = false
end

Public Instance Methods

api(name, opts = {}) click to toggle source

Returns an API module for working with the active configuration of the node.

# File lib/rbeapi/client.rb, line 540
def api(name, opts = {})
  path = opts.fetch(:path, 'rbeapi/api')
  namespace = opts.fetch(:namespace, 'Rbeapi::Api')
  require "#{path}/#{name}"
  clsname = "#{namespace}::#{name.capitalize}"
  cls = Rbeapi::Utils.class_from_string(clsname)
  return cls.instance(self) if cls.respond_to?(:instance)
  cls.new(self)
end
config(commands, opts = {}) click to toggle source

The config method is a convenience method that will handling putting the switch into config mode prior to executing commands. The method will insert 'config' at the top of the command stack and then pop the empty hash from the response output before return the array to the caller.

@param commands [Array<String, Hash>] An ordered list of commands to

execute. A string in the list is an eapi command. A Hash entry in the
array consists of the following key value pairs:
  { cmd: 'eapi command', input: 'text passed into stdin for command' }

@option opts encoding [String] The encoding scheme to use for sending

and receive eAPI messages. Valid values are json and text. The
default value is json.

@option opts open_timeout [Float] Number of seconds to wait for the

eAPI connection to open.

@option opts read_timeout [Float] Number of seconds to wait for one

block of eAPI results to be read (via one read(2) call).

@return [Array<Hash>] Ordered list of output from commands.

# File lib/rbeapi/client.rb, line 362
def config(commands, opts = {})
  commands = [*commands] unless commands.respond_to?('each')

  commands.insert(0, 'configure terminal')

  if @dry_run
    puts '[rbeapi dry-run commands]'
    puts commands
  else
    response = run_commands(commands, opts)

    refresh if @autorefresh

    response.shift
    response
  end
end
enable(commands, opts = {}) click to toggle source

The enable method is a convenience method that will handling putting the switch into privilege mode prior to executing commands.

rubocop:disable Metrics/MethodLength

@param commands [Array<String, Hash>] An ordered list of commands to

execute. A string in the list is an eapi command. A Hash entry in the
array consists of the following key value pairs:
  { cmd: 'eapi command', input: 'text passed into stdin for command' }

@option opts encoding [String] The encoding scheme to use for sending

and receive eAPI messages. Valid values are json and text. The
default value is json.

@option opts open_timeout [Float] Number of seconds to wait for the

eAPI connection to open.

@option opts read_timeout [Float] Number of seconds to wait for one

block of eAPI results to be read (via one read(2) call).

@return [Array<Hash>] Ordered list of output from commands.

# File lib/rbeapi/client.rb, line 402
def enable(commands, opts = {})
  commands = [*commands] unless commands.respond_to?('each')

  encoding = opts.fetch(:encoding, 'json')
  opts[:encoding] = encoding
  strict = opts.fetch(:strict, false)

  results = []
  if strict
    responses = run_commands(commands, opts)
    responses.each_with_index do |resp, idx|
      results << make_response(commands[idx], resp, encoding)
    end
  else
    commands.each do |cmd|
      begin
        response = run_commands(cmd, opts)
        results << make_response(cmd, response.first, encoding)
      rescue Rbeapi::Eapilib::CommandError => exc
        raise unless exc.error_code == 1003
        opts[:encoding] = 'text'
        response = run_commands(cmd, opts)
        results << make_response(cmd, response.first, encoding)
      end
    end
  end
  results
end
enable_authentication(password) click to toggle source

Configures the node instance to use an enable password. EOS can be configured to require a second layer of authentication when putting the session into enable mode. The password supplied will be used to authenticate the session to enable mode if necessary.

@param password [String] The value of the enable password.

# File lib/rbeapi/client.rb, line 335
def enable_authentication(password)
  @enablepwd = password
end
get_config(opts = {}) click to toggle source

This method will retrieve the specified configuration from the node and return it in full text.

@param [Hash] opts the options to create a message with

@option opts config [String] The configuration instance to return from

the node. Valid values are 'running-config' and 'startup-config'. If
no value is specified, then 'running-config' is used.

@option opts param [String] Additional parameters to append to the

retrieving the configuration. Valid values depend on the config
file requested.

running-config params
  all         Configuration with defaults
  detail      Detail configuration with defaults
  diffs       Differences from startup-config
  interfaces  Filter config to include only the given interfaces
  sanitized   Sanitized Output
  section     Display sections containing matching commands

startup-config params
  errors      Show information about the errors in startup-config
  interfaces  Filter config to include only the given interfaces
  section     Display sections containing matching commands

@return [String] The specified configuration as text or nil if no

config is found.  When encoding is set to json, returns
a hash.
# File lib/rbeapi/client.rb, line 513
def get_config(opts = {})
  config = opts.fetch(:config, 'running-config')
  params = opts.fetch(:params, '')
  encoding = opts.fetch(:encoding, 'text')
  as_string = opts.fetch(:as_string, false)
  begin
    result = run_commands("show #{config} #{params}", encoding: encoding)
  rescue Rbeapi::Eapilib::CommandError => error
    # rubocop:disable Style/GuardClause
    if error.to_s =~ /'show (running|startup)-config'/
      return nil
    else
      raise error
    end
    # rubocop:enable Style/GuardClause
  end
  if encoding == 'json' # rubocop:disable Style/GuardClause
    return result.first
  else
    return result.first['output'].strip.split("\n") unless as_string
    result.first['output'].strip
  end
end
refresh() click to toggle source

Forces both the running-config and startup-config to be refreshed on the next call to those properties.

# File lib/rbeapi/client.rb, line 553
def refresh
  @running_config = nil
  @startup_config = nil
end
run_commands(commands, opts = {}) click to toggle source

This method will send the ordered list of commands to the destination node using the transport. It is also response for inserting enable onto the command stack and popping the enable result on the response.

@param commands [Array] The ordered list of commands to send to the

destination node.

@option opts encoding [String] The encoding scheme to use for

sending and receive eAPI requests. This argument is optional.
Valid values include json or text. The default is json.

@option opts open_timeout [Float] Number of seconds to wait for the

eAPI connection to open.

@option opts read_timeout [Float] Number of seconds to wait for one

block of eAPI results to be read (via one read(2) call).
# File lib/rbeapi/client.rb, line 466
def run_commands(commands, opts = {})
  encoding = opts.fetch(:encoding, 'json')
  commands = [*commands] unless commands.respond_to?('each')
  commands = commands.dup

  if @enablepwd
    commands.insert(0, 'cmd' => 'enable', 'input' => @enablepwd)
  else
    commands.insert(0, 'enable')
  end

  opts[:format] = encoding
  response = @connection.execute(commands, opts)
  response.shift
  response
end
running_config() click to toggle source

Provides access the nodes running-configuration. This is a lazily loaded memoized property for working with the node configuration.

@return [String] The node's running-config as a string.

# File lib/rbeapi/client.rb, line 313
def running_config
  return @running_config if @running_config
  @running_config = get_config(params: 'all', as_string: true)
end
startup_config() click to toggle source

Provides access to the nodes startup-configuration. This is a lazily loaded memoized property for working with the nodes startup config.

@return [String] The node's startup-config as a string.

# File lib/rbeapi/client.rb, line 323
def startup_config
  return @startup_config if @startup_config
  @startup_config = get_config(config: 'startup-config', as_string: true)
end

Private Instance Methods

make_response(command, result, encoding) click to toggle source

Returns a response object from a call to the enable method. This private method is an internal method to ensure consistency in the return message format.

@param command [String] The command send to the node.

@param result [Hash] The response returned from the eAPI call.

@param encoding [String] The encoding scheme used in the response

which should be either json or text.

@return [Hash] A Ruby hash object.

# File lib/rbeapi/client.rb, line 444
def make_response(command, result, encoding)
  { command: command, result: result, encoding: encoding }
end