Class: Hawkular::Inventory::Client

Inherits:
BaseClient show all
Defined in:
lib/hawkular/inventory/inventory_api.rb

Overview

Client class to interact with Hawkular Inventory

Constant Summary

Constants inherited from BaseClient

BaseClient::HawkularConnectionException, BaseClient::HawkularException

Instance Attribute Summary collapse

Attributes inherited from BaseClient

#tenants

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseClient

#admin_header, #base_64_credentials, #generate_query_params, #http_delete, #http_get, #http_post, #http_put, #normalize_entrypoint_url, #now, #url, #url_with_websocket_scheme

Methods included from ClientUtils

#hawk_escape, #hawk_escape_id

Constructor Details

#initialize(entrypoint = nil, credentials = {}, options = {}, page_size = nil) ⇒ Client

Create a new Inventory Client

Parameters:

  • entrypoint (String) (defaults to: nil)

    base url of Hawkular-inventory - e.g localhost:8080/hawkular/inventory

  • credentials (Hash{String=>String}) (defaults to: {})

    Hash of username, password, token(optional)

  • options (Hash{String=>String}) (defaults to: {})

    Additional rest client options



16
17
18
19
20
21
22
23
# File 'lib/hawkular/inventory/inventory_api.rb', line 16

def initialize(entrypoint = nil, credentials = {}, options = {}, page_size = nil)
  entrypoint = normalize_entrypoint_url entrypoint, 'hawkular/inventory'
  @entrypoint = entrypoint
  super(entrypoint, credentials, options)
  version = fetch_version_and_status['Implementation-Version']
  @version = version.scan(/\d+/).map(&:to_i)
  @page_size = page_size || 100
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



9
10
11
# File 'lib/hawkular/inventory/inventory_api.rb', line 9

def version
  @version
end

Class Method Details

.create(hash) ⇒ Object

Creates a new Inventory Client and another sub-hash containing the hash with username, password, token(optional)

Parameters:



29
30
31
32
33
34
35
# File 'lib/hawkular/inventory/inventory_api.rb', line 29

def self.create(hash)
  fail Hawkular::ArgumentError, 'no parameter ":entrypoint" given' unless hash[:entrypoint]

  hash[:credentials] ||= {}
  hash[:options] ||= {}
  Client.new(hash[:entrypoint], hash[:credentials], hash[:options], hash[:page_size])
end

Instance Method Details

#children_resources(parent_id) ⇒ Object

Get childrens of a resource

Returns:

  • Children of a resource



56
57
58
# File 'lib/hawkular/inventory/inventory_api.rb', line 56

def children_resources(parent_id)
  http_get(url('/resources/%s/children', parent_id))['results'].map { |r| Resource.new(r) }
end

#fetch_version_and_statusHash{String=>String}

Return version and status information for the used version of Hawkular-Inventory

Returns:

  • (Hash{String=>String})

    (‘Implementation-Version’, ‘Built-From-Git-SHA1’, ‘Status’)



100
101
102
# File 'lib/hawkular/inventory/inventory_api.rb', line 100

def fetch_version_and_status
  http_get('/status')
end

#parent(id) ⇒ Object

Get parent of a resource

Returns:

  • Resource the parent resource, or nil if the provided ID referred to a root resource



62
63
64
65
# File 'lib/hawkular/inventory/inventory_api.rb', line 62

def parent(id)
  hash = http_get(url('/resources/%s/parent', id))
  Resource.new(hash) if hash
end

#resource(id) ⇒ Object

Get single resource by id

Returns:

  • Resource the resource



39
40
41
42
43
44
45
# File 'lib/hawkular/inventory/inventory_api.rb', line 39

def resource(id)
  hash = http_get(url('/resources/%s', id))
  Resource.new(hash)
rescue ::Hawkular::Exception => e
  return if e.cause.is_a?(::RestClient::NotFound)
  raise
end

#resource_tree(id) ⇒ Object

Get resource by id with its complete subtree

Returns:

  • Resource the resource



49
50
51
52
# File 'lib/hawkular/inventory/inventory_api.rb', line 49

def resource_tree(id)
  hash = http_get(url('/resources/%s/tree', id))
  Resource.new(hash)
end

#resources(filter = {}) ⇒ Enumeration<Resource>

List resources

Parameters:

  • filter (Hash) (defaults to: {})

    options to filter the resource list

Options Hash (filter):

  • :root (Object)

    If truthy, only get root resources

  • :feedId (Object)

    Filter by feed id

  • :typeId (Object)

    Filter by type id

Returns:

  • (Enumeration<Resource>)

    Lazy-loaded Enumeration of resources



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/hawkular/inventory/inventory_api.rb', line 79

def resources(filter = {})
  filter[:root] = !filter[:root].nil? if filter.key? :root
  filter[:maxResults] = @page_size

  fetch_func = lambda do |offset|
    filter[:startOffSet] = offset
    filter_query = '?' + filter.keys.join('=%s&') + '=%s' unless filter.empty?
    http_get(url("/resources#{filter_query}", *filter.values))
  end
  ResultFetcher.new(fetch_func).map { |r| Resource.new(r) }
end

#resources_for_type(type) ⇒ Enumeration<Resource>

List resources for type

Returns:

  • (Enumeration<Resource>)

    Lazy-loaded Enumeration of resources



93
94
95
# File 'lib/hawkular/inventory/inventory_api.rb', line 93

def resources_for_type(type)
  resources typeId: type
end

#root_resourcesEnumeration<Resource>

List root resources

Returns:

  • (Enumeration<Resource>)

    Lazy-loaded Enumeration of resources



69
70
71
# File 'lib/hawkular/inventory/inventory_api.rb', line 69

def root_resources
  resources root: 'true'
end