Class: Rbeapi::Eapilib::EapiConnection

Inherits:
Object
  • Object
show all
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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport) ⇒ EapiConnection

The connection contains the transport.

Parameters:

  • :transport (Net::HTTP)

    The HTTP transport to use for sending and receive eAPI request and response messages



130
131
132
133
# File 'lib/rbeapi/eapilib.rb', line 130

def initialize(transport)
  @transport = transport
  @error = nil
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



123
124
125
# File 'lib/rbeapi/eapilib.rb', line 123

def error
  @error
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



145
146
147
148
# File 'lib/rbeapi/eapilib.rb', line 145

def authentication(opts = {})
  @username = opts.fetch(:username, 'admin')
  @password = opts.fetch(:password, '')
end

#execute(commands, opts = {}) ⇒ Object

Executes the commands on the destination node and returns the response from the node.

Parameters:

  • :commands (Array)

    The ordered list of commands to execute on the destination node.

  • :opts (Hash)

    Optional keyword arguments



302
303
304
305
306
307
308
309
310
311
# File 'lib/rbeapi/eapilib.rb', line 302

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.

Examples:

eAPI Request

{
  "jsonrpc": "2.0",
  "method": "runCmds",
  "params": {
    "version": 1,
    "cmds": [
      <commands>
    ],
    "format": [json, text],
  }
  "id": <reqid>
}

Parameters:

  • :commands (Array)

    The ordered set of commands that should be included in the eAPI request

  • :opts (Hash)

    Optional keyword arguments

Returns:

  • (Hash)

    Returns a Ruby hash of the request message that is suitable to be JSON encoded and sent to the destination node



194
195
196
197
198
199
200
201
# File 'lib/rbeapi/eapilib.rb', line 194

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.

Examples:

eAPI Response - success

{
  "jsonrpc": "2.0",
  "result": [
    {},
    {},
    {
      "warnings": [
        <message>
      ]
    },
  ],
  "id": <reqid>
}

eAPI Response - failure

{
  "jsonrpc": "2.0",
  "error": {
    "code": <int>,
    "message": <string>,
    "data": [
      {},
      {},
      {
        "errors": [
          <message>
        ]
      }
    ]
  },
  "id": <reqid>
}

Parameters:

  • :data (Hash)

    A hash containing the body of the request message. This should be a valid eAPI request message.

  • :opts (Hash)

    a customizable set of options

Returns:

  • (Hash)

    returns the response message as a Ruby hash object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/rbeapi/eapilib.rb', line 254

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']
      fail 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.

Parameters:

  • :opts (Hash)

    a customizable set of options



160
161
162
163
# File 'lib/rbeapi/eapilib.rb', line 160

def timeouts(opts = {})
  @open_timeout = opts.fetch(:open_timeout, DEFAULT_HTTP_OPEN_TIMEOUT)
  @read_timeout = opts.fetch(:read_timeout, DEFAULT_HTTP_READ_TIMEOUT)
end