class AWS::EC2::ResourceTagCollection

Represents the EC2 tags associated with a single resource.

@example Manipulating the tags of an EC2 instance

i = ec2.instances["i-123"]
i.tags.to_h                  # => { "foo" => "bar", ... }
i.tags.clear
i.tags.stage = "production"
i.tags.stage                 # => "production"

Public Class Methods

new(resource, opts = {}) click to toggle source

@private

# File lib/aws/ec2/resource_tag_collection.rb, line 31
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

Public Instance Methods

<<(key) click to toggle source
Alias for: add
[](key) click to toggle source

@return [String] The value of the tag with the given key, or

nil if no such tag exists.

@param [String or Symbol] key The key of the tag to return.

# File lib/aws/ec2/resource_tag_collection.rb, line 43
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
[]=(key, value) click to toggle source

Changes the value of a tag.

@param [String or Symbol] The key of the tag to set.

@param [String] The new value. If this is nil, the tag will

be deleted.
# File lib/aws/ec2/resource_tag_collection.rb, line 90
def []=(key, value)
  if value
    @tags.create(@resource, key.to_s, :value => value)
  else
    delete(key)
  end
end
Also aliased as: store
add(key) click to toggle source

Adds a tag with a blank value.

@param [String or Symbol] key The key of the new tag.

# File lib/aws/ec2/resource_tag_collection.rb, line 102
def add(key)
  @tags.create(@resource, key.to_s)
end
Also aliased as: <<
clear() click to toggle source

Removes all tags from the resource.

# File lib/aws/ec2/resource_tag_collection.rb, line 148
def clear
  client.delete_tags(:resources => [@resource.send(:__resource_id__)])
end
delete(*keys) click to toggle source

Deletes the tags with the given keys (which may be strings or symbols).

# File lib/aws/ec2/resource_tag_collection.rb, line 139
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
each() { |key, value| ... } click to toggle source

@yield [key, value] The key/value pairs of each tag

associated with the resource.  If the block has an arity
of 1, the key and value will be yielded in an aray.
# File lib/aws/ec2/resource_tag_collection.rb, line 155
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
Also aliased as: each_pair
each_pair(&blk) click to toggle source
Alias for: each
empty?() click to toggle source

@return [Boolean] True if the resource has no tags.

# File lib/aws/ec2/resource_tag_collection.rb, line 53
def empty?
  if cached = cached_tags
    return cached.empty?
  end
  @tags.to_a.empty?
end
has_key?(key) click to toggle source

@return [Boolean] True if the resource has a tag for the given key.

@param [String or Symbol] The key of the tag to check.

# File lib/aws/ec2/resource_tag_collection.rb, line 63
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
Also aliased as: key?, include?, member?
has_value?(value) click to toggle source

@return [Boolean] True if the resource has a tag with the given value.

@param [String or Symbol] The value to check.

# File lib/aws/ec2/resource_tag_collection.rb, line 76
def has_value?(value)
  if cached = cached_tags
    return cached.values.include?(value)
  end
  !@tags.filter("value", value.to_s).to_a.empty?
end
Also aliased as: value?
include?(key) click to toggle source
Alias for: has_key?
key?(key) click to toggle source
Alias for: has_key?
member?(key) click to toggle source
Alias for: has_key?
method_missing(m, *args) click to toggle source

Allows setting and getting individual tags through instance methods. For example:

tags.color = "red"
tags.color         # => "red"
# File lib/aws/ec2/resource_tag_collection.rb, line 127
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
set(tags) click to toggle source

Sets multiple tags in a single request.

@param [Hash] tags The tags to set. The keys of the hash

may be strings or symbols, and the values must be strings.
Note that there is no way to both set and delete tags
simultaneously.
# File lib/aws/ec2/resource_tag_collection.rb, line 113
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
Also aliased as: update
store(key, value) click to toggle source
Alias for: []=
to_h() click to toggle source

@return [Hash] The current tags as a hash, where the keys

are the tag keys as strings and the values are the tag
values as strings.
# File lib/aws/ec2/resource_tag_collection.rb, line 194
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
update(tags) click to toggle source
Alias for: set
value?(value) click to toggle source
Alias for: has_value?
values_at(*keys) click to toggle source

@return [Array] An array of the tag values associated with

the given keys.  An entry for a key that has no value
(i.e. there is no such tag) will be nil.
# File lib/aws/ec2/resource_tag_collection.rb, line 174
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