Module: CloudCostTracker

Included in:
Billing::ResourceBillingPolicy
Defined in:
lib/cloud_cost_tracker.rb,
lib/cloud_cost_tracker/tracker.rb,
lib/cloud_cost_tracker/version.rb,
lib/cloud_cost_tracker/coding/rds/servers.rb,
lib/cloud_cost_tracker/billing/rds/servers.rb,
lib/cloud_cost_tracker/models/billing_code.rb,
lib/cloud_cost_tracker/extensions/fog_model.rb,
lib/cloud_cost_tracker/models/billing_record.rb,
lib/cloud_cost_tracker/billing/rds/server_storage.rb,
lib/cloud_cost_tracker/coding/compute/aws/servers.rb,
lib/cloud_cost_tracker/coding/compute/aws/volumes.rb,
lib/cloud_cost_tracker/billing/compute/aws/servers.rb,
lib/cloud_cost_tracker/billing/compute/aws/volumes.rb,
lib/cloud_cost_tracker/billing/elasticache/clusters.rb,
lib/cloud_cost_tracker/coding/account_coding_policy.rb,
lib/cloud_cost_tracker/billing/compute/aws/snapshots.rb,
lib/cloud_cost_tracker/coding/resource_coding_policy.rb,
lib/cloud_cost_tracker/billing/account_billing_policy.rb,
lib/cloud_cost_tracker/billing/resource_billing_policy.rb,
lib/cloud_cost_tracker/billing/storage/aws/directories.rb,
lib/cloud_cost_tracker/coding/rds/account_coding_policy.rb,
lib/cloud_cost_tracker/coding/compute/aws/account_coding_policy.rb

Overview

Abstract class, defines a generic Billing Policy that always returns 0.0 cost

Defined Under Namespace

Modules: Billing, Coding, Extensions Classes: Tracker

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.account_coding_class(fog_service, provider) ⇒ Object

Returns a Class object, of the appropriate subclass of AccountCodingPolicy, given a Fog service and provider name. If none exists, returns CloudCostTracker::Coding::AccountCodingPolicy.



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cloud_cost_tracker.rb', line 100

def self.(fog_service, provider)
  agent_class = CloudCostTracker::Coding::AccountCodingPolicy
  if CloudCostTracker::Coding.const_defined? fog_service
    service_module = CloudCostTracker::Coding::const_get fog_service
    if service_module.const_defined? provider
      provider_module = service_module.const_get provider
      if provider_module.const_defined? 'AccountCodingPolicy'
        agent_class = provider_module.const_get 'AccountCodingPolicy'
      end
    end
  end
  agent_class
end

.connect_to_database(db_file = ENV['DB_CONFIG_FILE']) ⇒ Hash

Connects to the database defined in db_file. Uses the YAML section indexed by env.

Parameters:

  • db_file (defaults to: ENV['DB_CONFIG_FILE'])

    the path to a Rails-style YAML DB config file.

Returns:

  • (Hash)

    the current envinronment’s database configuration.



27
28
29
30
31
32
33
34
# File 'lib/cloud_cost_tracker.rb', line 27

def self.connect_to_database(db_file = ENV['DB_CONFIG_FILE'])
  db_file ||= "./config/database.yml"
  all_dbs = YAML::load(File.read db_file)
  if not ::ActiveRecord::Base.connected?
    ::ActiveRecord::Base.establish_connection(all_dbs[CloudCostTracker.env])
  end
  all_dbs[CloudCostTracker.env]
end

.create_billing_agents(resource_class, options = {}) ⇒ Array <ResourceBillingPolicy>

Creates and returns an Array of ResourceBillingPolicy (subclass) instances for billing the given resource, or an empty Array of none are found

Parameters:

  • resource_class (Class)

    the Class object for the billed resource

  • options (Hash) (defaults to: {})

    optional additional parameters:

    • :logger - a Ruby Logger-compatible object

Returns:

  • (Array <ResourceBillingPolicy>)

    billing policy objects for resource



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cloud_cost_tracker.rb', line 49

def self.create_billing_agents(resource_class, options = {})
  agents = Array.new
  # Safely descend through the Billing module Heirarchy
  if matches = resource_class.name.match(%r{^Fog::(\w+)::(\w+)::(\w+)})
    fog_svc, provider, model_name = matches[1], matches[2], matches[3]
    if CloudCostTracker::Billing.const_defined? fog_svc
      service_module = CloudCostTracker::Billing::const_get fog_svc
      if service_module.const_defined? provider
        provider_module = service_module.const_get provider
        # Search through the classes in the module for all matches
        classes = provider_module.classes.each do |policy_class|
          if policy_class.name =~ /#{model_name}.*BillingPolicy/
            agents << policy_class.new(:logger => options[:logger])
          end
        end
      end
    end
  end
  agents.sort {|a,b| a.class.name <=> b.class.name} # sort by class name
end

.create_coding_agents(resource_class, options = {}) ⇒ Array <ResourceCodingPolicy>

Creates and returns an Array of ResourceCodingPolicy (subclass) instances for coding the given resource, or an empty Array of none are found

Parameters:

  • resource_class (Class)

    the Class object for the billed resource

  • options (Hash) (defaults to: {})

    optional additional parameters:

    • :logger - a Ruby Logger-compatible object

Returns:

  • (Array <ResourceCodingPolicy>)

    coding policy objects for resource



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cloud_cost_tracker.rb', line 76

def self.create_coding_agents(resource_class, options = {})
  agents = Array.new
  # Safely descend through the Coding module Heirarchy
  if matches = resource_class.name.match(%r{^Fog::(\w+)::(\w+)::(\w+)})
    fog_svc, provider, model_name = matches[1], matches[2], matches[3]
    if CloudCostTracker::Coding.const_defined? fog_svc
      service_module = CloudCostTracker::Coding::const_get fog_svc
      if service_module.const_defined? provider
        provider_module = service_module.const_get provider
        # Search through the classes in the module for all matches
        classes = provider_module.classes.each do |policy_class|
          if policy_class.name =~ /#{model_name}.*CodingPolicy/
            agents << policy_class.new(:logger => options[:logger])
          end
        end
      end
    end
  end
  agents.sort {|a,b| a.class.name <=> b.class.name} # sort by class name
end

.envString

Returns the current RACK_ENV or RAILS_ENV, or ‘development’ if not set.

Returns:

  • (String)

    ENV || ENV || development



19
20
21
# File 'lib/cloud_cost_tracker.rb', line 19

def self.env
  (ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development').downcase
end

.read_accounts(account_file = ENV['ACCOUNT_FILE']) ⇒ Hash

Loads account information defined in account_file.

Parameters:

  • account_file (defaults to: ENV['ACCOUNT_FILE'])

    the path to a YAML file (see accounts.yml.example).

Returns:

  • (Hash)

    the cleaned, validated Hash of account info.



39
40
41
# File 'lib/cloud_cost_tracker.rb', line 39

def self.read_accounts( = ENV['ACCOUNT_FILE'])
  FogTracker.read_accounts()
end