Class: ContainerLabelTagMapping
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- ContainerLabelTagMapping
- Defined in:
- app/models/container_label_tag_mapping.rb,
app/models/container_label_tag_mapping/mapper.rb
Overview
A mapping matches labels on `resource_type` (NULL means any), `name` (required), and `value` (NULL means any).
Note: `labeled_resource_type` doesn't really need to match the actual label's `resource_type`; it's simply matched against the type argument to Mapper#map_labels, and sometimes is a fake string like 'Vm' or 'Image' instead of a model name.
Different labels might map to same tag, and one label might map to multiple tags.
There are 2 kinds of rows:
-
When `label_value` is specified, we map only this value to a specific `tag`. TODO: drop this. This was never exposed in UI and is dead code to maintain.
-
When `label_value` is NULL, we map this name with any value to per-value tags. In this case, `tag` specifies the category under which to create the value-specific tag (and classification) on demand.
All involved tags must also have a Classification.
TODO: rename, no longer specific to containers.
Defined Under Namespace
Classes: Mapper
Constant Summary collapse
- TAG_PREFIXES =
%w(amazon azure kubernetes openstack).map { |name| "/managed/#{name}:" }.freeze
Class Method Summary collapse
-
.controls_tag?(tag) ⇒ Boolean
Checks whether a Tag record is under mapping control.
-
.mapper ⇒ Object
Return ContainerLabelTagMapping::Mapper instance that holds all current mappings, can compute applicable tags, and create/find Tag records.
-
.retag_entity(entity, tag_references) ⇒ Object
Assign/unassign mapping-controlled tags, preserving user-assigned tags.
Instance Method Summary collapse
Class Method Details
.controls_tag?(tag) ⇒ Boolean
Checks whether a Tag record is under mapping control. TODO: expensive.
39 40 41 42 43 |
# File 'app/models/container_label_tag_mapping.rb', line 39 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 |
.mapper ⇒ Object
Return ContainerLabelTagMapping::Mapper instance that holds all current mappings, can compute applicable tags, and create/find Tag records.
31 32 33 |
# File 'app/models/container_label_tag_mapping.rb', line 31 def self.mapper ContainerLabelTagMapping::Mapper.new(in_my_region.all) end |
.retag_entity(entity, tag_references) ⇒ Object
Assign/unassign mapping-controlled tags, preserving user-assigned tags. All tag references must have been resolved first by Mapper#find_or_create_tags.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/models/container_label_tag_mapping.rb', line 47 def self.retag_entity(entity, tag_references) = Mapper.(tag_references) = entity..controlled_by_mapping Tagging.transaction do ( - ).each do |tag| Tagging.create!(:taggable => entity, :tag => tag) end ( - ).tap do || Tagging.where(:taggable => entity, :tag => .collect(&:id)).destroy_all end end entity..reset entity.taggings.reset end |
Instance Method Details
#validate_tag_prefix ⇒ Object
62 63 64 65 66 |
# File 'app/models/container_label_tag_mapping.rb', line 62 def validate_tag_prefix unless TAG_PREFIXES.any? { |prefix| tag.name.start_with?(prefix) } errors.add(:tag_id, "tag category name #{tag.name} doesn't start with any of #{TAG_PREFIXES}") end end |