Class: Hawkular::Inventory::CanonicalPath

Inherits:
Object
  • Object
show all
Includes:
ClientUtils
Defined in:
lib/hawkular/inventory/entities.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClientUtils

#hawk_escape, #hawk_escape_id

Constructor Details

#initialize(hash) ⇒ CanonicalPath

Returns a new instance of CanonicalPath



173
174
175
176
177
178
179
180
181
# File 'lib/hawkular/inventory/entities.rb', line 173

def initialize(hash)
  @tenant_id = hash[:tenant_id]
  @feed_id = hash[:feed_id]
  @environment_id = hash[:environment_id]
  @resource_type_id = hash[:resource_type_id]
  @metric_type_id = hash[:metric_type_id]
  @resource_ids = hash[:resource_ids]
  @metric_id = hash[:metric_id]
end

Instance Attribute Details

#environment_idObject (readonly)

Returns the value of attribute environment_id



167
168
169
# File 'lib/hawkular/inventory/entities.rb', line 167

def environment_id
  @environment_id
end

#feed_idObject (readonly)

Returns the value of attribute feed_id



166
167
168
# File 'lib/hawkular/inventory/entities.rb', line 166

def feed_id
  @feed_id
end

#metric_idObject (readonly)

Returns the value of attribute metric_id



169
170
171
# File 'lib/hawkular/inventory/entities.rb', line 169

def metric_id
  @metric_id
end

#metric_type_idObject (readonly)

Returns the value of attribute metric_type_id



171
172
173
# File 'lib/hawkular/inventory/entities.rb', line 171

def metric_type_id
  @metric_type_id
end

#resource_idsObject (readonly)

Returns the value of attribute resource_ids



168
169
170
# File 'lib/hawkular/inventory/entities.rb', line 168

def resource_ids
  @resource_ids
end

#resource_type_idObject (readonly)

Returns the value of attribute resource_type_id



170
171
172
# File 'lib/hawkular/inventory/entities.rb', line 170

def resource_type_id
  @resource_type_id
end

#tenant_idObject (readonly)

Returns the value of attribute tenant_id



165
166
167
# File 'lib/hawkular/inventory/entities.rb', line 165

def tenant_id
  @tenant_id
end

Class Method Details

.parse(path) ⇒ Object



183
184
185
186
# File 'lib/hawkular/inventory/entities.rb', line 183

def self.parse(path)
  fail 'CanonicalPath must not be nil or empty' if path.to_s.strip.length == 0
  CanonicalPath.new(path_to_h path)
end

.parse_if_string(path) ⇒ Object



188
189
190
# File 'lib/hawkular/inventory/entities.rb', line 188

def self.parse_if_string(path)
  path.is_a?(CanonicalPath) ? path : CanonicalPath.parse(path)
end

Instance Method Details

#==(other) ⇒ Object



232
233
234
# File 'lib/hawkular/inventory/entities.rb', line 232

def ==(other)
  self.equal?(other) || other.class == self.class && other.state == state
end

#copy_hashObject



192
193
194
195
196
# File 'lib/hawkular/inventory/entities.rb', line 192

def copy_hash
  hash = to_h
  hash[:resource_ids] = hash[:resource_ids].clone unless hash[:resource_ids].nil?
  hash
end

#down(resource) ⇒ Object

Add resource down to the current path

Returns:

  • a new CanonicalPath based on the current one



210
211
212
213
214
# File 'lib/hawkular/inventory/entities.rb', line 210

def down(resource)
  hash = copy_hash
  hash[:resource_ids] = (hash[:resource_ids] || []) << hawk_escape_id(resource)
  CanonicalPath.new(hash)
end

#metric_type(metric_type) ⇒ Object

Set metric type to the current path

Returns:

  • a new CanonicalPath based on the current one



226
227
228
229
230
# File 'lib/hawkular/inventory/entities.rb', line 226

def metric_type(metric_type)
  hash = copy_hash
  hash[:metric_type_id] = hawk_escape_id(metric_type)
  CanonicalPath.new(hash)
end

#resource_type(resource_type) ⇒ Object

Set resource type to the current path

Returns:

  • a new CanonicalPath based on the current one



218
219
220
221
222
# File 'lib/hawkular/inventory/entities.rb', line 218

def resource_type(resource_type)
  hash = copy_hash
  hash[:resource_type_id] = hawk_escape_id(resource_type)
  CanonicalPath.new(hash)
end

#stateObject (protected)



270
271
272
# File 'lib/hawkular/inventory/entities.rb', line 270

def state
  [@tenant_id, @feed_id, @environment_id, @resource_ids, @metric_id, @metric_type_id, @resource_type_id]
end

#to_hObject



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

def to_h
  {
    tenant_id: @tenant_id,
    feed_id: @feed_id,
    environment_id: environment_id,
    resource_type_id: resource_type_id,
    metric_type_id: metric_type_id,
    resource_ids: resource_ids,
    metric_id: @metric_id
  }
end

#to_sObject



248
249
250
251
252
253
254
255
256
257
# File 'lib/hawkular/inventory/entities.rb', line 248

def to_s
  ret = "/t;#{@tenant_id}"
  ret += "/f;#{@feed_id}" unless @feed_id.nil?
  ret += "/e;#{@environment_id}" unless @environment_id.nil?
  ret += "/rt;#{@resource_type_id}" unless @resource_type_id.nil?
  ret += "/mt;#{@metric_type_id}" unless @metric_type_id.nil?
  ret += resources_chunk.to_s
  ret += "/m;#{@metric_id}" unless @metric_id.nil?
  ret
end

#to_tagsObject



259
260
261
262
263
264
265
266
# File 'lib/hawkular/inventory/entities.rb', line 259

def to_tags
  fail 'Missing feed_id' if @feed_id.nil?
  tags = "module:inventory,feed:#{Regexp.quote(@feed_id)}"
  tags += ",type:rt,id:#{Regexp.quote(@resource_type_id)}" if @resource_type_id
  tags += ",type:mt,id:#{Regexp.quote(@metric_type_id)}" if @metric_type_id
  tags += ",type:r,id:#{Regexp.quote(@resource_ids[0])}" if @resource_ids && (!@resource_ids.empty?)
  tags
end

#upObject

Move up to the parent path of the resource. resource_ids set to empty array when there is no parent.

Returns:

  • CanonicalPath corresponding to the direct ancestor of the resource represented by this path object.



200
201
202
203
204
205
206
# File 'lib/hawkular/inventory/entities.rb', line 200

def up
  hash = copy_hash
  res = hash[:resource_ids] || []
  res = res.take(res.length - 1) unless res.empty?
  hash[:resource_ids] = res
  CanonicalPath.new(hash)
end