Class: Rbeapi::Client::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Node

The Node object provies an instnace for sending and receiveing messages with a specific EOS device. The methods provided in this calss allow for handling both enable mode and config mode commands

Parameters:


259
260
261
262
# File 'lib/rbeapi/client.rb', line 259

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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection


250
251
252
# File 'lib/rbeapi/client.rb', line 250

def connection
  @connection
end

Instance Method Details

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

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


435
436
437
438
439
440
441
442
443
# File 'lib/rbeapi/client.rb', line 435

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

Returns:

  • (Array<Hash>)

    ordered list of output from commands


307
308
309
310
311
312
313
314
315
316
317
# File 'lib/rbeapi/client.rb', line 307

def config(commands)
  commands = [*commands] unless commands.respond_to?('each')

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

  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 priviledge mode prior to executing commands.

Parameters:

  • commands (Array<String>)

    An ordered list of commands to execute

  • :encoding (String)

    The encoding scheme to use for sending and receive eAPI messages. Valid values are json and text. The default value is json

Returns:

  • (Array<Hash>)

    ordered list of output from commands


329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/rbeapi/client.rb', line 329

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

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

  results = []
  if strict
    responses = run_commands(commands, encoding)
    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, encoding)
        results << make_response(cmd, response.first, encoding)
      rescue Rbeapi::Eapilib::CommandError => exc
        raise unless exc.error_code == 1003
        response = run_commands(cmd, 'text')
        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


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

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


423
424
425
426
427
428
429
430
# File 'lib/rbeapi/client.rb', line 423

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}", '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.


448
449
450
451
# File 'lib/rbeapi/client.rb', line 448

def refresh
  @running_config = nil
  @startup_config = nil
end

#run_commands(commands, encoding = 'json') ⇒ 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.

  • :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


382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/rbeapi/client.rb', line 382

def run_commands(commands, 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

  response = @connection.execute(commands, format: encoding)
  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


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

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

#startup_configString

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

Returns:

  • (String)

    The node's startup-config as a string


280
281
282
283
284
# File 'lib/rbeapi/client.rb', line 280

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