Class: Rbeapi::Client::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/rbeapi/client.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Node

Save the connection and set autorefresh to true.

Parameters:



301
302
303
304
305
# File 'lib/rbeapi/client.rb', line 301

def initialize(connection)
  @connection = connection
  @autorefresh = true
  @dry_run = false
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



293
294
295
# File 'lib/rbeapi/client.rb', line 293

def connection
  @connection
end

#dry_runObject

Returns the value of attribute dry_run.



294
295
296
# File 'lib/rbeapi/client.rb', line 294

def dry_run
  @dry_run
end

Instance Method Details

#api(name, opts = {}) ⇒ Object

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



537
538
539
540
541
542
543
544
545
# File 'lib/rbeapi/client.rb', line 537

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 = {}) ⇒ Array<Hash>

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.

Parameters:

  • 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' }
    
  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (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.

  • open_timeout (Float)

    Number of seconds to wait for the eAPI connection to open.

  • read_timeout (Float)

    Number of seconds to wait for one block of eAPI results to be read (via one read(2) call).

Returns:

  • (Array<Hash>)

    Ordered list of output from commands.



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/rbeapi/client.rb', line 361

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 = {}) ⇒ Array<Hash>

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

rubocop:disable Metrics/MethodLength

Parameters:

  • 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' }
    
  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (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.

  • open_timeout (Float)

    Number of seconds to wait for the eAPI connection to open.

  • read_timeout (Float)

    Number of seconds to wait for one block of eAPI results to be read (via one read(2) call).

Returns:

  • (Array<Hash>)

    Ordered list of output from commands.



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/rbeapi/client.rb', line 401

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) ⇒ Object

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.

Parameters:

  • password (String)

    The value of the enable password.



334
335
336
# File 'lib/rbeapi/client.rb', line 334

def enable_authentication(password)
  @enablepwd = password
end

#get_config(opts = {}) ⇒ String

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

Parameters:

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

    the options to create a message with

Options Hash (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.

  • 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
    

Returns:

  • (String)

    The specified configuration as text or nil if no config is found. When encoding is set to json, returns a hash.



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/rbeapi/client.rb', line 512

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
    if ( error.to_s =~ /'show (running|startup)-config'/ )
      return nil
    else
      raise error
    end
  end
  if encoding == 'json'
    return result.first
  else
    return result.first['output'].strip.split("\n") unless as_string
    result.first['output'].strip
  end
end

#refreshObject

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



550
551
552
553
# File 'lib/rbeapi/client.rb', line 550

def refresh
  @running_config = nil
  @startup_config = nil
end

#run_commands(commands, opts = {}) ⇒ Object

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.

Parameters:

  • commands (Array)

    The ordered list of commands to send to the destination node.

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

    a customizable set of options

Options Hash (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.

  • open_timeout (Float)

    Number of seconds to wait for the eAPI connection to open.

  • read_timeout (Float)

    Number of seconds to wait for one block of eAPI results to be read (via one read(2) call).



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/rbeapi/client.rb', line 465

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_configString

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

Returns:

  • (String)

    The node’s running-config as a string.



312
313
314
315
# File 'lib/rbeapi/client.rb', line 312

def running_config
  return @running_config if @running_config
  @running_config = get_config(params: 'all', as_string: true)
end

#startup_configString

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

Returns:

  • (String)

    The node’s startup-config as a string.



322
323
324
325
# File 'lib/rbeapi/client.rb', line 322

def startup_config
  return @startup_config if @startup_config
  @startup_config = get_config(config: 'startup-config', as_string: true)
end