Skip to content

Commit

Permalink
Add 'allocated cpu cores' chargeback rate detail to existing chargeba…
Browse files Browse the repository at this point in the history
…ck rates
  • Loading branch information
Ari Zellner committed Oct 26, 2017
1 parent 267c684 commit 2425ed9
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions db/migrate/20171026103833_add_cores_allocated_rate_detail.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class AddCoresAllocatedRateDetail < ActiveRecord::Migration[5.0]
def up
chargeable_field = ChargeableField.find_or_create(:metric => "derived_vm_numvcpus_cores",
:description => "Allocated CPU Cores",
:group => "cpu_cores",
:source => "allocated") if chargeable_field.nil?

rate_detail_template = ChargebackRateDetail.where(:description => "Allocated CPU Count").first
return if rate_detail_template.nil? # No rates that need this detail.
rate_detail_template = rate_detail_template.dup
rate_detail_template.chargeable_field = chargeable_field
rate_detail_template.description = "Allocated CPU Cores"
rate_detail_template.per_unit = "cpu core"
tier_template = {:start => 0, :finish => Float::INFINITY, :fixed_rate => 1.0, :variable_rate => 0.0}

# Add to cb rates that do not have the "Allocated CPU Cores" cb detail
ChargebackRate.where(:rate_type => "Compute").where.not(:id => ChargebackRateDetail.where(:description => "Allocated CPU Cores").select(:chargeback_rate_id)).each do |rate|
new_rate_detail = rate_detail_template.dup
new_rate_detail.chargeback_tiers << ChargebackTier.new(tier_template.slice(*ChargebackTier::FORM_ATTRIBUTES))
rate.chargeback_rate_details << new_rate_detail
end
end

def down
ChargebackRateDetail.where(:description => "Allocated CPU Cores").destroy_all
end
end

0 comments on commit 2425ed9

Please sign in to comment.