Class: Hawkular::Inventory::Client

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

Overview

Client class to interact with Hawkular Inventory

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_with_websocket_scheme

Methods included from ClientUtils

#hawk_escape, #hawk_escape_id

Constructor Details

#initialize(entrypoint = nil, credentials = {}, options = {}) ⇒ 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



24
25
26
27
28
29
30
# File 'lib/hawkular/inventory/inventory_api.rb', line 24

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

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version



17
18
19
# File 'lib/hawkular/inventory/inventory_api.rb', line 17

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:



36
37
38
39
40
41
# File 'lib/hawkular/inventory/inventory_api.rb', line 36

def self.create(hash)
  fail 'no parameter ":entrypoint" given' unless hash[:entrypoint]
  hash[:credentials] ||= {}
  hash[:options] ||= {}
  Client.new(hash[:entrypoint], hash[:credentials], hash[:options])
end

Instance Method Details

#feed_cp(feed_id) ⇒ Object



301
302
303
# File 'lib/hawkular/inventory/inventory_api.rb', line 301

def feed_cp(feed_id)
  CanonicalPath.new(tenant_id: @tenant, feed_id: hawk_escape_id(feed_id))
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')



297
298
299
# File 'lib/hawkular/inventory/inventory_api.rb', line 297

def fetch_version_and_status
  http_get('/status')
end

#get_config_data_for_resource(resource_path) ⇒ Hash<String,Object] Hash with additional data

Retrieve runtime properties for the passed resource

Parameters:

  • resource_path (String)

    Canonical path of the resource to read properties from.

Returns:

  • (Hash<String,Object] Hash with additional data)

    Hash<String,Object] Hash with additional data



138
139
140
141
142
143
# File 'lib/hawkular/inventory/inventory_api.rb', line 138

def get_config_data_for_resource(resource_path)
  path = CanonicalPath.parse_if_string(resource_path)
  raw_hash = get_raw_entity_hash(path)
  return {} unless raw_hash
  { 'value' => fetch_properties(raw_hash) }
end

#get_metric_type(metric_type_path) ⇒ Object

Return the metric type object for the passed path

Parameters:

  • metric_type_path (String)

    Canonical path of the metric type to fetch.



250
251
252
253
254
255
256
257
258
259
# File 'lib/hawkular/inventory/inventory_api.rb', line 250

def get_metric_type(metric_type_path)
  path = CanonicalPath.parse_if_string(metric_type_path)
  raw_hash = get_raw_entity_hash(path)
  unless raw_hash
    exception = HawkularException.new("Metric type not found: #{metric_type_path}")
    fail exception
  end
  entity_hash = entity_json_to_hash(-> (_) { path }, raw_hash, false)
  MetricType.new(entity_hash)
end

#get_resource(resource_path, fetch_properties = true) ⇒ Object

Return the resource object for the passed path

Parameters:

  • resource_path (String)

    Canonical path of the resource to fetch.

  • fetch_properties (Boolean) (defaults to: true)

    Should the resource config data be fetched?



224
225
226
227
228
229
230
231
232
233
# File 'lib/hawkular/inventory/inventory_api.rb', line 224

def get_resource(resource_path, fetch_properties = true)
  path = CanonicalPath.parse_if_string(resource_path)
  raw_hash = get_raw_entity_hash(path)
  unless raw_hash
    exception = HawkularException.new("Resource not found: #{resource_path}")
    fail exception
  end
  entity_hash = entity_json_to_hash(-> (_) { path }, raw_hash, fetch_properties)
  Resource.new(entity_hash)
end

#get_resource_type(resource_type_path) ⇒ Object

Return the resource type object for the passed path

Parameters:

  • resource_type_path (String)

    Canonical path of the resource type to fetch.



237
238
239
240
241
242
243
244
245
246
# File 'lib/hawkular/inventory/inventory_api.rb', line 237

def get_resource_type(resource_type_path)
  path = CanonicalPath.parse_if_string(resource_type_path)
  raw_hash = get_raw_entity_hash(path)
  unless raw_hash
    exception = HawkularException.new("Resource type not found: #{resource_type_path}")
    fail exception
  end
  entity_hash = entity_json_to_hash(-> (_) { path }, raw_hash, false)
  ResourceType.new(entity_hash)
end

#list_child_resources(parent_res_path, recursive = false) ⇒ Array<Resource>

Obtain the child resources of the passed resource. In case of a WildFly server, those would be Datasources, Deployments and so on.

Parameters:

  • parent_res_path (String)

    Canonical path of the resource to obtain children from.

  • recursive (Boolean) (defaults to: false)

    Whether to fetch also all the children of children of …

Returns:

  • (Array<Resource>)

    List of resources that are children of the given parent resource. Can be empty



151
152
153
154
155
156
157
# File 'lib/hawkular/inventory/inventory_api.rb', line 151

def list_child_resources(parent_res_path, recursive = false)
  path = CanonicalPath.parse_if_string(parent_res_path)
  feed_id = path.feed_id
  fail 'Feed id must be given' unless feed_id
  entity_hash = get_raw_entity_hash(path)
  extract_child_resources([], path.to_s, entity_hash, recursive) if entity_hash
end

#list_feedsArray<String>

List feeds in the system

Returns:

  • (Array<String>)

    List of feed ids



45
46
47
48
49
# File 'lib/hawkular/inventory/inventory_api.rb', line 45

def list_feeds
  ret = http_get('/strings/tags/module:inventory,feed:*')
  return [] unless ret.key? 'feed'
  ret['feed']
end

#list_metric_types(feed_id) ⇒ Array<MetricType>

List metric types for the given feed

Parameters:

  • feed_id (String)

    The id of the feed the type lives under

Returns:

  • (Array<MetricType>)

    List of types, that can be empty



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/hawkular/inventory/inventory_api.rb', line 72

def list_metric_types(feed_id)
  fail 'Feed id must be given' unless feed_id
  feed_path = feed_cp(feed_id)
  response = http_post(
    '/strings/raw/query',
    fromEarliest: true,
    order: 'DESC',
    tags: "#{feed_path.to_tags},type:mt")
  structures = extract_structures_from_body(response)
  structures.map do |mt|
    root_hash = entity_json_to_hash(-> (id) { feed_path.metric_type(id) }, mt['inventoryStructure'], false)
    MetricType.new(root_hash)
  end
end

#list_metrics_for_metric_type(metric_type_path) ⇒ Array<Metric>

List the metrics for the passed metric type. If feed is not passed in the path, all the metrics across all the feeds of a given type will be retrieved

Parameters:

  • metric_type_path (String)

    Canonical path of the resource type to look for. Can be obtained from MetricType.path. Must not be nil. The tenant_id in the canonical path doesn't have to be there.

Returns:

  • (Array<Metric>)

    List of metrics. Can be empty



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/hawkular/inventory/inventory_api.rb', line 164

def list_metrics_for_metric_type(metric_type_path)
  path = CanonicalPath.parse_if_string(metric_type_path)
  fail 'Feed id must be given' unless path.feed_id
  fail 'Metric type id must be given' unless path.metric_type_id
  feed_id = URI.unescape(path.feed_id)
  metric_type_id = URI.unescape(path.metric_type_id)

  feed_path = feed_cp(feed_id)
  escaped_for_regex = Regexp.quote("|#{metric_type_id}|")
  response = http_post(
    '/strings/raw/query',
    fromEarliest: true,
    order: 'DESC',
    tags: "#{feed_path.to_tags},type:r,mtypes:.*#{escaped_for_regex}.*")
  structures = extract_structures_from_body(response)
  return [] if structures.empty?

  # Now find each collected resource path in their belonging InventoryStructure
  metric_type = get_metric_type(path)
  extract_metrics_for_type(structures, feed_path, metric_type)
end

#list_metrics_for_resource(resource_path, filter = {}) ⇒ Array<Metric>

List metric (definitions) for the passed resource. It is possible to filter down the

result by a filter to only return a subset. The

Examples:

# Filter by type and match on metrics id
client.list_metrics_for_resource(wild_fly, type: 'GAUGE', match: 'Metrics~Heap')
# Filter by type only
client.list_metrics_for_resource(wild_fly, type: 'COUNTER')
# Don't filter, return all metric definitions
client.list_metrics_for_resource(wild_fly)

Parameters:

  • resource_path (String)

    Canonical path of the resource.

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

    for 'type' and 'match' Metric type can be one of 'GAUGE', 'COUNTER', 'AVAILABILITY'. If a key is missing it will not be used for filtering

Returns:

  • (Array<Metric>)

    List of metrics that can be empty.



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/hawkular/inventory/inventory_api.rb', line 200

def list_metrics_for_resource(resource_path, filter = {})
  path = CanonicalPath.parse_if_string(resource_path)
  raw_hash = get_raw_entity_hash(path)
  return [] unless raw_hash
  to_filter = []
  if (raw_hash.key? 'children') && (raw_hash['children'].key? 'metric') && !raw_hash['children']['metric'].empty?
    # Need to merge metric type info that we must grab from another place
    metric_types = list_metric_types(URI.unescape(path.feed_id))
    metric_types_index = {}
    metric_types.each { |mt| metric_types_index[mt.path] = mt }
    to_filter = raw_hash['children']['metric'].map do |m|
      metric_data = m['data']
      metric_data['path'] = "#{path}/m;#{metric_data['id']}"
      metric_type = metric_types_index[metric_data['metricTypePath']]
      Metric.new(metric_data, metric_type) if metric_type
    end
    to_filter = to_filter.select { |m| m }
  end
  filter_entities(to_filter, filter)
end

#list_operation_definitions(resource_type_path) ⇒ Array<String>

List operation definitions (types) for a given resource type

Parameters:

  • resource_type_path (String)

    canonical path of the resource type entity

Returns:

  • (Array<String>)

    List of operation type ids



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/hawkular/inventory/inventory_api.rb', line 264

def list_operation_definitions(resource_type_path)
  path = CanonicalPath.parse_if_string(resource_type_path)
  fail 'Missing feed_id in resource_type_path' unless path.feed_id
  fail 'Missing resource_type_id in resource_type_path' unless path.resource_type_id
  response = http_post(
    '/strings/raw/query',
    fromEarliest: true,
    order: 'DESC',
    tags: path.to_tags)
  structures = extract_structures_from_body(response)
  res = {}
  structures.map { |rt| rt['inventoryStructure'] }
    .select { |rt| rt['children'] && rt['children']['operationType'] }
    .flat_map { |rt| rt['children']['operationType'] }
    .each do |ot|
    hash = optype_json_to_hash(ot)
    od = OperationDefinition.new hash
    res[od.name] = od
  end
  res
end

#list_operation_definitions_for_resource(resource_path) ⇒ Array<String>

List operation definitions (types) for a given resource

Parameters:

  • resource_path (String)

    canonical path of the resource entity

Returns:

  • (Array<String>)

    List of operation type ids



289
290
291
292
# File 'lib/hawkular/inventory/inventory_api.rb', line 289

def list_operation_definitions_for_resource(resource_path)
  resource = get_resource(resource_path, false)
  list_operation_definitions(resource.type_path)
end

#list_resource_types(feed_id) ⇒ Array<ResourceType>

List resource types for the given feed

Parameters:

  • feed_id (String)

    The id of the feed the type lives under

Returns:

  • (Array<ResourceType>)

    List of types, that can be empty



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hawkular/inventory/inventory_api.rb', line 54

def list_resource_types(feed_id)
  fail 'Feed id must be given' unless feed_id
  feed_path = feed_cp(feed_id)
  response = http_post(
    '/strings/raw/query',
    fromEarliest: true,
    order: 'DESC',
    tags: "#{feed_path.to_tags},type:rt")
  structures = extract_structures_from_body(response)
  structures.map do |rt|
    root_hash = entity_json_to_hash(-> (id) { feed_path.resource_type(id) }, rt['inventoryStructure'], false)
    ResourceType.new(root_hash)
  end
end

#list_resources_for_feed(feed_id, fetch_properties = false, filter = {}) ⇒ Array<Resource>

Return all resources for a feed

Parameters:

  • feed_id (String)

    Id of the feed that hosts the resources

  • fetch_properties (Boolean) (defaults to: false)

    Should the config data be fetched too

Returns:

  • (Array<Resource>)

    List of resources, which can be empty.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/hawkular/inventory/inventory_api.rb', line 91

def list_resources_for_feed(feed_id, fetch_properties = false, filter = {})
  fail 'Feed id must be given' unless feed_id
  feed_path = feed_cp(feed_id)
  response = http_post(
    '/strings/raw/query',
    fromEarliest: true,
    order: 'DESC',
    tags: "#{feed_path.to_tags},type:r")
  structures = extract_structures_from_body(response)
  to_filter = structures.map do |r|
    root_hash = entity_json_to_hash(-> (id) { feed_path.down(id) }, r['inventoryStructure'], fetch_properties)
    Resource.new(root_hash)
  end
  filter_entities(to_filter, filter)
end

#list_resources_for_type(resource_type_path, fetch_properties = false) ⇒ Array<Resource>

List the resources for the passed resource type. The representation for resources under a feed are sparse and additional data must be retrieved separately. It is possible though to also obtain runtime properties by setting #fetch_properties to true.

Parameters:

  • resource_type_path (String)

    Canonical path of the resource type. Can be obtained from ResourceType.path. Must not be nil. The tenant_id in the canonical path doesn't have to be there.

  • fetch_properties (Boolean) (defaults to: false)

    Shall additional runtime properties be fetched?

Returns:

  • (Array<Resource>)

    List of resources. Can be empty



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/hawkular/inventory/inventory_api.rb', line 114

def list_resources_for_type(resource_type_path, fetch_properties = false)
  path = CanonicalPath.parse_if_string(resource_type_path)
  fail 'Feed id must be given' unless path.feed_id
  fail 'Resource type must be given' unless path.resource_type_id

  # Fetch metrics by tag
  feed_path = feed_cp(URI.unescape(path.feed_id))
  resource_type_id = URI.unescape(path.resource_type_id)
  escaped_for_regex = Regexp.quote("|#{resource_type_id}|")
  response = http_post(
    '/strings/raw/query',
    fromEarliest: true,
    order: 'DESC',
    tags: "#{feed_path.to_tags},type:r,restypes:.*#{escaped_for_regex}.*")
  structures = extract_structures_from_body(response)
  return [] if structures.empty?

  # Now find each collected resource path in their belonging InventoryStructure
  extract_resources_for_type(structures, feed_path, resource_type_id, fetch_properties)
end