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
# 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 if timeout
    config.debugging = debugging if debugging
  end
  @api_client = TriglavClient::ApiClient.new(config)
  initialize_current_token
  authenticate
end

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/triglav/agent/api_client.rb', line 66

def authorized?
  @current_token.has_key?(:access_token)
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


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

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


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

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