Class: CvpClient
- Inherits:
-
Object
- Object
- CvpClient
- Defined in:
- lib/cvprac/client.rb
Overview
Provide simplified RESTful methods to access Arista CloudVision Portal
Establish and maintain connections with Arista CloudVision Portal servers, providing basic RESTful methods which handle session, cookie, and reconnects behind the scenes.
Constant Summary collapse
- NUM_RETRY_REQUESTS =
Maximum number of times to retry a get or post to the same CVP node.
3
Instance Attribute Summary collapse
-
#agent ⇒ String
Agent is the first part of the complete User-Agent.
-
#api ⇒ Object
An instance of CvpApi.
-
#connect_timeout ⇒ Fixnum
Max number of seconds before failing an HTTP connect.
-
#cookies ⇒ HTTP::CookieJar
readonly
request.
-
#file_log_level ⇒ Object
Returns the value of attribute file_log_level.
-
#headers ⇒ Hash
HTTP headers sent with each request.
-
#logger.level ⇒ Object
logger severity level: Logger::DEBUG < Logger::INFO < Logger::WARN < Logger::ERROR < Logger::FATAL.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#port ⇒ Fixnum
TCP port used for connections.
-
#protocol ⇒ String
‘http’ or ‘https’.
-
#ssl_verify_mode ⇒ Object
OpenSSL::SSL::VERIFY_NONE or OpenSSL::SSL::VERIFY_PEER.
RESTful methods collapse
-
#get(endpoint, **opts) ⇒ JSON
Send an HTTP GET request with session data and return the response.
-
#post(endpoint, **opts) ⇒ Net::HTTP Response
Send an HTTP POST request with session data and return the response.
Instance Method Summary collapse
-
#connect(nodes, username, password, **opts) ⇒ Object
Connect to one or more CVP nodes.
-
#initialize(**opts) ⇒ CvpClient
constructor
Initialize a new CvpClient object.
-
#log(severity = Logger::INFO, msg = nil) ⇒ Object
Log message to all configured loggers.
Constructor Details
#initialize(**opts) ⇒ CvpClient
Initialize a new CvpClient object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/cvprac/client.rb', line 146 def initialize(**opts) opts = { logger: 'cvprac', syslog: false, filename: nil, file_log_level: Logger::INFO }.merge(opts) @agent = File.basename($PROGRAM_NAME) @agent_full = "#{@agent} (#{RUBY_PLATFORM}) "\ "cvprac-rb/#{Cvprac::VERSION}" @authdata = nil @connect_timeout = nil @cookies = HTTP::CookieJar.new @error_msg = nil @file_log_level = opts[:file_log_level] @headers = { 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'User-agent' => @agent_full } @node_count = nil @node_pool = nil @nodes = nil @port = nil @protocol = nil @session = nil # OpenSSL::SSL::VERIFY_NONE or OpenSSL::SSL::VERIFY_PEER @ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE @url_prefix = nil if opts[:filename] == 'STDOUT' @logstdout = Logger.new(STDOUT) @logstdout.level = @file_log_level else unless opts[:filename].nil? @logfile = Logger.new(opts[:filename]) @logfile.level = @file_log_level end end @syslog = Syslog::Logger.new(opts[:filename]) if opts[:syslog] # Instantiate the CvpApi class @api = CvpApi.new(self) log(Logger::INFO, 'CvpClient initialized') end |
Instance Attribute Details
#agent ⇒ String
Agent is the first part of the complete User-Agent
118 119 120 |
# File 'lib/cvprac/client.rb', line 118 def agent @agent end |
#api ⇒ Object
An instance of CvpApi
118 119 |
# File 'lib/cvprac/client.rb', line 118 attr_accessor :agent, :connect_timeout, :headers, :port, :protocol, :ssl_verify_mode, :file_log_level, :api |
#connect_timeout ⇒ Fixnum
Returns Max number of seconds before failing an HTTP connect.
118 119 |
# File 'lib/cvprac/client.rb', line 118 attr_accessor :agent, :connect_timeout, :headers, :port, :protocol, :ssl_verify_mode, :file_log_level, :api |
#cookies ⇒ HTTP::CookieJar (readonly)
request
128 129 130 |
# File 'lib/cvprac/client.rb', line 128 def @cookies end |
#file_log_level ⇒ Object
Returns the value of attribute file_log_level.
118 119 120 |
# File 'lib/cvprac/client.rb', line 118 def file_log_level @file_log_level end |
#headers ⇒ Hash
Returns HTTP headers sent with each request.
118 119 |
# File 'lib/cvprac/client.rb', line 118 attr_accessor :agent, :connect_timeout, :headers, :port, :protocol, :ssl_verify_mode, :file_log_level, :api |
#logger.level ⇒ Object
logger severity level: Logger::DEBUG < Logger::INFO < Logger::WARN < Logger::ERROR < Logger::FATAL. This allows the user to increase or decrease the logging level of the STDOUT log as needed throughout their application.
118 119 |
# File 'lib/cvprac/client.rb', line 118 attr_accessor :agent, :connect_timeout, :headers, :port, :protocol, :ssl_verify_mode, :file_log_level, :api |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
128 |
# File 'lib/cvprac/client.rb', line 128 attr_reader :cookies, :headers, :nodes |
#port ⇒ Fixnum
Returns TCP port used for connections.
118 119 |
# File 'lib/cvprac/client.rb', line 118 attr_accessor :agent, :connect_timeout, :headers, :port, :protocol, :ssl_verify_mode, :file_log_level, :api |
#protocol ⇒ String
Returns ‘http’ or ‘https’.
118 119 |
# File 'lib/cvprac/client.rb', line 118 attr_accessor :agent, :connect_timeout, :headers, :port, :protocol, :ssl_verify_mode, :file_log_level, :api |
#ssl_verify_mode ⇒ Object
OpenSSL::SSL::VERIFY_NONE or OpenSSL::SSL::VERIFY_PEER
118 119 |
# File 'lib/cvprac/client.rb', line 118 attr_accessor :agent, :connect_timeout, :headers, :port, :protocol, :ssl_verify_mode, :file_log_level, :api |
Instance Method Details
#connect(nodes, username, password, **opts) ⇒ Object
Connect to one or more CVP nodes.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/cvprac/client.rb', line 225 def connect(nodes, username, password, **opts) opts = { connect_timeout: 10, protocol: 'https', port: nil, verify_ssl: false }.merge(opts) connect_timeout = opts[:connect_timeout] protocol = opts[:protocol] port = opts[:port] @nodes = Array(nodes) # Ensure nodes is always an array @node_index = 0 @node_count = nodes.length @node_last = @node_count - 1 @node_pool = Enumerator.new do |y| loop do index = @node_index % @node_count if @node_index == @node_last @node_index = 0 else @node_index += 1 end y.yield @nodes[index] end end @authdata = { userId: username, password: password } @connect_timeout = connect_timeout @protocol = protocol if port.nil? if protocol == 'http' port = 80 elsif protocol == 'https' port = 443 else raise ArgumentError, "No default port for protocol: #{protocol}" end end @port = port @ssl_verify_mode = if opts[:verify_ssl] OpenSSL::SSL::VERIFY_PEER else OpenSSL::SSL::VERIFY_NONE end create_session(nil) raise CvpLoginError, @error_msg unless @session end |
#get(endpoint, **opts) ⇒ JSON
Send an HTTP GET request with session data and return the response.
285 286 287 288 |
# File 'lib/cvprac/client.rb', line 285 def get(endpoint, **opts) data = opts.key?(:data) ? opts[:data] : nil make_request(:get, endpoint, data: data) end |
#log(severity: Logger::INFO, msg: nil) ⇒ Object #log(severity: Logger::INFO) {|msg| ... } ⇒ Object
Log message to all configured loggers
200 201 202 203 204 205 |
# File 'lib/cvprac/client.rb', line 200 def log(severity = Logger::INFO, msg = nil) msg = yield if block_given? @logstdout.add(severity, msg) if defined? @logstdout @logfile.add(severity, msg) if defined? @logfile @syslog.add(severity, msg) if defined? @syslog end |
#post(endpoint, **opts) ⇒ Net::HTTP Response
Send an HTTP POST request with session data and return the response.
298 299 300 301 302 |
# File 'lib/cvprac/client.rb', line 298 def post(endpoint, **opts) data = opts.key?(:data) ? opts[:data] : nil body = opts.key?(:body) ? opts[:body] : nil make_request(:post, endpoint, data: data, body: body) end |