Skip to content

Commit

Permalink
Merge pull request ManageIQ#22491 from kbrock/chargeback-vm
Browse files Browse the repository at this point in the history
Fix Chargeback not working when company name settings changes
  • Loading branch information
Fryguy authored May 9, 2023
2 parents 9dec0c4 + 929d933 commit 5141f2c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
11 changes: 8 additions & 3 deletions app/models/chargeback_vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,18 @@ def self.vms(region)
end

tenant_name = tenant.name
tenant = Tenant.in_region(region).find_by(:name => tenant.name)
# NOTE: tenant.name has settings applied.
# i.e.: Tenant.where(:id => tenant.id, :name => tenant.name).exists? could be false
if tenant.region_number != region
tenant = Tenant.in_region(region).find_by(:name => tenant.name)
end
if tenant.nil?
error_message = "Unable to find tenant '#{tenant_name}' (based on tenant id '#{@options[:tenant_id]}' from default region) in region #{region}"
_log.info("#{error_message}. Calculating chargeback costs skipped for #{@options[:tenant_id]} in region #{region}.")
return Vm.none
Vm.none
else
Vm.where(:tenant_id => tenant.subtree.select(:id))
end
Vm.where(:id => tenant.subtree.map { |t| t.vms.ids }.flatten)
elsif @options[:service_id]
service = Service.find(@options[:service_id])
if service.nil?
Expand Down
28 changes: 18 additions & 10 deletions spec/models/chargeback_vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -443,24 +443,32 @@ def pluck_rollup(metric_rollup_records)
end

context "Report a chargeback of a tenant" do
let(:options_tenant) { base_options.merge(:tenant_id => @tenant.id).tap { |t| t.delete(:tag) } }
let(:tenant) { FactoryBot.create(:tenant) }
let(:tenant_child) { FactoryBot.create(:tenant, :parent => tenant) }
let(:options_tenant) { base_options.merge(:tenant_id => tenant).tap { |t| t.delete(:tag) } }

let(:start_time) { report_run_time - 17.hours }
let(:finish_time) { report_run_time - 14.hours }

before do
@tenant = FactoryBot.create(:tenant)
@tenant_child = FactoryBot.create(:tenant, :parent => @tenant)
@vm_tenant = FactoryBot.create(:vm_vmware, :tenant_id => @tenant_child.id,
:name => "test_vm_tenant", :created_on => month_beginning)

add_metric_rollups_for(@vm_tenant, start_time...finish_time, 1.hour, metric_rollup_params)
let!(:vm) do
FactoryBot.create(:vm_vmware, :tenant => tenant_child, :created_on => month_beginning).tap do |vm|
add_metric_rollups_for(vm, start_time...finish_time, 1.hour, metric_rollup_params)
end
end

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

it "report a chargeback of a subtenant" do
expect(subject.vm_name).to eq(@vm_tenant.name)
expect(subject.vm_name).to eq(vm.name)
end

context "with settings derived tenant name" do
let(:tenant) { FactoryBot.create(:tenant, :use_config_for_attributes => true) }

before { stub_settings_merge(:server => {:company => "Special Name"}) }

it "report a chargeback of a subtenant" do
expect(subject&.vm_name).to eq(vm.name)
end
end
end

Expand Down

0 comments on commit 5141f2c

Please sign in to comment.