Class: Triglav::Agent::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/triglav/agent/api_client.rb

Overview

This Triglav client connects to triglav API with $setting.triglav.url, and authenticates with $setting.triglav.credential, and stores the token into $setting.token_file.

Re-authenticate automatically if token is expired

require 'triglav/agent/api_client'

api_client = Triglav::Agent::ApiClient.new
resources = api_client.list_resources(uri_prefix)
resources.each do |resource|
  events = get_events(resource) # implement this!
  api_client.send_messages(events)
end

Defined Under Namespace

Classes: AuthenticationError, ConnectionError, Error

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ApiClient

Initialize TriglavClient



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/triglav/agent/api_client.rb', line 31

def initialize(opts = {})
  @opts = opts || {}
  config = TriglavClient::Configuration.new do |config|
    uri = URI.parse(triglav_url)
    config.scheme = uri.scheme
    config.host = "#{uri.host}:#{uri.port}"
    config.timeout = timeout unless timeout.nil?
    config.debugging = debugging unless debugging.nil?
    config.verify_ssl = verify_ssl unless verify_ssl.nil?
    config.verify_ssl_host = verify_ssl_host unless verify_ssl_host.nil?
    config.ssl_ca_cert = ssl_ca_cert unless ssl_ca_cert.nil?
    config.cert_file = cert_file unless cert_file.nil?
    config.key_file = key_file unless key_file.nil?
  end
  @api_client = TriglavClient::ApiClient.new(config)
  initialize_current_token
  authenticate
end

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/triglav/agent/api_client.rb', line 75

def authorized?
  @current_token.has_key?(:access_token)
end

#closeObject



50
51
52
# File 'lib/triglav/agent/api_client.rb', line 50

def close
  # typhoeus makes a new connection and disconnects it on each request
end

#list_aggregated_resources(uri_prefix) ⇒ Array of TriglavClient::ResourceEachResponse

List resources required to be monitored

Parameters:

  • uri_prefix (String)

Returns:

  • (Array of TriglavClient::ResourceEachResponse)

    array of resources

See Also:

  • TriglavClient::ResourceEachResponse


59
60
61
62
63
# File 'lib/triglav/agent/api_client.rb', line 59

def list_aggregated_resources(uri_prefix)
  $logger.debug { "ApiClient#list_aggregated_resources(#{uri_prefix.inspect})" }
  resources_api = TriglavClient::ResourcesApi.new(@api_client)
  handle_error { resources_api.list_aggregated_resources(uri_prefix) }
end

#send_messages(events) ⇒ Object

Send messages

Parameters:

  • array (Array of TriglavClient::MessageRequest)

    of event messages

See Also:

  • TriglavClient::MessageRequest


69
70
71
72
73
# File 'lib/triglav/agent/api_client.rb', line 69

def send_messages(events)
  $logger.debug { "ApiClient#send_messages(#{events.inspect})" }
  messages_api = TriglavClient::MessagesApi.new(@api_client)
  handle_error { messages_api.send_messages(events) }
end