Class: CFO::CloudResource

Inherits:
Object
  • Object
show all
Includes:
CloudCostTracker::Billing
Defined in:
lib/cfo/resources/cloud_resource.rb

Overview

Represents a billable asset on a cloud computing service.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ CloudResource

Creates a object representing a REST Resource for a billable resource.

Parameters:

  • attributes (Hash) (defaults to: {})
    • :account, :resource_type,

    :resource_id, or :billing_codes



22
23
24
# File 'lib/cfo/resources/cloud_resource.rb', line 22

def initialize(attributes = {})
  attributes.each {|k,v| self.send("#{k}=",v) if self.respond_to?("#{k}=")}
end

Instance Attribute Details

#accountObject

A CFO::Account object.



7
8
9
# File 'lib/cfo/resources/cloud_resource.rb', line 7

def 
  @account
end

#billing_codesObject

An Array of String pairs representing all the BillingCodes associated with all this Resource’s BillingRecords.



17
18
19
# File 'lib/cfo/resources/cloud_resource.rb', line 17

def billing_codes
  @billing_codes
end

#resource_idObject

The resource ID, unique to this account and resource type.



13
14
15
# File 'lib/cfo/resources/cloud_resource.rb', line 13

def resource_id
  @resource_id
end

#resource_typeObject

The resource type, unique this account.



10
11
12
# File 'lib/cfo/resources/cloud_resource.rb', line 10

def resource_type
  @resource_type
end

Class Method Details

.hash_list(conditions = {}) ⇒ Array<Resource>

Returns a ‘links-only’ list of matcing Resources, loaded from the database.

Returns:

  • (Array<Resource>)

    a ‘links-only’ list of matcing Resources, loaded from the database.



63
64
65
66
67
68
69
70
71
# File 'lib/cfo/resources/cloud_resource.rb', line 63

def self.hash_list(conditions = {})
  BillingRecord.select('*').group(:resource_id).where(conditions).
  collect do |rec|
    CloudResource.new(
      rec.attributes.merge('account' =>
        CFO::.new(rec.attributes.merge(:name => rec.)))
    ).to_hash(false)  # Don't include the full account info
  end
end

Instance Method Details

#report_uriString

Returns a URI path to this object’s cost report.

Returns:

  • (String)

    a URI path to this object’s cost report.



53
54
55
56
# File 'lib/cfo/resources/cloud_resource.rb', line 53

def report_uri
  "#{CFO.root}/report/for_account/#{URI.escape(@account.name)}/for_type/"+
    "#{URI.escape(@resource_type)}/for_resource/#{URI.escape(@resource_id)}"
end

#to_hash(verbose = true) ⇒ Hash

Returns a RESTful Hash representation of this object.

Parameters:

  • verbose (Booolean) (defaults to: true)

    whether to include a full representation.

Returns:

  • (Hash)

    a RESTful Hash representation of this object.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cfo/resources/cloud_resource.rb', line 28

def to_hash(verbose = true)
  extra_data = {
    'account' => {
      'id'          => @account.name,
      'service'     => @account.service,
      'provider'    => @account.provider,
      'url'         => @account.uri,
    },
    'report_url'    => report_uri,
    'billing_codes' => @billing_codes,
  }
  { # Return the resource Hash, perhaps with the account merged in
    'resource_id'   => @resource_id,
    'resource_type' => @resource_type,
    'url'           => uri,
  }.merge(verbose ? extra_data : {})
end

#to_jsonString

Returns a RESTful JSON representation of this object.

Returns:

  • (String)

    a RESTful JSON representation of this object.



59
# File 'lib/cfo/resources/cloud_resource.rb', line 59

def to_json ; to_hash.to_json end

#uriString

Returns a URI path to this object’s JSON representation.

Returns:

  • (String)

    a URI path to this object’s JSON representation



47
48
49
50
# File 'lib/cfo/resources/cloud_resource.rb', line 47

def uri
  "#{CFO.root}/accounts/#{URI.escape(@account.name)}/"+
    "#{URI.escape(@resource_type)}/#{URI.escape(@resource_id)}"
end