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

[Fine] Fine tag mapping #17068

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/models/classification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class Classification < ApplicationRecord
scope :read_only, -> { where :read_only => true }
scope :writeable, -> { where :read_only => false }

scope :is_category, -> { where(:parent_id => 0) }
scope :is_entry, -> { where.not(:parent_id => 0) }

DEFAULT_NAMESPACE = "/managed"

default_value_for :read_only, false
Expand Down
19 changes: 7 additions & 12 deletions app/models/container_label_tag_mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ContainerLabelTagMapping < ApplicationRecord
#
# All involved tags must also have a Classification.

TAG_PREFIXES = ['/managed/amazon:', '/managed/kubernetes:'].freeze
AUTOTAG_PREFIX = "kubernetes".freeze

MAPPABLE_ENTITIES = [
Expand All @@ -29,16 +30,7 @@ class ContainerLabelTagMapping < ApplicationRecord

belongs_to :tag

# Pass the data this returns to map_* methods.
def self.cache
# {[name, type, value] => [tag_id, ...]}
in_my_region.find_each
.group_by { |m| [m.label_name, m.labeled_resource_type, m.label_value].freeze }
.transform_values { |mappings| mappings.collect(&:tag_id) }
end

# We expect labels to be {:name, :value} hashes
# and return {:tag_id} or {:category_tag_id, :entry_name, :entry_description} hashes.
require_nested :Mapper

def self.map_labels(cache, type, labels)
labels.collect_concat { |label| map_label(cache, type, label) }.uniq
Expand Down Expand Up @@ -93,15 +85,18 @@ def self.find_or_create_tag(tag_hash)
end
end

# Checks whether a Tag record is under mapping control.
# TODO: expensive.
def self.controls_tag?(tag)
return false unless tag.classification.try(:read_only) # never touch user-assignable tags.
tag_ids = [tag.id, tag.category.tag_id].uniq
where(:tag_id => tag_ids).any?
end

# Assign/unassign mapping-controlled tags, preserving user-assigned tags.
def self.retag_entity(entity, tag_hashes)
mapped_tags = tag_hashes.map { |tag_hash| find_or_create_tag(tag_hash) }
# All tag references must have been resolved first by Mapper#find_or_create_tags.
def self.retag_entity(entity, tag_references)
mapped_tags = Mapper.references_to_tags(tag_references)
existing_tags = entity.tags
Tagging.transaction do
(mapped_tags - existing_tags).each do |tag|
Expand Down
3 changes: 3 additions & 0 deletions app/models/manageiq/providers/cloud_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class << model_name
has_many :security_groups, :through => :network_manager
has_one :source_tenant, :as => :source, :class_name => 'Tenant'
has_many :vm_and_template_labels, :through => :vms_and_templates, :source => :labels
# Only taggings mapped from labels, excluding user-assigned tags.
has_many :vm_and_template_taggings, -> { joins(:tag).merge(Tag.controlled_by_mapping) },
:through => :vms_and_templates, :source => :taggings

validates_presence_of :zone

Expand Down