Skip to content

Commit

Permalink
Merge pull request #16638 from lpichler/consider_cloud_volumes_as_dat…
Browse files Browse the repository at this point in the history
…astorages

Consider cloud volumes as data storages in chargeback
  • Loading branch information
gtanzillo authored Dec 12, 2017
2 parents 79a7726 + 56edf32 commit 633283e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
6 changes: 6 additions & 0 deletions app/models/chargeback_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class ChargebackRate < ApplicationRecord
scope :with_rate_type, ->(rate_type) { where(:rate_type => rate_type) }

VALID_CB_RATE_TYPES = ["Compute", "Storage"]
DATASTORE_MAPPING = {'CloudVolume' => 'Storage'}.freeze

def self.tag_class(klass)
klass = ChargebackRate::DATASTORE_MAPPING[klass] || klass
super(klass)
end

def rate_details_relevant_to(report_cols)
# we can memoize, as we get the same report_cols thrrough the life of the object
Expand Down
5 changes: 2 additions & 3 deletions app/models/metric/chargeback_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ def tag_list_with_prefix
def resource_parents
[parent_host || resource.try(:host),
parent_ems_cluster || resource.try(:ems_cluster),
parent_storage || resource.try(:storage),
parent_storage || resource.try(:storage) || resource.try(:cloud_volumes),
parent_ems || resource.try(:ext_management_system),
resource.try(:tenant)
].compact
resource.try(:tenant)].flatten.compact
end

def parents_determining_rate
Expand Down
8 changes: 5 additions & 3 deletions app/models/mixins/assignment_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ def assignments
end
end

def tag_class(klass)
klass == "VmOrTemplate" ? "vm" : klass.underscore
end

# @param target
# @option options :parents
# @option options :tag_list
Expand All @@ -196,9 +200,7 @@ def get_assigned_for_target(target, options = {})
tlist = Tagging.where("tags.name like '/managed/%'")
.where(:taggable => parents)
.references(:tag).includes(:tag).map do |t|
klass = t.taggable_type
lower_klass = klass == "VmOrTemplate" ? "vm" : klass.underscore
"#{lower_klass}/tag#{t.tag.name}"
"#{tag_class(t.taggable_type)}/tag#{t.tag.name}"
end
tagged_resources = tlist.flat_map { |t| assignments_cached[t] }.uniq
(individually_assigned_resources + tagged_resources).uniq
Expand Down
21 changes: 21 additions & 0 deletions spec/models/mixins/assignment_mixin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@
# too ingrained in AR - has many, acts_as_miq_taggable, ...
let(:test_class) { MiqAlertSet }

describe '#get_assigned_for_target' do
context 'searching for ChargebackRate' do
let(:test_class) { ChargebackRate }
let(:vm) { FactoryGirl.create(:vm_openstack) }
let(:hardware) { FactoryGirl.create(:hardware, :vm_or_template_id => vm.id) }
let(:cloud_volume) { FactoryGirl.create(:cloud_volume, :hardwares => [hardware]) }
let(:chargeback_rate) { FactoryGirl.create(:chargeback_rate, :rate_type => 'Storage') }

before do
ct1 = ctag("environment", "test1")
chargeback_rate.assign_to_tags([ct1], "storage")
cloud_volume.tag_add('environment/test1', :ns => '/managed')
end

it 'returns rates based on tagged cloud volume' do
result = test_class.get_assigned_for_target(vm, :parents => [cloud_volume])
expect(result).to match_array([chargeback_rate])
end
end
end

describe '#assignments' do
it "finds no assignments" do
expect(test_class.assignments).to eq({})
Expand Down

0 comments on commit 633283e

Please sign in to comment.