Class: Hawkular::Inventory::Client
- Inherits:
-
BaseClient
- Object
- BaseClient
- Hawkular::Inventory::Client
- Defined in:
- lib/hawkular/inventory/inventory_api.rb
Overview
Client class to interact with Hawkular Inventory
Instance Attribute Summary collapse
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Attributes inherited from BaseClient
Class Method Summary collapse
Instance Method Summary collapse
-
#create_feed(feed_id, feed_name = nil) ⇒ Object
Create a new feed.
-
#create_metric_for_resource(metric_type_path, resource_path, metric_id, metric_name = nil) ⇒ Metric
Create a Metric and associate it with a resource.
-
#create_metric_type(feed_id, metric_type_id, type = 'GAUGE', unit = 'NONE', collection_interval = 60) ⇒ MetricType
Create a new metric type for a feed.
-
#create_resource(resource_type_path, resource_id, resource_name = nil, properties = {}) ⇒ Object
Create a resource of a given type.
-
#create_resource_type(feed_id, type_id, type_name) ⇒ ResourceType
Create a new resource type.
-
#create_resource_under_resource(res_type_path, parent_res_path, resource_id, resource_name = nil, properties = {}) ⇒ Object
Create a resource of a given type under a given resource.
-
#delete_feed(feed_id) ⇒ Object
Delete the feed with the passed feed id.
-
#events(type = 'resource', action = 'created') ⇒ Object
Listen on inventory changes Allowed values: resource, metric, resourcetype, metrictype, feed, environment, operationtype, metadatapack Allowed values: created, updated, deleted, copied, registered.
-
#fetch_version_and_status ⇒ Hash{String=>String}
Return version and status information for the used version of Hawkular-Inventory.
-
#get_config_data_for_resource(resource_path) ⇒ Hash<String,Object] Hash with additional data
Retrieve runtime properties for the passed resource.
-
#get_entity(path) ⇒ Object
Retrieve a single entity from inventory by its canonical path.
-
#get_resource(resource_path, fetch_resource_config = true) ⇒ Object
Return the resource object for the passed path.
-
#get_tenant(credentials = {}) ⇒ String
deprecated
Deprecated.
this doesn't provide any value, because it merely returns the tenant ID which is known before the
-
#initialize(entrypoint = nil, credentials = {}, options = {}) ⇒ Client
constructor
Create a new Inventory Client.
-
#list_child_resources(parent_res_path, recursive = false) ⇒ Array<Resource>
Obtain the child resources of the passed resource.
-
#list_feeds ⇒ Array<String>
List feeds in the system.
-
#list_metrics_for_metric_type(metric_type_path) ⇒ Array<Metric>
List the metrics for the passed metric type.
-
#list_metrics_for_resource(resource_path, filter = {}) ⇒ Array<Metric>
List metric (definitions) for the passed resource.
-
#list_metrics_for_resource_type(resource_type_path) ⇒ Array<Metric>
List the metrics for all the resources of a given resource type.
-
#list_operation_definitions(resource_type_path) ⇒ Array<String>
List operation definitions (types) for a given resource type.
-
#list_operation_definitions_for_resource(resource_path) ⇒ Array<String>
List operation definitions (types) for a given resource.
-
#list_relationships(entity_path, named = nil) ⇒ Array<Relationship>
Obtain a list of relationships starting at the passed resource.
-
#list_relationships_for_feed(feed_id, named = nil) ⇒ Array<Relationship>
Obtain a list of relationships for the passed feed.
-
#list_resource_types(feed_id = nil) ⇒ Array<ResourceType>
List resource types.
-
#list_resources_for_feed(feed_id, fetch_properties = false, filter = {}) ⇒ Array<Resource>
Return all resources for a feed.
-
#list_resources_for_type(resource_type_path, fetch_properties = false) ⇒ Array<Resource>
List the resources for the passed resource type.
-
#no_more_events! ⇒ Object
Stop listening on inventory events.
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
Constructor Details
#initialize(entrypoint = nil, credentials = {}, options = {}) ⇒ Client
Create a new Inventory Client
22 23 24 25 26 27 28 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 22 def initialize(entrypoint = nil, credentials = {}, = {}) entrypoint = normalize_entrypoint_url entrypoint, 'hawkular/inventory' @entrypoint = entrypoint super(entrypoint, credentials, ) version = fetch_version_and_status['Implementation-Version'] @version = version.scan(/\d+/).map(&:to_i) end |
Instance Attribute Details
#version ⇒ Object (readonly)
Returns the value of attribute version
15 16 17 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 15 def version @version end |
Class Method Details
.create(hash) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 34 def self.create(hash) fail 'no parameter ":entrypoint" given' if hash[:entrypoint].nil? hash[:credentials] ||= {} hash[:options] ||= {} Client.new(hash[:entrypoint], hash[:credentials], hash[:options]) end |
Instance Method Details
#create_feed(feed_id, feed_name = nil) ⇒ Object
Create a new feed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 271 def create_feed(feed_id, feed_name = nil) feed = create_blueprint feed[:id] = feed_id feed[:name] = feed_name begin return http_post('/entity/feed', feed) rescue HawkularException => error # 409 We already exist -> that is ok if error.status_code == 409 the_feed = hawk_escape_id feed_id http_get("/entity/f;#{the_feed}") else raise end end end |
#create_metric_for_resource(metric_type_path, resource_path, metric_id, metric_name = nil) ⇒ Metric
Create a Metric and associate it with a resource.
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 448 def create_metric_for_resource(metric_type_path, resource_path, metric_id, metric_name = nil) type_path = metric_type_path.is_a?(CanonicalPath) ? metric_type_path : CanonicalPath.parse(metric_type_path) feed_id = type_path.feed_id res_path = resource_path.is_a?(CanonicalPath) ? resource_path : CanonicalPath.parse(resource_path) res_path_str = 'r;' + res_path.resource_ids.join('/r;') m = {} m['id'] = metric_id m['name'] = metric_name || metric_id m['metricTypePath'] = type_path.to_s begin http_post("/entity/f;#{feed_id}/metric", m) rescue HawkularException => error # 409 We already exist -> that is ok raise unless error.status_code == 409 end ret = http_get("/entity/f;#{feed_id}/m;#{metric_id}") the_metric = Metric.new(ret) begin rl = {} rl['otherEnd'] = the_metric.path.to_s rl['name'] = 'incorporates' http_post("/entity/f;#{feed_id}/#{res_path_str}/relationship", [rl]) rescue HawkularException => error # 409 We already exist -> that is ok raise unless error.status_code == 409 end the_metric end |
#create_metric_type(feed_id, metric_type_id, type = 'GAUGE', unit = 'NONE', collection_interval = 60) ⇒ MetricType
Create a new metric type for a feed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 395 def create_metric_type(feed_id, metric_type_id, type = 'GAUGE', unit = 'NONE', collection_interval = 60) the_feed = hawk_escape_id feed_id metric_kind = type.nil? ? 'GAUGE' : type.upcase fail "Unknown type #{metric_kind}" unless %w(GAUGE COUNTER AVAILABILITY').include?(metric_kind) mt = build_metric_type_hash(collection_interval, metric_kind, metric_type_id, unit) begin http_post("/entity/f;#{the_feed}/metricType", mt) rescue HawkularException => error # 409 We already exist -> that is ok raise unless error.status_code == 409 end new_mt = http_get("/entity/f;#{the_feed}/mt;#{metric_type_id}") MetricType.new(new_mt) end |
#create_resource(resource_type_path, resource_id, resource_name = nil, properties = {}) ⇒ Object
Create a resource of a given type. To retrieve that resource you need to call #get_resource
326 327 328 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 326 def create_resource(resource_type_path, resource_id, resource_name = nil, properties = {}) create_resource_under_resource(resource_type_path, nil, resource_id, resource_name, properties) end |
#create_resource_type(feed_id, type_id, type_name) ⇒ ResourceType
Create a new resource type
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 301 def create_resource_type(feed_id, type_id, type_name) the_feed = hawk_escape_id feed_id type = create_blueprint type[:id] = type_id type[:name] = type_name begin http_post("/entity/f;#{the_feed}/resourceType", type) rescue HawkularException => error # 409 We already exist -> that is ok raise unless error.status_code == 409 ensure the_type = hawk_escape_id type_id res = http_get("/entity/f;#{the_feed}/rt;#{the_type}") end ResourceType.new(res) end |
#create_resource_under_resource(res_type_path, parent_res_path, resource_id, resource_name = nil, properties = {}) ⇒ Object
Create a resource of a given type under a given resource. To retrieve that resource you need to call #get_resource
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 338 def create_resource_under_resource(res_type_path, parent_res_path, resource_id, resource_name = nil, properties = {}) type_path = res_type_path.is_a?(CanonicalPath) ? res_type_path : CanonicalPath.parse(res_type_path) feed_id = type_path.feed_id res = create_blueprint res[:properties] = properties res[:id] = resource_id res[:name] = resource_name res[:resourceTypePath] = type_path.to_s begin if parent_res_path.nil? res = http_post("/entity/f;#{feed_id}/resource", res) else path = parent_res_path.is_a?(CanonicalPath) ? parent_res_path : CanonicalPath.parse(parent_res_path) resource_path = 'r;' + path.resource_ids.join('/r;') res = http_post("/entity/f;#{feed_id}/#{resource_path}/resource", res) end rescue HawkularException => error # 409 We already exist -> that is ok raise unless error.status_code == 409 # Ensure we have a consistent behaviour if resource already exists Issue#180 if parent_res_path.nil? hash = type_path.to_h hash.delete(:metric_type_id) path = CanonicalPath.new(hash) end res = get_resource(path.to_resource(resource_id)).to_h end Resource.new(res) end |
#delete_feed(feed_id) ⇒ Object
Delete the feed with the passed feed id.
291 292 293 294 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 291 def delete_feed(feed_id) the_feed = hawk_escape_id feed_id http_delete("/entity/f;#{the_feed}") end |
#events(type = 'resource', action = 'created') ⇒ Object
Listen on inventory changes Allowed values: resource, metric, resourcetype, metrictype, feed, environment, operationtype, metadatapack Allowed values: created, updated, deleted, copied, registered
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 486 def events(type = 'resource', action = 'created') tenant_id = get_tenant base64_creds = ["#{@credentials[:username]}:#{@credentials[:password]}"].pack('m').delete("\r\n") = { headers: { Authorization: 'Basic ' + base64_creds, Accept: 'application/json' } } [:headers][:Hawkular-Tenant'] = tenant_id url = normalize_entrypoint_url(@entrypoint, "ws/events?tenantId=#{tenant_id}&type=#{type}&action=#{action}") url = url_with_websocket_scheme(url) @ws = WebSocket::Client::Simple.connect url, do |client| client.on :message do |msg| = JSON.parse(msg.data) entity = case type when 'resource' Resource.new() when 'resourcetype' ResourceType.new() when 'metric' Metric.new() when 'metrictype' MetricType.new() else BaseEntity.new() end yield entity end end end |
#fetch_version_and_status ⇒ Hash{String=>String}
Return version and status information for the used version of Hawkular-Inventory
529 530 531 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 529 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
122 123 124 125 126 127 128 129 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 122 def get_config_data_for_resource(resource_path) path = resource_path.is_a?(CanonicalPath) ? resource_path : CanonicalPath.parse(resource_path) resource_path = 'r;' + path.resource_ids.join('/r;') feed_id = path.feed_id http_get("/entity/f;#{feed_id}/#{resource_path}/d;configuration") rescue {} end |
#get_entity(path) ⇒ Object
Retrieve a single entity from inventory by its canonical path
195 196 197 198 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 195 def get_entity(path) c_path = path.is_a?(CanonicalPath) ? path : CanonicalPath.parse(path) http_get("/entity#{c_path}") end |
#get_resource(resource_path, fetch_resource_config = true) ⇒ Object
Return the resource object for the passed path
374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 374 def get_resource(resource_path, fetch_resource_config = true) path = resource_path.is_a?(CanonicalPath) ? resource_path : CanonicalPath.parse(resource_path) feed_id = path.feed_id res_path = 'r;' + path.resource_ids.join('/r;') res = http_get("/entity/f;#{feed_id}/#{res_path}") if fetch_resource_config p = get_config_data_for_resource(resource_path) res['properties'] ||= {} res['properties'].merge! p['value'] unless p['value'].nil? end Resource.new(res) end |
#get_tenant(credentials = {}) ⇒ String
this doesn't provide any value, because it merely returns the tenant ID which is known before the
Retrieve the tenant id for the passed credentials. If no credentials are passed, the ones from the constructor are used call anyway.
47 48 49 50 51 52 53 54 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 47 def get_tenant(credentials = {}) creds = credentials.empty? ? @credentials : credentials auth_header = { Authorization: base_64_credentials(creds) } ret = http_get('/tenant', auth_header) ret['id'] 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.
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 137 def list_child_resources(parent_res_path, recursive = false) path = parent_res_path.is_a?(CanonicalPath) ? parent_res_path : CanonicalPath.parse(parent_res_path) parent_resource_path = 'r;' + path.resource_ids.join('/r;') feed_id = path.feed_id if recursive ret = http_get("/traversal/f;#{feed_id}/#{parent_resource_path}/recursive;over=isParentOf;type=r") else ret = http_get("/traversal/f;#{feed_id}/#{parent_resource_path}/type=r") end ret.map { |r| Resource.new(r) } end |
#list_feeds ⇒ Array<String>
List feeds in the system
58 59 60 61 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 58 def list_feeds ret = http_get('/traversal/type=f') ret.map { |f| f['id'] } 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 This method may perform multiple REST calls.
206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 206 def list_metrics_for_metric_type(metric_type_path) path = metric_type_path.is_a?(CanonicalPath) ? metric_type_path : CanonicalPath.parse(metric_type_path) metric_type_id = path.metric_type_id feed_id = path.feed_id if feed_id.nil? ret = http_get("/traversal/mt;#{metric_type_id}/rl;defines/type=m") else ret = http_get("/traversal/f;#{feed_id}/mt;#{metric_type_id}/rl;defines/type=m") end ret.map { |m| Metric.new(m) } rescue [] 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
256 257 258 259 260 261 262 263 264 265 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 256 def list_metrics_for_resource(resource_path, filter = {}) path = resource_path.is_a?(CanonicalPath) ? resource_path : CanonicalPath.parse(resource_path) feed_id = path.feed_id resource_path_escaped = 'r;' + path.resource_ids.join('/r;') query = generate_query_params sort: 'id' ret = http_get("/traversal/f;#{feed_id}/#{resource_path_escaped}/rl;incorporates/type=m#{query}") to_filter = ret.map { |m| Metric.new(m) } filter_entities(to_filter, filter) end |
#list_metrics_for_resource_type(resource_type_path) ⇒ Array<Metric>
List the metrics for all the resources of a given resource type. If feed is not passed in the resource type canonical path, all the metrics across all the feeds of a resource type will be retrieved. This method may perform multiple REST calls.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 227 def list_metrics_for_resource_type(resource_type_path) path = resource_type_path.is_a?(CanonicalPath) ? resource_type_path : CanonicalPath.parse(resource_type_path) resource_type_id = path.resource_type_id feed_id = path.feed_id query = generate_query_params sort: 'id' if feed_id.nil? ret = http_get("/traversal/rt;#{resource_type_id}/rl;defines/type=r/rl;incorporates/type=m#{query}") else ret = http_get( "/traversal/f;#{feed_id}/rt;#{resource_type_id}/rl;defines/type=r/rl;incorporates/type=m#{query}") end ret.map { |m| Metric.new(m) } end |
#list_operation_definitions(resource_type_path) ⇒ Array<String>
List operation definitions (types) for a given resource type
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 418 def list_operation_definitions(resource_type_path) parsed_path = CanonicalPath.parse(resource_type_path.to_s) feed_id = parsed_path.feed_id resource_type_id = parsed_path.resource_type_id ots = http_get("/traversal/f;#{feed_id}/rt;#{resource_type_id}/type=ot") res = {} ots.each do |ot| ot_name = ERB::Util.url_encode ot['name'] pts = http_get("/traversal/f;#{feed_id}/rt;#{resource_type_id}/ot;#{ot_name}/d;parameterTypes") ot['parameters'] = pts[0]['value'] unless pts.empty? od = OperationDefinition.new ot res.store od.name, od end res end |
#list_operation_definitions_for_resource(resource_path) ⇒ Array<String>
List operation definitions (types) for a given resource
437 438 439 440 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 437 def list_operation_definitions_for_resource(resource_path) resource = get_resource(resource_path.to_s, false) list_operation_definitions(resource.type_path) end |
#list_relationships(entity_path, named = nil) ⇒ Array<Relationship>
Obtain a list of relationships starting at the passed resource
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 154 def list_relationships(entity_path, named = nil) path = entity_path.is_a?(CanonicalPath) ? entity_path : CanonicalPath.parse(entity_path) query_params = { sort: '__targetCp' } query = generate_query_params query_params if named.nil? ret = http_get("/traversal#{path}/relationships#{query}") else ret = http_get("/traversal#{path}/relationships;name=#{named}#{query}") end ret.map { |r| Relationship.new(r) } end |
#list_relationships_for_feed(feed_id, named = nil) ⇒ Array<Relationship>
Obtain a list of relationships for the passed feed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 174 def list_relationships_for_feed(feed_id, named = nil) the_feed = hawk_escape_id feed_id query_params = { sort: '__targetCp' } query = generate_query_params query_params if named.nil? ret = http_get("/traversal/f;#{the_feed}/relationships#{query}") else ret = http_get("/traversal;/f;#{the_feed}/relationships;named=#{named}#{query}") end ret.map { |r| Relationship.new(r) } rescue [] end |
#list_resource_types(feed_id = nil) ⇒ Array<ResourceType>
List resource types. If no feed_id is given all types are listed
66 67 68 69 70 71 72 73 74 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 66 def list_resource_types(feed_id = nil) if feed_id.nil? ret = http_get('/traversal/type=rt') else the_feed = hawk_escape_id feed_id ret = http_get("/traversal/f;#{the_feed}/type=rt") end ret.map { |rt| ResourceType.new(rt) } end |
#list_resources_for_feed(feed_id, fetch_properties = false, filter = {}) ⇒ Array<Resource>
Return all resources for a feed
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 80 def list_resources_for_feed(feed_id, fetch_properties = false, filter = {}) fail 'Feed id must be given' unless feed_id the_feed = hawk_escape_id feed_id ret = http_get("/traversal/f;#{the_feed}/type=r") to_filter = ret.map do |r| if fetch_properties p = get_config_data_for_resource(r['path']) r['properties'] = p['value'] end Resource.new(r) 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.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 101 def list_resources_for_type(resource_type_path, fetch_properties = false) path = resource_type_path.is_a?(CanonicalPath) ? resource_type_path : CanonicalPath.parse(resource_type_path) resource_type_id = path.resource_type_id feed_id = path.feed_id if feed_id.nil? ret = http_get("/traversal/rt;#{resource_type_id}/rl;defines/type=r") else ret = http_get("/traversal/f;#{feed_id}/rt;#{resource_type_id}/rl;defines/type=r") end ret.map do |r| if fetch_properties && !feed_id.nil? p = get_config_data_for_resource(r['path']) r['properties'] = p['value'] end Resource.new(r) end end |
#no_more_events! ⇒ Object
Stop listening on inventory events. this method closes the web socket connection
522 523 524 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 522 def no_more_events! @ws.close end |