Class: CloudCostTracker::Coding::AccountCodingPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_cost_tracker/coding/account_coding_policy.rb

Overview

Implements the generic logic for attaching billing codes to all resources in a single account. Initializes the necessary ResourceCodingPolicy objects, sorts the resources by policy, and calls #code once on each resource’s policy, to compute the billing codes as pairs of Strings.

Instance Method Summary collapse

Constructor Details

#initialize(resources, options = {}) ⇒ AccountCodingPolicy

Creates an object that implements a coding policy that attaches no billing codes

Parameters:

  • resources (Array <Fog::Model>)

    the resources to code

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

    optional parameters:

    • :logger - a Ruby Logger-compatible object



15
16
17
18
# File 'lib/cloud_cost_tracker/coding/account_coding_policy.rb', line 15

def initialize(resources, options={})
  @log = options[:logger] || FogTracker.default_logger
  setup_resource_coding_agents(resources)
end

Instance Method Details

#attach_account_codes(resource) ⇒ Object

Defines an acount-wide coding strategy for coding each resource. Override this method if you need to write logic for attaching billing codes to all resources in an account, regardless of collection / type.



34
# File 'lib/cloud_cost_tracker/coding/account_coding_policy.rb', line 34

def (resource) ; end

#code(resources) ⇒ Object

Defines the default method for coding all resources. Attaches Billing Codes (String pairs) to resources, as @billing_codes. Resources whose class is in #priority_classes are coded first.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cloud_cost_tracker/coding/account_coding_policy.rb', line 39

def code(resources)
  return if resources.empty?
   = resources.first.
  @log.info "Coding account #{[:name]}"
  resources.each {|resource| (resource)}
  classes_to_code = priority_classes + (@agents.keys - priority_classes)
  classes_to_code.delete_if {|res_class| @agents[res_class] == nil}
  classes_to_code.each do |fog_model_class|
    @log.info "Coding #{fog_model_class} in account #{[:name]}"
    collection = resources.select {|r| r.class == fog_model_class}
    collection.each do |resource|
      @agents[fog_model_class].each do |agent|
        @log.debug "Coding #{resource.tracker_description}"
        agent.code(resource)
      end
    end
  end
end

#priority_classesArray <Class>

Defines the order in which resource collections are coded. Override this method if you need to code the resource collections in a particular order. Return an Array of the Fog::Model subclasses.

Returns:

  • (Array <Class>)

    the class names, in preferred coding order



29
# File 'lib/cloud_cost_tracker/coding/account_coding_policy.rb', line 29

def priority_classes ; Array.new end

#setup(resources) ⇒ Object

An initializer called by the framework once per bill coding cycle. Override this method if you need to perform high-latency operations, like network transactions, that should not be performed per-resource.



23
# File 'lib/cloud_cost_tracker/coding/account_coding_policy.rb', line 23

def setup(resources) ; end