Module: Puppet::Util::Tagging

Included in:
Resource, Resource::Catalog, Resource::Status, Transaction, Transaction::Event, Type, Log, SkipTags
Defined in:
lib/puppet/util/tagging.rb

Constant Summary collapse

ValidTagRegex =
/\A[[:alnum:]_][[:alnum:]_:.-]*\Z/

Instance Method Summary collapse

Instance Method Details

#merge_into(tag_set) ⇒ Object

Merge the tags of this instance into the provide TagSet



96
97
98
# File 'lib/puppet/util/tagging.rb', line 96

def merge_into(tag_set)
  tag_set.merge(@tags) unless @tags.nil?
end

#merge_tags(tag_source) ⇒ Object

Merge tags from a tagged instance with no attempts to split, downcase or verify the tags



90
91
92
93
# File 'lib/puppet/util/tagging.rb', line 90

def merge_tags(tag_source)
  @tags ||= new_tags
  tag_source.merge_into(@tags)
end

#raw_tagged?(tag_array) ⇒ Boolean

Answers if this resource is tagged with at least one of the tags given in downcased string form.

The method is a faster variant of the tagged? method that does no conversion of its arguments.

Parameters:

  • tag_array (Array[String])

    array of tags to look for

Returns:

  • (Boolean)

    true if this instance is tagged with at least one of the provided tags



71
72
73
74
# File 'lib/puppet/util/tagging.rb', line 71

def raw_tagged?(tag_array)
  my_tags = self.tags
  !tag_array.index { |t| my_tags.include?(t) }.nil?
end

#set_tags(tag_source) ⇒ Object

Only use this method when copying known tags from one Tagging instance to another



77
78
79
# File 'lib/puppet/util/tagging.rb', line 77

def set_tags(tag_source)
  @tags = tag_source.tags
end

#tag(*ary) ⇒ Object

Add a tag to the current tag set. When a tag set is used for a scope, these tags will be added to all of the objects contained in this scope when the objects are finished.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/puppet/util/tagging.rb', line 10

def tag(*ary)
  @tags ||= new_tags

  ary.flatten.each do |tag|
    name = tag.to_s.downcase
    # Add the tag before testing if it's valid since this means that
    # we never need to test the same valid tag twice. This speeds things
    # up since we get a lot of duplicates and rarely fail on bad tags
    if @tags.add?(name)
      # not seen before, so now we test if it is valid
      if name =~ ValidTagRegex
        if split_qualified_tags?
        # avoid adding twice by first testing if the string contains '::'
          @tags.merge(name.split('::')) if name.include?('::')
        end
      else
        @tags.delete(name)
        fail(Puppet::ParseError, _("Invalid tag '%{name}'") % { name: name })
      end
    end
  end
end

#tag_if_valid(name) ⇒ Object

Add a name to the current tag set. Silently ignore names that does not represent valid tags.

Use this method instead of doing this:

tag(name) if is_valid?(name)

since that results in testing the same string twice



42
43
44
45
46
47
48
49
50
# File 'lib/puppet/util/tagging.rb', line 42

def tag_if_valid(name)
  if name.is_a?(String) && name =~ ValidTagRegex
    name = name.downcase
    @tags ||= new_tags
    if @tags.add?(name) && name.include?('::')
      @tags.merge(name.split('::'))
    end
  end
end

#tagged?(*tags) ⇒ Boolean

Answers if this resource is tagged with at least one of the given tags.

The given tags are converted to downcased strings before the match is performed.

Parameters:

  • *tags (String)

    splat of tags to look for

Returns:

  • (Boolean)

    true if this instance is tagged with at least one of the provided tags



59
60
61
# File 'lib/puppet/util/tagging.rb', line 59

def tagged?(*tags)
  raw_tagged?(tags.collect {|t| t.to_s.downcase})
end

#tagsObject

Return a copy of the tag list, so someone can’t ask for our tags and then modify them.



83
84
85
86
# File 'lib/puppet/util/tagging.rb', line 83

def tags
  @tags ||= new_tags
  @tags.dup
end

#tags=(tags) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/puppet/util/tagging.rb', line 100

def tags=(tags)
  @tags = new_tags

  return if tags.nil?

  tags = tags.strip.split(/\s*,\s*/) if tags.is_a?(String)
  tag(*tags)
end