Skip to content

Commit

Permalink
Merge pull request #17558 from lpichler/refactor_tag_tag_list_with_pr…
Browse files Browse the repository at this point in the history
…efix

Refactor tag_list_with_prefix in Metric::ChargebackHelper
(cherry picked from commit 9c48f0a)

https://bugzilla.redhat.com/show_bug.cgi?id=1591939
  • Loading branch information
gtanzillo authored and simaishi committed Jun 15, 2018
1 parent d3719f3 commit d24aa42
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
31 changes: 20 additions & 11 deletions app/models/metric/chargeback_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,31 @@ def tag_prefix
klass_prefix + TAG_MANAGED_PREFIX
end

def tag_list_with_prefix
def chargeback_container_labels
resource.try(:container_image).try(:docker_labels).try(:collect_concat) do |l|
escaped_name = AssignmentMixin.escape(l.name)
escaped_value = AssignmentMixin.escape(l.value)
[
# The assignments in tags can appear the old way as they are, or escaped
"container_image/label/managed/#{l.name}/#{l.value}",
"container_image/label/managed/#{escaped_name}/#{escaped_value}"
]
end || []
end

def container_tag_list_with_prefix
if resource.kind_of?(Container)
state = resource.vim_performance_state_for_ts(timestamp.to_s)
image_tag_name = "#{state.image_tag_names}|" if state
labels = resource.try(:container_image).try(:docker_labels).try(:collect_concat) do |l|
escaped_name = AssignmentMixin.escape(l.name)
escaped_value = AssignmentMixin.escape(l.value)
[
# The assignments in tags can appear the old way as they are, or escaped
"container_image/label/managed/#{l.name}/#{l.value}",
"container_image/label/managed/#{escaped_name}/#{escaped_value}"
]
end

image_tag_name.split("|")
else
[]
end
end

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

def resource_parents
Expand Down
32 changes: 31 additions & 1 deletion spec/models/metric_rollup/chargeback_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
describe MetricRollup do
describe '#parents_determining_rate' do
let(:ems) { FactoryGirl.build(:ems_vmware) }

before do
MiqRegion.seed
MiqEnterprise.seed
end

context 'VmOrTemplate' do
let(:ems) { FactoryGirl.build(:ems_vmware) }
let(:ems_cluster) { FactoryGirl.build(:ems_cluster, :ext_management_system => ems) }
let(:storage) { FactoryGirl.build(:storage_target_vmware) }
let(:host) { FactoryGirl.build(:host) }
Expand Down Expand Up @@ -57,6 +58,35 @@

it { is_expected.to match_array(parents_from_vm) }
end

describe "#tag_list_with_prefix" do
let(:tag) { FactoryGirl.create(:tag, :name => "/managed/operations/analysis_failed") }
let(:vm) { FactoryGirl.create(:vm_vmware, :tags => [tag]) }
let(:metric_rollup) { FactoryGirl.create(:metric_rollup_vm_hr, :resource => vm, :tag_names => "environment/prod|environment/dev") }

it 'returns array of tags' do
expect(metric_rollup.tag_list_with_prefix).to match_array(%w(vm/tag/managed/operations/analysis_failed vm/tag/managed/environment/prod vm/tag/managed/environment/dev))
end
end
end

context "with Containers" do
describe "#tag_list_with_prefix" do
let(:timestamp) { Time.parse('2012-09-01 23:59:59Z').utc }
let(:vim_performance_state) { FactoryGirl.create(:vim_performance_state, :timestamp => timestamp, :image_tag_names => "environment/stage") }

let(:image) { FactoryGirl.create(:container_image, :ext_management_system => ems, :docker_labels => [label]) }
let(:label) { FactoryGirl.create(:custom_attribute, :name => "version/1.2/_label-1", :value => "test/1.0.0 rc_2", :section => 'docker_labels') }
let(:project) { FactoryGirl.create(:container_project, :name => "my project", :ext_management_system => ems) }
let(:node) { FactoryGirl.create(:container_node, :name => "node") }
let(:group) { FactoryGirl.create(:container_group, :ext_management_system => ems, :container_project => project, :container_node => node) }
let(:container) { FactoryGirl.create(:kubernetes_container, :container_group => group, :container_image => image, :vim_performance_states => [vim_performance_state]) }
let(:metric_rollup_container) { FactoryGirl.create(:metric_rollup_vm_hr, :timestamp => timestamp, :resource => container, :tag_names => "environment/cont|environment/cust") }

it 'returns array of tags' do
expect(metric_rollup_container.tag_list_with_prefix).to match_array(%w(container_image/tag/managed/environment/cont container_image/tag/managed/environment/cust container_image/tag/managed/environment/stage container_image/label/managed/version/1.2/_label-1/test/1.0.0\ \ rc_2 container_image/label/managed/escaped:{version%2F1%2E2%2F%5Flabel%2D1}/escaped:{test%2F1%2E0%2E0%20%20rc%5F2}))
end
end
end
end
end

0 comments on commit d24aa42

Please sign in to comment.