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
- #feed_cp(feed_id) ⇒ Object
-
#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_metric_type(metric_type_path) ⇒ Object
Return the metric type object for the passed path.
-
#get_resource(resource_path, fetch_properties = true) ⇒ Object
Return the resource object for the passed path.
-
#get_resource_type(resource_type_path) ⇒ Object
Return the resource type object for the passed path.
-
#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_metric_types(feed_id) ⇒ Array<MetricType>
List metric types for the given feed.
-
#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_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_resource_types(feed_id) ⇒ Array<ResourceType>
List resource types for the given feed.
-
#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.
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
24 25 26 27 28 29 30 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 24 def initialize(entrypoint = nil, credentials = {}, = {}) entrypoint = normalize_entrypoint_url entrypoint, 'hawkular/metrics' @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
17 18 19 |
# File 'lib/hawkular/inventory/inventory_api.rb', line 17 def version @version end |
Class Method Details
.create(hash) ⇒ Object
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_status ⇒ Hash{String=>String}
Return version and status information for the used version of Hawkular-Inventory
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
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
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
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
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.
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_feeds ⇒ Array<String>
List feeds in the system
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
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.},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
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.},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
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
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.) 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
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
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.},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
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.},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.
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.},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 |