Class: CloudCostTracker::Billing::ResourceBillingPolicy
- Inherits:
-
Object
- Object
- CloudCostTracker::Billing::ResourceBillingPolicy
- Includes:
- CloudCostTracker
- Defined in:
- lib/cloud_cost_tracker/billing/resource_billing_policy.rb
Overview
Implements the logic for billing a single resource. All Billing Policies should inherit from this class, and define #get_cost_for_duration
Direct Known Subclasses
AWS::Elasticache::ClusterBillingPolicy, AWS::RDS::ServerBillingPolicy, AWS::RDS::ServerStorageBillingPolicy, Compute::AWS::ServerBillingPolicy, Compute::AWS::SnapshotBillingPolicy, Compute::AWS::VolumeBillingPolicy, Storage::AWS::DirectoryBillingPolicy
Constant Summary
Constants included from CloudCostTracker
Instance Method Summary collapse
-
#billing_type ⇒ String
Returns the default billing type for this policy.
-
#get_cost_for_duration(resource, duration) ⇒ Float
Returns the cost for a particular resource over some duration in seconds.
-
#initialize(options = {}) ⇒ ResourceBillingPolicy
constructor
Don’t override this method - use #setup instead for one-time behavior.
-
#setup(resources) ⇒ Object
An initializer called by the framework once per billling cycle.
-
#write_billing_record_for(resource, hourly_rate, total, start_time, end_time) ⇒ Object
Creates or Updates a BillingRecord for this BillingPolicy’s resource.
Methods included from CloudCostTracker
account_coding_class, connect_to_database, create_billing_agents, create_coding_agents, env, read_accounts
Constructor Details
#initialize(options = {}) ⇒ ResourceBillingPolicy
Don’t override this method - use #setup instead for one-time behavior
30 31 32 |
# File 'lib/cloud_cost_tracker/billing/resource_billing_policy.rb', line 30 def initialize(={}) @log = [:logger] || FogTracker.default_logger end |
Instance Method Details
#billing_type ⇒ String
Returns the default billing type for this policy. Override this to set a human-readable name for the policy. Defaults to the last part of the subclass name.
50 51 52 |
# File 'lib/cloud_cost_tracker/billing/resource_billing_policy.rb', line 50 def billing_type self.class.name.split('::').last #(defaluts to class name) end |
#get_cost_for_duration(resource, duration) ⇒ Float
Returns the cost for a particular resource over some duration in seconds.
ALL BILLING POLICY SUBCLASSES SHOULD OVERRIDE THIS METHOD
44 |
# File 'lib/cloud_cost_tracker/billing/resource_billing_policy.rb', line 44 def get_cost_for_duration(resource, duration) ; 1.0 end |
#setup(resources) ⇒ Object
An initializer called by the framework once per billling cycle. Override this method if you need to perform high-latency operations, like network transactions, that should not be performed per-resource.
37 |
# File 'lib/cloud_cost_tracker/billing/resource_billing_policy.rb', line 37 def setup(resources) ; end |
#write_billing_record_for(resource, hourly_rate, total, start_time, end_time) ⇒ Object
Creates or Updates a BillingRecord for this BillingPolicy’s resource. Don’t override this – it’s called once for each resource by the AccountBillingPolicy.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/cloud_cost_tracker/billing/resource_billing_policy.rb', line 62 def write_billing_record_for(resource, hourly_rate, total, start_time, end_time) account = resource.tracker_account resource_type = resource.class.name.split('::').last return if total == 0.0 # Write no record if the cost is zero new_record = BillingRecord.new( :provider => account[:provider], :service => account[:service], :account => account[:name], :resource_id => resource.identity, :resource_type => resource_type, :billing_type => billing_type, :start_time => start_time, :stop_time => end_time, :cost_per_hour => hourly_rate, :total_cost => total ) new_record.set_codes(resource.billing_codes) # Combine BillingRecords within maximim_gap of one another write_or_merge_with_existing(new_record, account[:delay].to_i) end |