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
@private
AWS::Core::Model::new
# 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
@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
Changes the value of a tag. @param [String or Symbol] key The key of the tag to set. @param [String] value The new value. If this is nil, the tag will
be deleted.
# File lib/aws/ec2/resource_tag_collection.rb, line 86 def []=(key, value) if value @tags.create(@resource, key.to_s, :value => value) else delete(key) end end
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 98 def add(key) @tags.create(@resource, key.to_s) end
Removes all tags from the resource.
# File lib/aws/ec2/resource_tag_collection.rb, line 144 def clear client.delete_tags(:resources => [@resource.send(:__resource_id__)]) end
Deletes the tags with the given keys (which may be strings or symbols).
# File lib/aws/ec2/resource_tag_collection.rb, line 135 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
@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 151 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
@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
@param [String or Symbol] key The key of the tag to check. @return [Boolean] True if the resource has a tag for the given key.
# File lib/aws/ec2/resource_tag_collection.rb, line 62 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
@param [String or Symbol] value The value to check. @return [Boolean] True if the resource has a tag with the given value.
# File lib/aws/ec2/resource_tag_collection.rb, line 74 def has_value?(value) if cached = cached_tags return cached.values.include?(value) end !@tags.filter("value", value.to_s).to_a.empty? end
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 123 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
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 109 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
@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 190 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
@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 170 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