Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix chargeback for container Images with rate assigning by docker label #12851

Merged
merged 10 commits into from
Dec 6, 2016
56 changes: 54 additions & 2 deletions app/controllers/chargeback_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def cb_assign_field_changed
render :update do |page|
page << javascript_prologue
changed = (@edit[:new] != @edit[:current])
page.replace("cb_assignment_div", :partial => "cb_assignments") if params[:cbshow_typ] || params[:cbtag_cat] # only replace if cbshow_typ or cbtag_cat has changed
page.replace("cb_assignment_div", :partial => "cb_assignments") if params[:cbshow_typ] || params[:cbtag_cat] || params[:cblabel_key]
page << javascript_for_miq_button_visibility(changed)
end
end
Expand Down Expand Up @@ -623,6 +623,19 @@ def cb_assign_set_record_vars
@edit[:set_assignments].push(temp)
end
end
elsif @edit[:new][:cbshow_typ].ends_with?("-labels")
@edit[:set_assignments] = []
@edit[:cb_assign][:docker_label_values].each do |id, _value|
key = "#{@edit[:new][:cbshow_typ]}__#{id}"
if !@edit[:new][key].nil? && @edit[:new][key] != "nil"
temp = {
:cb_rate => ChargebackRate.find(@edit[:new][key]),
:label => [CustomAttribute.find(id)]
}
temp[:label].push(@edit[:new][:cbshow_typ].split("-").first)
@edit[:set_assignments].push(temp)
end
end
else
@edit[:set_assignments] = []
@edit[:cb_assign][:cis].each do |id, _ci|
Expand Down Expand Up @@ -670,7 +683,11 @@ def cb_assign_set_form_vars
when MiqEnterprise
"enterprise"
when NilClass
"#{@edit[:current_assignment][0][:tag][1]}-tags"
if @edit[:current_assignment][0][:tag]
"#{@edit[:current_assignment][0][:tag][1]}-tags"
else
"#{@edit[:current_assignment][0][:label][1]}-labels"
end
else
@edit[:current_assignment][0][:object].class.name.downcase
end
Expand All @@ -684,6 +701,16 @@ def cb_assign_set_form_vars
else
@edit[:current_assignment] = []
end
elsif @edit[:new][:cbshow_typ] && @edit[:new][:cbshow_typ].ends_with?("-labels")
get_docker_labels_all_keys
label = @edit[:current_assignment][0][:label][0]
if label
label = @edit[:cb_assign][:docker_label_keys].detect { |_key, value| value == label.name }
@edit[:new][:cblabel_key] = label.first.to_s
get_docker_labels_all_values(label.first)
else
@edit[:current_assignment] = []
end
elsif @edit[:new][:cbshow_typ]
get_cis_all
end
Expand All @@ -693,6 +720,9 @@ def cb_assign_set_form_vars
@edit[:new]["#{@edit[:new][:cbshow_typ]}__#{el[:object]["id"]}"] = el[:cb_rate]["id"].to_s
elsif el[:tag]
@edit[:new]["#{@edit[:new][:cbshow_typ]}__#{el[:tag][0]["id"]}"] = el[:cb_rate]["id"].to_s
elsif el[:label]
label = @edit[:cb_assign][:docker_label_values].detect { |_key, value| value == el[:label][0].value }
@edit[:new]["#{@edit[:new][:cbshow_typ]}__#{label.first}"] = el[:cb_rate]["id"].to_s
end
end

Expand All @@ -718,6 +748,22 @@ def get_tags_all(category)
classification.entries.each { |e| @edit[:cb_assign][:tags][e.id.to_s] = e.description } if classification
end

def get_docker_labels_all_keys
@edit[:cb_assign][:docker_label_keys] = {}
CustomAttribute.where(:section => "docker_labels").pluck(:id, :name).uniq(&:second).each do |label|
@edit[:cb_assign][:docker_label_keys][label.first.to_s] = label.second
end
end

def get_docker_labels_all_values(label_id)
@edit[:cb_assign][:docker_label_values] = {}
label_name = CustomAttribute.find(label_id).name

CustomAttribute.where(:section => "docker_labels", :name => label_name).pluck(:id, :value).uniq(&:second).each do |label|
@edit[:cb_assign][:docker_label_values][label.first.to_s] = label.second
end
end

WHITELIST_INSTANCE_TYPE = %w(enterprise storage ext_management_system ems_cluster tenant ems_container).freeze
NOTHING_FORM_VALUE = "nil".freeze

Expand Down Expand Up @@ -763,16 +809,22 @@ def cb_assign_get_form_vars
@edit[:new][:cbshow_typ] = params[:cbshow_typ] if params[:cbshow_typ]
@edit[:new][:cbtag_cat] = nil if params[:cbshow_typ] # Reset categories pull down if assign to selection is changed
@edit[:new][:cbtag_cat] = params[:cbtag_cat].to_s if params[:cbtag_cat]
@edit[:new][:cblabel_key] = nil if params[:cbshow_typ]
@edit[:new][:cblabel_key] = params[:cblabel_key].to_s if params[:cblabel_key]

if @edit[:new][:cbshow_typ].ends_with?("-tags")
get_categories_all
get_tags_all(params[:cbtag_cat]) if params[:cbtag_cat]
elsif @edit[:new][:cbshow_typ].ends_with?("-labels")
get_docker_labels_all_keys
get_docker_labels_all_values(params[:cblabel_key]) unless params[:cblabel_key].blank?
else
get_cis_all
end

cb_assign_params_to_edit(:cis)
cb_assign_params_to_edit(:tags)
cb_assign_params_to_edit(:docker_label_values)
end

def replace_right_cell(options = {})
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/container_image_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class ContainerImageController < ApplicationController
after_action :cleanup_action
after_action :set_session_data

def show_list
process_show_list(:where_clause => 'container_images.deleted_on IS NULL')
end

def guest_applications
show_association('guest_applications', _('Packages'), 'guest_application', :guest_applications, GuestApplication)
end
Expand Down
16 changes: 9 additions & 7 deletions app/controllers/report_controller/reports/editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1281,15 +1281,17 @@ def set_form_vars
@edit[:cb_owner_name] = current_user.name
end

@edit[:cb_providers] = {}
@edit[:cb_providers][:container_project] = {}
@edit[:cb_providers][:vm] = {} # Fill this in if entity show type it ever becomes relevent for VMs
@edit[:cb_entities_by_provider_id] = {}
ManageIQ::Providers::ContainerManager.all.each do |provider|
@edit[:cb_providers] = { :container_project => {}, :container_image => {} }
@edit[:cb_entities_by_provider_id] = { :container_project => {}, :container_image => {} }
ManageIQ::Providers::ContainerManager.includes(:container_projects, :container_images).all.each do |provider|
@edit[:cb_providers][:container_project][provider.name] = provider.id
@edit[:cb_entities_by_provider_id][provider.id] = {}
@edit[:cb_providers][:container_image][provider.name] = provider.id
@edit[:cb_entities_by_provider_id][provider.id] = {:container_project => {}, :container_image => {}}
provider.container_projects.all.each do |project|
@edit[:cb_entities_by_provider_id][provider.id][project.id] = project.name
@edit[:cb_entities_by_provider_id][provider.id][:container_project][project.id] = project.name
end
provider.container_images.all.each do |image|
@edit[:cb_entities_by_provider_id][provider.id][:container_image][image.id] = image.name
end
end

Expand Down
8 changes: 2 additions & 6 deletions app/helpers/container_helper/textual_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,9 @@ def textual_group_env
}
end

def collect_env
def collect_env_variables
@record.container_definition.container_env_vars.collect do |var|
[
var.name,
var.value.nil? ? "REFERENCE" : "VALUE",
{:value => var.value.nil? ? var.field_path : var.value, :expandable => true}
]
[var.name, var.value, var.field_path]
end
end
end
38 changes: 37 additions & 1 deletion app/helpers/container_image_helper/textual_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module TextualSummary
#

def textual_group_properties
%i(name tag id full_name os_distribution product_type product_name)
%i(name tag id full_name os_distribution product_type product_name architecture author command entrypoint docker_version exposed_ports size)
end

def textual_group_relationships
Expand Down Expand Up @@ -79,5 +79,41 @@ def textual_openscap_failed_rules_high
high = failed_rules_summary[:High]
{:label => _("High"), :value => high} if high
end

def textual_exposed_ports
{:label => _("Exposed Ports"),
:value => (@record['exposed_ports'].collect { |t, p| "#{p}/#{t}" }).join(', ')
}
end

def method_missing(mthd_sym, *args, &bolck)
match = /textual_(.*)/.match(mthd_sym)
if match && match.size > 1
prop = match[1]
value = @record[prop] if @record.attribute_present?(prop)
value = value.join(' ') if value.kind_of?(Array)
{:label => _(prop.titlecase), :value => value}
else
super
end
end
end

def textual_group_env
{
:additional_table_class => "table-fixed",
:labels => [_("Name"), _("Type"), _("Value")],
:values => collect_env
}
end

def collect_env_variables
@record['environment_variables'].collect do |name, value|
[name, value, nil]
end
end

def textual_group_container_docker_labels
textual_key_value_group(@record.docker_labels.to_a)
end
end
10 changes: 10 additions & 0 deletions app/helpers/container_summary_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ def textual_tags
h
end

def collect_env
collect_env_variables.collect do |name, value, field_path|
[
name,
value.nil? ? "REFERENCE" : "VALUE",
{:value => value.nil? ? field_path : value, :expandable => true}
]
end
end

private

def textual_key_value_group(items)
Expand Down
27 changes: 15 additions & 12 deletions app/helpers/ui_constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -507,23 +507,26 @@ module UiConstants
# This set of assignments was created for chargeback_rates
ASSIGN_TOS[:chargeback_storage] = ASSIGN_TOS["Storage"]
ASSIGN_TOS[:chargeback_compute] = {
"enterprise" => N_("The Enterprise"),
"ext_management_system" => PostponedTranslation.new(N_("Selected %{tables}")) do
"enterprise" => N_("The Enterprise"),
"ext_management_system" => PostponedTranslation.new(N_("Selected %{tables}")) do
{:tables => ui_lookup(:tables => "ext_management_systems")}
end,
"ems_cluster" => PostponedTranslation.new(N_("Selected %{tables}")) do
end,
"ems_cluster" => PostponedTranslation.new(N_("Selected %{tables}")) do
{:tables => ui_lookup(:tables => "ems_cluster")}
end,
"ems_container" => PostponedTranslation.new(N_("Selected %{tables}")) do
end,
"ems_container" => PostponedTranslation.new(N_("Selected %{tables}")) do
{:tables => ui_lookup(:tables => "ems_container")}
end,
"vm-tags" => PostponedTranslation.new(N_("Tagged %{tables}")) do
end,
"vm-tags" => PostponedTranslation.new(N_("Tagged %{tables}")) do
{:tables => ui_lookup(:tables => "vm")}
end,
"container_image-tags" => PostponedTranslation.new(N_("Tagged %{tables}")) do
end,
"container_image-tags" => PostponedTranslation.new(N_("Tagged %{tables}")) do
{:tables => ui_lookup(:tables => "container_image")}
end,
"tenant" => N_("Tenants")
end,
"container_image-labels" => PostponedTranslation.new(N_("Labeled %{tables}")) do
{:tables => ui_lookup(:tables => "container_image")}
end,
"tenant" => N_("Tenants")
}

EXP_COUNT_TYPE = [N_("Count of"), "count"].freeze # Selection for count based filters
Expand Down
2 changes: 1 addition & 1 deletion app/models/chargeback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def self.build_results_for_report_chargeback(options)
rates = RatesCache.new

base_rollup = MetricRollup.includes(
:resource => [:hardware, :tenant, :tags, :vim_performance_states, :custom_attributes],
:resource => [:hardware, :tenant, :tags, :vim_performance_states, :custom_attributes, {:container_image => :custom_attributes}],
:parent_host => :tags,
:parent_ems_cluster => :tags,
:parent_storage => :tags,
Expand Down
5 changes: 5 additions & 0 deletions app/models/chargeback/rates_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ def get(perf)
ChargebackRate.get_assigned_for_target(perf.resource,
:tag_list => perf.tag_list_with_prefix,
:parents => perf.parents_determining_rate)

if perf.resource_type == Container.name && @rates[perf.hash_features_affecting_rate].empty?
@rates[perf.hash_features_affecting_rate] = [ChargebackRate.find_by(:description => "Default", :rate_type => "Compute")]
end
@rates[perf.hash_features_affecting_rate]
end
end
end
3 changes: 2 additions & 1 deletion app/models/chargeback_container_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def init_extra_fields(perf)
self.image_name = self.class.image(perf).try(:full_name) || _('Deleted') # until image archiving is implemented
self.project_uid = self.class.project(perf).ems_ref
self.provider_name = perf.parent_ems.try(:name)
self.provider_uid = perf.parent_ems.try(:name)
self.provider_uid = perf.parent_ems.try(:guid)
self.archived = self.class.project(perf).archived? ? _('Yes') : _('No')
self.entity = self.class.image(perf)
end
end # class ChargebackContainerImage
4 changes: 3 additions & 1 deletion app/models/chargeback_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def self.get_assignments(type)
assigned_tos = rate.get_assigned_tos
assigned_tos[:tags].each { |tag| result << {:cb_rate => rate, :tag => tag} }
assigned_tos[:objects].each { |object| result << {:cb_rate => rate, :object => object} }
assigned_tos[:labels].each { |label| result << {:cb_rate => rate, :label => label} }
end
result
end
Expand All @@ -51,6 +52,7 @@ def self.set_assignments(type, cb_rates)
cb_rates.each do |rate|
rate[:cb_rate].assign_to_objects(rate[:object]) if rate.key?(:object)
rate[:cb_rate].assign_to_tags(*rate[:tag]) if rate.key?(:tag)
rate[:cb_rate].assign_to_labels(*rate[:label]) if rate.key?(:label)
end
end

Expand Down Expand Up @@ -159,7 +161,7 @@ def self.seed_chargeback_rate
end

def assigned?
get_assigned_tos != {:objects => [], :tags => []}
get_assigned_tos != {:objects => [], :tags => [], :labels => []}
end

###########################################################
Expand Down
13 changes: 13 additions & 0 deletions app/models/container_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class ContainerImage < ApplicationRecord
include MiqPolicyMixin
include ScanningMixin
include TenantIdentityMixin
include CustomAttributeMixin


DOCKER_IMAGE_PREFIX = "docker://"
Expand All @@ -20,6 +21,8 @@ class ContainerImage < ApplicationRecord
has_one :operating_system, :through => :computer_system
has_one :openscap_result, :dependent => :destroy
has_many :openscap_rule_results, :through => :openscap_result
has_many :labels, -> { where(:section => "labels") }, :class_name => CustomAttribute, :as => :resource, :dependent => :destroy
has_many :docker_labels, -> { where(:section => "docker_labels") }, :class_name => CustomAttribute, :as => :resource, :dependent => :destroy

serialize :exposed_ports, Hash
serialize :environment_variables, Hash
Expand Down Expand Up @@ -99,5 +102,15 @@ def openscap_failed_rules_summary
openscap_rule_results.where(:result => "fail").group(:severity).count.symbolize_keys
end

def disconnect_inv
_log.info "Disconnecting Image [#{name}] id [#{id}] from EMS [#{ext_management_system.name}]" \
"id [#{ext_management_system.id}] "
self.container_image_registry = nil
self.old_ems_id = ems_id
self.ext_management_system = nil
self.deleted_on = Time.now.utc
save
end

alias_method :perform_metadata_sync, :sync_stashed_metadata
end
Loading