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:



277
278
279
280
# File 'lib/rbeapi/client.rb', line 277

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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



270
271
272
# File 'lib/rbeapi/client.rb', line 270

def connection
  @connection
end

Instance Method Details

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

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



472
473
474
475
476
477
478
479
480
# File 'lib/rbeapi/client.rb', line 472

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

    An ordered list of commands to execute

  • :opts (Hash)

    a customizable set of options

Returns:

  • (Array<Hash>)

    ordered list of output from commands



330
331
332
333
334
335
336
337
338
339
340
# File 'lib/rbeapi/client.rb', line 330

def config(commands, opts = {})
  commands = [*commands] unless commands.respond_to?('each')

  commands.insert(0, 'configure')
  response = run_commands(commands, opts)

  refresh if @autorefresh

  response.shift
  response
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>)

    An ordered list of commands to execute

  • :opts (Hash)

    a customizable set of options

Returns:

  • (Array<Hash>)

    ordered list of output from commands



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/rbeapi/client.rb', line 358

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



309
310
311
# File 'lib/rbeapi/client.rb', line 309

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.

@ :opts [String] :param 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

Parameters:

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

    the options to create a message with

  • :opts (Hash)

    a customizable set of options

Returns:

  • (String)

    the specified configuration as text



460
461
462
463
464
465
466
467
# File 'lib/rbeapi/client.rb', line 460

def get_config(opts = {})
  config = opts.fetch(:config, 'running-config')
  params = opts.fetch(:params, '')
  as_string = opts.fetch(:as_string, false)
  result = run_commands("show #{config} #{params}", encoding: 'text')
  return result.first['output'].strip.split("\n") unless as_string
  result.first['output'].strip
end

#refreshObject

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



485
486
487
488
# File 'lib/rbeapi/client.rb', line 485

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)

    a customizable set of options



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/rbeapi/client.rb', line 417

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



287
288
289
290
# File 'lib/rbeapi/client.rb', line 287

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



297
298
299
300
# File 'lib/rbeapi/client.rb', line 297

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