Method: Rbeapi::Client::Node#enable

Defined in:
lib/rbeapi/client.rb

#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.



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
429
# 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