class AWS::EC2::ResourceTagCollection

i.tags.stage # => “production”
i.tags.stage = “production”
i.tags.clear
i.tags.to_h # => { “foo” => “bar”, … }
i = ec2.instances[“i-123”]
@example Manipulating the tags of an EC2 instance
Represents the EC2 tags associated with a single resource.

def [](key)

Parameters:
  • key (String or Symbol) -- The key of the tag to return.

Returns:
  • (String) - The value of the tag with the given key, or
def [](key)
  if cached = cached_tags
    return cached[key.to_s]
  end
  Tag.new(@resource, key, :config => config).value
rescue Resource::NotFound => e
  nil
end

def []=(key, value)

Parameters:
  • value (String) -- The new value. If this is nil, the tag will
  • key (String or Symbol) -- The key of the tag to set.
def []=(key, value)
  if value
    @tags.create(@resource, key.to_s, :value => value)
  else
    delete(key)
  end
end

def add(key)

Parameters:
  • key (String or Symbol) -- The key of the new tag.
def add(key)
  @tags.create(@resource, key.to_s)
end

def cached_tags

def cached_tags
  if @resource.respond_to?(:cached_tags) and
      cached = @resource.cached_tags
    cached
  end
end

def clear

Removes all tags from the resource.
def clear
  client.delete_tags(:resources => [@resource.send(:__resource_id__)])
end

def delete(*keys)

or symbols).
Deletes the tags with the given keys (which may be strings
def delete(*keys)
  return if keys.empty?
  client.delete_tags(:resources => [@resource.send(:__resource_id__)],
                     :tags => keys.map do |key|
                       { :key => key.to_s }
                     end)
end

def each(&blk)

Other tags:
    Yield: - The key/value pairs of each tag
def each(&blk)
  if cached = cached_tags
    cached.each(&blk)
    return
  end
  @tags.filtered_request(:describe_tags).tag_set.each do |tag|
    if blk.arity == 2
      yield(tag.key, tag.value)
    else
      yield([tag.key, tag.value])
    end
  end
  nil
end

def empty?

Returns:
  • (Boolean) - True if the resource has no tags.
def empty?
  if cached = cached_tags
    return cached.empty?
  end
  @tags.to_a.empty?
end

def has_key?(key)

Returns:
  • (Boolean) - True if the resource has a tag for the given key.

Parameters:
  • key (String or Symbol) -- The key of the tag to check.
def has_key?(key)
  if cached = cached_tags
    return cached.has_key?(key.to_s)
  end
  !@tags.filter("key", key.to_s).to_a.empty?
end

def has_value?(value)

Returns:
  • (Boolean) - True if the resource has a tag with the given value.

Parameters:
  • value (String or Symbol) -- The value to check.
def has_value?(value)
  if cached = cached_tags
    return cached.values.include?(value)
  end
  !@tags.filter("value", value.to_s).to_a.empty?
end

def initialize(resource, opts = {})

Other tags:
    Private: -
def initialize(resource, opts = {})
  @resource = resource
  super(opts)
  @tags = TagCollection.new(:config => config).
    filter("resource-id", @resource.send(:__resource_id__)).
    filter("resource-type", @resource.tagging_resource_type)
end

def method_missing(m, *args)

tags.color # => "red"
tags.color = "red"

methods. For example:
Allows setting and getting individual tags through instance
def method_missing(m, *args)
  if m.to_s[-1,1] == "="
    self.send(:[]=, m.to_s[0...-1], *args)
  elsif args.empty?
    self[m]
  else
    super
  end
end

def set(tags)

Parameters:
  • tags (Hash) -- The tags to set. The keys of the hash
def set(tags)
  client.create_tags(:resources => [@resource.send(:__resource_id__)],
                     :tags => tags.map do |(key, value)|
                       { :key => key.to_s,
                         :value => value }
                     end)
end

def to_h

Returns:
  • (Hash) - The current tags as a hash, where the keys
def to_h
  if cached = cached_tags
    return cached
  end
  @tags.filtered_request(:describe_tags).tag_set.inject({}) do |hash, tag|
    hash[tag.key] = tag.value
    hash
  end
end

def values_at(*keys)

Returns:
  • (Array) - An array of the tag values associated with
def values_at(*keys)
  if cached = cached_tags
    return cached.values_at(*keys.map { |k| k.to_s })
  end
  keys = keys.map { |k| k.to_s }
  tag_set = @tags.
    filter("key", *keys).
    filtered_request(:describe_tags).tag_set
  hash = tag_set.inject({}) do |hash, tag|
    hash[tag.key] = tag.value
    hash
  end
  keys.map do |key|
    hash[key]
  end
end