Class: Rbeapi::Eapilib::EapiConnection
- Inherits:
-
Object
- Object
- Rbeapi::Eapilib::EapiConnection
- Defined in:
- lib/rbeapi/eapilib.rb
Direct Known Subclasses
HttpEapiConnection, HttpLocalEapiConnection, HttpsEapiConnection, SocketEapiConnection
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
Instance Method Summary collapse
-
#authentication(username, password) ⇒ Object
Configures the connection authentication values (username and and password).
-
#execute(commands, opts = {}) ⇒ Object
Executes the commands on the destination node and returns the response from the node.
-
#initialize(transport) ⇒ EapiConnection
constructor
The EapiConnection provides a base class for building eAPI connection instances with a specific transport for connecting to Arista EOS devices.
-
#request(commands, opts = {}) ⇒ Hash
Generates the eAPI JSON request message.
-
#send(data) ⇒ 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.
Constructor Details
#initialize(transport) ⇒ EapiConnection
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 over HTTP. This class should not need to be directly instantiated.
125 126 127 128 |
# File 'lib/rbeapi/eapilib.rb', line 125 def initialize(transport) @transport = transport @error = nil end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
114 115 116 |
# File 'lib/rbeapi/eapilib.rb', line 114 def error @error end |
Instance Method Details
#authentication(username, password) ⇒ Object
Configures the connection authentication values (username and 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
140 141 142 143 |
# File 'lib/rbeapi/eapilib.rb', line 140 def authentication(username, password) @username = username @password = password end |
#execute(commands, opts = {}) ⇒ Object
Executes the commands on the destination node and returns the response from the node.
270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/rbeapi/eapilib.rb', line 270 def execute(commands, opts = {}) begin @error = nil request = request(commands, opts) response = send request return response['result'] rescue ConnectionError, CommandError => exc exc.commands = commands @error = exc raise end end |
#request(commands, opts = {}) ⇒ Hash
Generates the eAPI JSON request message.
174 175 176 177 178 179 180 181 |
# File 'lib/rbeapi/eapilib.rb', line 174 def request(commands, opts = {}) id = opts.fetch(:reqid, self.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) ⇒ 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.
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/rbeapi/eapilib.rb', line 230 def send(data) request = Net::HTTP::Post.new('/command-api') request.body = JSON.dump(data) request.basic_auth @username, @password begin @transport.open_timeout = DEFAULT_HTTP_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 return decoded end |