From 2425ed96ecae95902d0af24db2bd824654e666fe Mon Sep 17 00:00:00 2001 From: Ari Zellner Date: Thu, 26 Oct 2017 15:45:47 +0300 Subject: [PATCH] Add 'allocated cpu cores' chargeback rate detail to existing chargeback rates --- ...6103833_add_cores_allocated_rate_detail.rb | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 db/migrate/20171026103833_add_cores_allocated_rate_detail.rb diff --git a/db/migrate/20171026103833_add_cores_allocated_rate_detail.rb b/db/migrate/20171026103833_add_cores_allocated_rate_detail.rb new file mode 100644 index 000000000..5dd52ecf1 --- /dev/null +++ b/db/migrate/20171026103833_add_cores_allocated_rate_detail.rb @@ -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