Class: Rbeapi::Api::Entity

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

Overview

The Entity class provides a base class implementation for building API modules. The Entity class is typically not instantiated directly but serves as a super class with convenience methods used to work with the node.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Entity

The Entity class provides a base class implementation for building API modules. The Entity class is typically not instantiated directly but serves as a super class with convenience methods used to work with the node.

Parameters:

  • node (Node)

    This should be an instance of Rbeapi::Client::Node that is used to send and receive eAPI messages.



68
69
70
# File 'lib/rbeapi/api.rb', line 68

def initialize(node)
  @node = node
end

Instance Attribute Details

#configString (readonly)

Returns the running configuration from the node instance. This is a convenience method to easily access the current running config from an API module.

Returns:

  • (String)

    The current running-config from the node.



78
79
80
# File 'lib/rbeapi/api.rb', line 78

def config
  @config
end

#errorRbeapi::Eapilib::CommandError (readonly)

Provides a convenience method for access the connection error (if one exists) of the node’s connection instance.

Returns:



88
89
90
# File 'lib/rbeapi/api.rb', line 88

def error
  @error
end

#nodeObject (readonly)

Returns the value of attribute node.



48
49
50
# File 'lib/rbeapi/api.rb', line 48

def node
  @node
end

Class Method Details

.instance(node) ⇒ Object

Construct the node.

Parameters:

  • node (Node)

    An instance of Rbeapi::Client::Node used to send and receive eAPI messages.



55
56
57
# File 'lib/rbeapi/api.rb', line 55

def self.instance(node)
  new(node)
end

Instance Method Details

#command_builder(cmd, opts = {}) ⇒ String

Method called to build the correct version of a command based on the modifier options. If value option is set then it is appended to the command. If the enable option is false then the ‘no’ keyword is prefixed to the command. If the default value is provided and set to true, then the default keyword is used. If both options are provided, then default option takes precedence.

Parameters:

  • cmd (String, Array)

    The commands to send to the node over the API connection to configure the system.

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

    The options for the command.

Options Hash (opts):

  • value (String)

    Optional value that is appended to the command if set.

  • enable (Boolean)

    Prefix the command with ‘no’. Default is true.

  • default (Boolean)

    Configure the command using the default keyword. Default is false.

Returns:

  • (String)

    Returns built command string.



158
159
160
161
162
163
164
165
# File 'lib/rbeapi/api.rb', line 158

def command_builder(cmd, opts = {})
  enable = opts.fetch(:enable, true)
  default = opts.fetch(:default, false)
  cmd << " #{opts[:value]}" if opts[:value]
  return "default #{cmd}" if default
  return "no #{cmd}" unless enable
  cmd
end

#configure(commands) ⇒ Boolean

Method called to send configuration commands to the node. This method will send the commands to the node and rescue from CommandError or ConnectionError.

Parameters:

  • commands (String, Array)

    The commands to send to the node over the API connection to configure the system.

Returns:

  • (Boolean)

    Returns True if the commands were successful or returns False if there was an error issuing the commands on the node. Use error to further investigate the cause of any errors.



128
129
130
131
132
133
# File 'lib/rbeapi/api.rb', line 128

def configure(commands)
  @node.config(commands)
  return true
rescue Rbeapi::Eapilib::CommandError, Rbeapi::Eapilib::ConnectionError
  return false
end

#configure_interface(name, commands) ⇒ Boolean

configure_interface sends the commands over eAPI to the destination node to configure a specific interface.

Parameters:

  • name (String)

    The interface name to apply the configuration to. The name value must be the full interface identifier.

  • commands (Array)

    The list of commands to configure the interface.

Returns:

  • (Boolean)

    Returns true if the commands complete successfully.



178
179
180
181
182
# File 'lib/rbeapi/api.rb', line 178

def configure_interface(name, commands)
  commands = [*commands]
  commands.insert(0, "interface #{name}")
  configure commands
end

#get_block(text) ⇒ nil, String

Returns a block of configuration from the current running config as a string. The argument is used to search the config and return the text along with any child configuration statements.

Parameters:

  • text (String)

    The text to be used to find the parent line in the nodes configuration.

Returns:

  • (nil, String)

    Returns the block of configuration based on the supplied argument. If the argument is not found in the configuration, nil is returned.



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rbeapi/api.rb', line 103

def get_block(text)
  mdata = /^#{text}$/.match(config)
  return nil unless mdata
  block_start, line_end = mdata.offset(0)

  mdata = /^[^\s]/.match(config, line_end)
  return nil unless mdata

  _, block_end = mdata.offset(0)
  block_end -= block_start

  config[block_start, block_end]
end