Class: AWS::EC2::ResourceTagCollection

Inherits:
Object
  • Object
show all
Includes:
Core::Model, 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 Attribute Summary

Attributes included from Core::Model

#config

Instance Method Summary collapse

Methods included from Core::Model

#client, #config_prefix, #inspect

Constructor Details

#initialize(resource, opts = {}) ⇒ ResourceTagCollection



31
32
33
34
35
36
37
# 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

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"


123
124
125
126
127
128
129
130
131
# 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

Instance Method Details

#[](key) ⇒ String



43
44
45
46
47
48
49
50
# 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) ⇒ Object Also known as: store

Changes the value of a tag.



86
87
88
89
90
91
92
# 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

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

Adds a tag with a blank value.



98
99
100
# File 'lib/aws/ec2/resource_tag_collection.rb', line 98

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

#clearObject

Removes all tags from the resource.



144
145
146
# File 'lib/aws/ec2/resource_tag_collection.rb', line 144

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



135
136
137
138
139
140
141
# 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

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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# 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

#empty?Boolean



53
54
55
56
57
58
# 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) ⇒ Boolean Also known as: key?, include?, member?



62
63
64
65
66
67
# 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

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



74
75
76
77
78
79
# 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

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

Sets multiple tags in a single request.



109
110
111
112
113
114
115
# 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

#to_hHash



190
191
192
193
194
195
196
197
198
# 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

#values_at(*keys) ⇒ Array



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# 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