Class: AWS::EC2::ResourceTagCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/aws/ec2/resource_tag_collection.rb

Overview

Represents the EC2 tags associated with a single resource.

Examples:

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"

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

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

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


132
133
134
135
136
137
138
# File 'lib/aws/ec2/resource_tag_collection.rb', line 132

def method_missing(m, *args)
  if m.to_s[-1,1] == "="
    send(:[]=, m.to_s[0...-1], *args)
  else
    super
  end
end

Instance Method Details

#[](key) ⇒ String

Returns The value of the tag with the given key, or nil if no such tag exists.

Parameters:

  • key (String or Symbol)

    The key of the tag to return.

Returns:

  • (String)

    The value of the tag with the given key, or nil if no such tag exists.



48
49
50
51
52
53
54
55
# File 'lib/aws/ec2/resource_tag_collection.rb', line 48

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) ⇒ Object Also known as: store

Changes the value of a tag.

Parameters:

  • The (String or Symbol)

    key of the tag to set.

  • The (String)

    new value. If this is nil, the tag will be deleted.



95
96
97
98
99
100
101
# File 'lib/aws/ec2/resource_tag_collection.rb', line 95

def []=(key, value)
  if value
    @tags.create(@resource, key.to_s, :value => value)
  else
    delete(key)
  end
end

#add(key) ⇒ Object Also known as: <<

Adds a tag with a blank value.

Parameters:

  • key (String or Symbol)

    The key of the new tag.



107
108
109
# File 'lib/aws/ec2/resource_tag_collection.rb', line 107

def add(key)
  @tags.create(@resource, key.to_s)
end

#clearObject

Removes all tags from the resource.



151
152
153
# File 'lib/aws/ec2/resource_tag_collection.rb', line 151

def clear
  client.delete_tags(:resources => [@resource.send(:__resource_id__)])
end

#delete(*keys) ⇒ Object

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



142
143
144
145
146
147
148
# File 'lib/aws/ec2/resource_tag_collection.rb', line 142

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| ... } ⇒ Object Also known as: each_pair

Yields:

  • (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.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/aws/ec2/resource_tag_collection.rb', line 158

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

#empty?Boolean

Returns True if the resource has no tags.

Returns:

  • (Boolean)

    True if the resource has no tags.



58
59
60
61
62
63
# File 'lib/aws/ec2/resource_tag_collection.rb', line 58

def empty?
  if cached = cached_tags
    return cached.empty?
  end
  @tags.to_a.empty?
end

#has_key?(key) ⇒ Boolean Also known as: key?, include?, member?

Returns True if the resource has a tag for the given key.

Parameters:

  • The (String or Symbol)

    key of the tag to check.

Returns:

  • (Boolean)

    True if the resource has a tag for the given key.



68
69
70
71
72
73
# File 'lib/aws/ec2/resource_tag_collection.rb', line 68

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

#has_value?(value) ⇒ Boolean Also known as: value?

Returns True if the resource has a tag with the given value.

Parameters:

  • The (String or Symbol)

    value to check.

Returns:

  • (Boolean)

    True if the resource has a tag with the given value.



81
82
83
84
85
86
# File 'lib/aws/ec2/resource_tag_collection.rb', line 81

def has_value?(value)
  if cached = cached_tags
    return cached.values.include?(value)
  end
  !@tags.filter("value", value.to_s).to_a.empty?
end

#set(tags) ⇒ Object Also known as: update

Sets multiple tags in a single request.

Parameters:

  • tags (Hash)

    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.



118
119
120
121
122
123
124
# File 'lib/aws/ec2/resource_tag_collection.rb', line 118

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

#to_hHash

Returns The current tags as a hash, where the keys are the tag keys as strings and the values are the tag values as strings.

Returns:

  • (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.



197
198
199
200
201
202
203
204
205
# File 'lib/aws/ec2/resource_tag_collection.rb', line 197

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

#values_at(*keys) ⇒ Array

Returns 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.

Returns:

  • (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.



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/aws/ec2/resource_tag_collection.rb', line 177

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