Class: CFO::CloudResource
- Inherits:
-
Object
- Object
- CFO::CloudResource
- 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
-
#account ⇒ Object
A CFO::Account object.
-
#billing_codes ⇒ Object
An Array of String pairs representing all the BillingCodes associated with all this Resource’s BillingRecords.
-
#resource_id ⇒ Object
The resource ID, unique to this account and resource type.
-
#resource_type ⇒ Object
The resource type, unique this account.
Class Method Summary collapse
-
.hash_list(conditions = {}) ⇒ Array<Resource>
A ‘links-only’ list of matcing Resources, loaded from the database.
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ CloudResource
constructor
Creates a object representing a REST Resource for a billable resource.
-
#report_uri ⇒ String
A URI path to this object’s cost report.
-
#to_hash(verbose = true) ⇒ Hash
A RESTful Hash representation of this object.
-
#to_json ⇒ String
A RESTful JSON representation of this object.
-
#uri ⇒ String
A URI path to this object’s JSON representation.
Constructor Details
#initialize(attributes = {}) ⇒ CloudResource
Creates a object representing a REST Resource for a billable resource.
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
#account ⇒ Object
A CFO::Account object.
7 8 9 |
# File 'lib/cfo/resources/cloud_resource.rb', line 7 def account @account end |
#billing_codes ⇒ Object
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_id ⇒ Object
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_type ⇒ Object
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.
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::Account.new(rec.attributes.merge(:name => rec.account))) ).to_hash(false) # Don't include the full account info end end |
Instance Method Details
#report_uri ⇒ String
Returns 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.
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_json ⇒ String
Returns 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 |
#uri ⇒ String
Returns 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 |