Skip to content

Commit

Permalink
Merge pull request #17470 from lpichler/filter_chargeback_according_t…
Browse files Browse the repository at this point in the history
…o_current_tags

Use current tags for filtering resources in chargeback for VMs
  • Loading branch information
jrafanie authored May 25, 2018
2 parents c65502b + 9130b57 commit 1a2a40f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/models/chargeback/consumption_with_rollups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def hash_features_affecting_rate

def tag_names
@tag_names ||= @rollups.inject([]) do |memo, rollup|
memo |= rollup.tag_names.split('|') if rollup.tag_names.present?
memo |= rollup.all_tag_names
memo
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/chargeback_vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def self.build_results_for_report_ChargebackVm(options)
def self.where_clause(records, options, region)
scope = records.where(:resource_type => "VmOrTemplate")
if options[:tag] && (@report_user.nil? || !@report_user.self_service?)
scope.for_tag_names(options[:tag].split("/")[2..-1])
scope_with_current_tags = scope.where(:resource => Vm.find_tagged_with(:any => @options[:tag], :ns => '*'))
scope.for_tag_names(options[:tag].split("/")[2..-1]).or(scope_with_current_tags)
else
scope.where(:resource => vms(region))
end
Expand Down
14 changes: 13 additions & 1 deletion app/models/metric/chargeback_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def tag_list_with_prefix
end
end

"#{image_tag_name}#{tag_names}".split("|").reject(&:empty?).map { |x| "#{tag_prefix}#{x}" } + (labels || [])
"#{image_tag_name}#{all_tag_names.join("|")}".split("|").reject(&:empty?).map { |x| "#{tag_prefix}#{x}" } + (labels || [])
end

def resource_parents
Expand All @@ -47,4 +47,16 @@ def parents_determining_rate
[parent_ems].compact
end
end

def resource_current_tag_names
resource ? resource.tags.collect(&:name).map { |x| x.gsub("/managed/", "") } : []
end

def resource_tag_names
tag_names ? tag_names.split("|") : []
end

def all_tag_names
resource_current_tag_names | resource_tag_names
end
end
24 changes: 22 additions & 2 deletions spec/models/chargeback_vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,21 @@ def result_row_for_vm(vm)

subject { ChargebackVm.build_results_for_report_ChargebackVm(options).first.first }

context "when MetricRollup#tag_names are not considered" do
before do
# report filter is set to different tag
@vm1.metric_rollups.each { |mr| mr.update(:tag_names => 'registered/no|folder_path_yellow/datacenters') }
end

it "cpu" do
expect(subject.cpu_allocated_metric).to eq(cpu_count)
used_metric = used_average_for(:cpu_usagemhz_rate_average, hours_in_month, @vm1)
expect(subject.cpu_used_metric).to be_within(0.01).of(used_metric)
expect(subject.cpu_used_cost).to be_within(0.01).of(used_metric * hourly_rate * hours_in_month)
expect(subject.cpu_allocated_cost).to be_within(0.01).of(cpu_count * count_hourly_rate * hours_in_month)
end
end

context "chargeback rate contains rate unrelated to chargeback vm" do
let!(:chargeback_rate) do
FactoryGirl.create(:chargeback_rate, :detail_params => detail_params.merge(:chargeback_rate_detail_cpu_cores_allocated => {:tiers => [count_hourly_variable_tier_rate]}))
Expand Down Expand Up @@ -905,19 +920,24 @@ def result_row_for_vm(vm)
FactoryGirl.create(:metric_rollup_vm_hr, :timestamp => report_run_time - 1.day - 17.hours,
:parent_host_id => @host1.id, :parent_ems_cluster_id => @ems_cluster.id,
:parent_ems_id => ems.id, :parent_storage_id => @storage.id,
:resource => @vm1)
:resource => @vm)
end
let(:consumption) { Chargeback::ConsumptionWithRollups.new([metric_rollup], nil, nil) }

before do
@storage.tag_with([classification_1.tag.name, classification_2.tag.name], :ns => '*')
ChargebackRate.set_assignments(:storage, [rate_assignment_options_1, rate_assignment_options_2])
@vm = FactoryGirl.create(:vm_vmware, :name => "test_vm_1", :evm_owner => admin, :ems_ref => "ems_ref", :created_on => month_beginning)
end

it "return only one chargeback rate according to tag name of Vm" do
skip('this feature needs to be added to new chargeback') if Settings.new_chargeback

[rate_assignment_options_1, rate_assignment_options_2].each do |rate_assignment|
metric_rollup.tag_names = rate_assignment[:tag].first.tag.send(:name_path)
uniq_rates = chargeback_vm.get(consumption)
@vm.tag_with(["/managed/#{metric_rollup.tag_names}"], :ns => '*')
@vm.reload
uniq_rates = Chargeback::RatesCache.new.get(consumption)
consumption.instance_variable_set(:@tag_names, nil)
consumption.instance_variable_set(:@hash_features_affecting_rate, nil)
expect([rate_assignment[:cb_rate]]).to match_array(uniq_rates)
Expand Down

0 comments on commit 1a2a40f

Please sign in to comment.