Class: Rbeapi::Client::Node
- Inherits:
-
Object
- Object
- Rbeapi::Client::Node
- 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
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#dry_run ⇒ Object
Returns the value of attribute dry_run.
Instance Method Summary collapse
-
#api(name, opts = {}) ⇒ Object
Returns an API module for working with the active configuration of the node.
-
#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.
-
#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.
-
#enable_authentication(password) ⇒ Object
Configures the node instance to use an enable password.
-
#get_config(opts = {}) ⇒ String
This method will retrieve the specified configuration from the node and return it in full text.
-
#initialize(connection) ⇒ Node
constructor
Save the connection and set autorefresh to true.
-
#refresh ⇒ Object
Forces both the running-config and startup-config to be refreshed on the next call to those properties.
-
#run_commands(commands, opts = {}) ⇒ Object
This method will send the ordered list of commands to the destination node using the transport.
-
#running_config ⇒ String
Provides access the nodes running-configuration.
-
#startup_config ⇒ String
Provides access to the nodes startup-configuration.
Constructor Details
#initialize(connection) ⇒ Node
Save the connection and set autorefresh to true.
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
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
293 294 295 |
# File 'lib/rbeapi/client.rb', line 293 def connection @connection end |
#dry_run ⇒ Object
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.
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
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.
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.
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 |
#refresh ⇒ Object
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.
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_config ⇒ String
Provides access the nodes running-configuration. This is a lazily loaded memoized property for working with the node configuration.
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_config ⇒ String
Provides access to the nodes startup-configuration. This is a lazily loaded memoized property for working with the nodes startup config.
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 |