Class: Rbeapi::Eapilib::EapiConnection
- Inherits:
-
Object
- Object
- Rbeapi::Eapilib::EapiConnection
- Defined in:
- lib/rbeapi/eapilib.rb
Overview
The EapiConnection provides a base class for building eAPI connection instances with a specific transport for connecting to Arista EOS devices. This class handles sending and receiving eAPI calls using JSON-RPC. This class should not need to be directly instantiated.
Direct Known Subclasses
HttpEapiConnection, HttpLocalEapiConnection, HttpsEapiConnection, SocketEapiConnection
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#open_timeout ⇒ Object
readonly
Returns the value of attribute open_timeout.
-
#read_timeout ⇒ Object
readonly
Returns the value of attribute read_timeout.
Instance Method Summary collapse
-
#authentication(opts = {}) ⇒ Object
Configures the connection authentication values (username and password).
-
#execute(commands, opts = {}) ⇒ Array<Hash>
Executes the commands on the destination node and returns the response from the node.
-
#initialize(transport) ⇒ EapiConnection
constructor
The connection contains the transport.
-
#request(commands, opts = {}) ⇒ Hash
Generates the eAPI JSON request message.
-
#send(data, opts) ⇒ Hash
This method will send the request to the node over the specified transport and return a response message with the contents from the eAPI response.
-
#timeouts(opts = {}) ⇒ Object
Configures the connection timeout values (open_timeout and read_timeout).
Constructor Details
#initialize(transport) ⇒ EapiConnection
The connection contains the transport.
136 137 138 139 |
# File 'lib/rbeapi/eapilib.rb', line 136 def initialize(transport) @transport = transport @error = nil end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
127 128 129 |
# File 'lib/rbeapi/eapilib.rb', line 127 def error @error end |
#open_timeout ⇒ Object (readonly)
Returns the value of attribute open_timeout.
128 129 130 |
# File 'lib/rbeapi/eapilib.rb', line 128 def open_timeout @open_timeout end |
#read_timeout ⇒ Object (readonly)
Returns the value of attribute read_timeout.
129 130 131 |
# File 'lib/rbeapi/eapilib.rb', line 129 def read_timeout @read_timeout end |
Instance Method Details
#authentication(opts = {}) ⇒ Object
Configures the connection authentication values (username and password). The authentication values are used to authenticate the eAPI connection. Using authentication is only required for connections that use Http or Https transports.
154 155 156 157 |
# File 'lib/rbeapi/eapilib.rb', line 154 def authentication(opts = {}) @username = opts.fetch(:username, 'admin') @password = opts.fetch(:password, '') end |
#execute(commands, opts = {}) ⇒ Array<Hash>
Executes the commands on the destination node and returns the response from the node.
321 322 323 324 325 326 327 328 329 330 |
# File 'lib/rbeapi/eapilib.rb', line 321 def execute(commands, opts = {}) @error = nil request = request(commands, opts) response = send(request, opts) return response['result'] rescue ConnectionError, CommandError => exc exc.commands = commands @error = exc raise end |
#request(commands, opts = {}) ⇒ Hash
Generates the eAPI JSON request message.
207 208 209 210 211 212 213 214 |
# File 'lib/rbeapi/eapilib.rb', line 207 def request(commands, opts = {}) id = opts.fetch(:reqid, object_id) format = opts.fetch(:format, 'json') cmds = [*commands] params = { 'version' => 1, 'cmds' => cmds, 'format' => format } { 'jsonrpc' => '2.0', 'method' => 'runCmds', 'params' => params, 'id' => id } end |
#send(data, opts) ⇒ Hash
This method will send the request to the node over the specified transport and return a response message with the contents from the eAPI response. eAPI responds to request messages with either a success message or failure message.
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/rbeapi/eapilib.rb', line 269 def send(data, opts) request = Net::HTTP::Post.new('/command-api') request.body = JSON.dump(data) request.basic_auth @username, @password open_timeout = opts.fetch(:open_timeout, @open_timeout) read_timeout = opts.fetch(:read_timeout, @read_timeout) begin @transport.open_timeout = open_timeout @transport.read_timeout = read_timeout response = @transport.request(request) decoded = JSON(response.body) if decoded.include?('error') code = decoded['error']['code'] msg = decoded['error']['message'] raise CommandError.new(msg, code) end rescue Timeout::Error raise ConnectionError, 'unable to connect to eAPI' end decoded end |
#timeouts(opts = {}) ⇒ Object
Configures the connection timeout values (open_timeout and read_timeout). The timeout values are used for the eAPI connection.
170 171 172 173 |
# File 'lib/rbeapi/eapilib.rb', line 170 def timeouts(opts = {}) @open_timeout = opts.fetch(:open_timeout, DEFAULT_HTTP_OPEN_TIMEOUT) @read_timeout = opts.fetch(:read_timeout, DEFAULT_HTTP_READ_TIMEOUT) end |