Class: Rbeapi::Api::Entity

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, opts = {}) ⇒ Entity

The Entity class provides a base class implementation for building API modules. The Entity class is typcially 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


64
65
66
# File 'lib/rbeapi/api.rb', line 64

def initialize(node, opts = {})
  @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


74
75
76
# File 'lib/rbeapi/api.rb', line 74

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:


84
85
86
# File 'lib/rbeapi/api.rb', line 84

def error
  @error
end

#nodeObject (readonly)

Returns the value of attribute node


42
43
44
# File 'lib/rbeapi/api.rb', line 42

def node
  @node
end

Class Method Details

.instance(node) ⇒ Object

The instance class method is used to create a new instance of the object. It works in conjunction with the Node.api loader method to make it easy to load classes derived from Entity

Parameters:

  • :node (Node)

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


51
52
53
# File 'lib/rbeapi/api.rb', line 51

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

Instance Method Details

#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


124
125
126
127
128
129
130
131
# File 'lib/rbeapi/api.rb', line 124

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

#get_block(text) ⇒ Object

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.


99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rbeapi/api.rb', line 99

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_end - block_start

  config[block_start, block_end]
end