Skip to content
This repository has been archived by the owner on Jan 20, 2019. It is now read-only.

Commit

Permalink
Experimental support for EC2 tags provided by tomdz.
Browse files Browse the repository at this point in the history
  • Loading branch information
grempe committed Nov 21, 2010
2 parents da53f4b + 1b7f6c2 commit 098e528
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Expand Up @@ -2,6 +2,12 @@


=== 0.9.17 2010-11-21 === 0.9.17 2010-11-21
* Converted from Jeweler to Bundler, 'bundle install' to install dependencies * Converted from Jeweler to Bundler, 'bundle install' to install dependencies
* Adding ability to launch t1.micro instances for ec2 regular and spot price
instances
* Adding support for describe/modify/reset instance attributes with tests.
* Updated to API version : 2010-08-31
* Added experimental support for EC2 create_tags, describe_tags, delete_tags



=== 0.9.16 2010-07-07 === 0.9.16 2010-07-07
* Less strict checking on which server endpoints you can pass in as an environment var. * Less strict checking on which server endpoints you can pass in as an environment var.
Expand Down
42 changes: 41 additions & 1 deletion lib/AWS.rb
Expand Up @@ -219,7 +219,46 @@ def pathhashlist(key, arr_of_hashes, mappings)
params = {} params = {}
arr_of_hashes.each_with_index do |hash, i| arr_of_hashes.each_with_index do |hash, i|
hash.each do |attribute, value| hash.each do |attribute, value|
params["#{key}.#{i+1}.#{mappings[attribute]}"] = value.to_s if value.is_a? Array
params["#{key}.#{i+1}.Name"] = mappings[attribute]
value.each_with_index do |item, j|
params["#{key}.#{i+1}.Value.#{j+1}"] = item.to_s
end
else
params["#{key}.#{i+1}.#{mappings[attribute]}"] = value.to_s
end
end
end
params
end

# Same as _pathhashlist_ except it generates explicit <prefix>.Key= and <prefix>.Value or <prefix>.Value.1, <prefix>.Value.2
# depending on whether the value is a scalar or an array.
#
# So if you pass in args
# ("People", [{:name=>'jon'}, {:names=>['chris', 'bob']} with key_name = 'Key' and value_name = 'Value',
# you should get
# {"People.1.Key"=>"name", "People.1.Value"=>'jon', "People.2.Key"=>'names', 'People.2.Value.1'=>'chris', 'People.2.Value.2'=>'bob'}
def pathkvlist(key, arr_of_hashes, key_name, value_name, mappings)
raise ArgumentError, "expected a key that is a String" unless key.is_a? String
raise ArgumentError, "expected a arr_of_hashes that is an Array" unless arr_of_hashes.is_a? Array
arr_of_hashes.each{|h| raise ArgumentError, "expected each element of arr_of_hashes to be a Hash" unless h.is_a?(Hash)}
raise ArgumentError, "expected a key_nam that is a String" unless key_name.is_a? String
raise ArgumentError, "expected a value_name that is a String" unless value_name.is_a? String
raise ArgumentError, "expected a mappings that is an Hash" unless mappings.is_a? Hash
params = {}
arr_of_hashes.each_with_index do |hash, i|
hash.each do |attribute, value|
params["#{key}.#{i+1}.#{key_name}"] = mappings.fetch(attribute, attribute)
if !value.nil?
if value.is_a? Array
value.each_with_index do |item, j|
params["#{key}.#{i+1}.#{value_name}.#{j+1}"] = item.to_s
end
else
params["#{key}.#{i+1}.#{value_name}"] = value.to_s
end
end
end end
end end
params params
Expand Down Expand Up @@ -282,6 +321,7 @@ def response_generator( options = {} )


http_response = make_request(options[:action], options[:params]) http_response = make_request(options[:action], options[:params])
http_xml = http_response.body http_xml = http_response.body

return Response.parse(:xml => http_xml) return Response.parse(:xml => http_xml)
end end


Expand Down
2 changes: 1 addition & 1 deletion lib/AWS/EC2.rb
Expand Up @@ -14,7 +14,7 @@ module EC2
DEFAULT_HOST = 'ec2.amazonaws.com' DEFAULT_HOST = 'ec2.amazonaws.com'
end end


API_VERSION = '2009-11-30' API_VERSION = '2010-08-31'


class Base < AWS::Base class Base < AWS::Base
def api_version def api_version
Expand Down
47 changes: 47 additions & 0 deletions lib/AWS/EC2/tags.rb
@@ -0,0 +1,47 @@
module AWS
module EC2
class Base < AWS::Base

# The CreateTags operation adds or overwrites tags for the specified resource(s).
#
# @option options [Array] :resource_id ([]) The ids of the resource(s) to tag
# @option options [Array] :tag ([]) An array of Hashes representing the tags { tag_name => tag_value }. Use nil or empty string to get a tag without a value
#
def create_tags( options = {} )
raise ArgumentError, "No :resource_id provided" if options[:resource_id].nil? || options[:resource_id].empty?
raise ArgumentError, "No :tag provided" if options[:tag].nil? || options[:tag].empty?

params = pathlist("ResourceId", options[:resource_id] )
params.merge!(pathkvlist('Tag', options[:tag], 'Key', 'Value', {}))
return response_generator(:action => "CreateTags", :params => params)
end

# The DescribeTags operation lists the tags, If you do not specify any filters, all tags will be returned.
#
# @option options [optional, Array] :filter ([]) An array of Hashes representing the filters. Note that the values in the hashes have to be arrays. E.g. [{:key => ['name']}, {:resource_type => ['instance']},...]
#
def describe_tags( options = {} )
params = {}
if options[:filter]
params.merge!(pathkvlist('Filter', options[:filter], 'Name', 'Value', {:resource_id => 'resource-id', :resource_type => 'resource-type' }))
end
return response_generator(:action => "DescribeTags", :params => params)
end

# The DeleteTags operation deletes tags for the specified resource(s).
#
# @option options [Array] :resource_id ([]) The ids of the resource(s) to tag
# @option options [Array] :tag ([]) An array of Hashes representing the tags { tag_name => tag_value }. If a value is given (instead of nil/empty string), then the tag is only deleted if it has this value
#
def delete_tags( options = {} )
raise ArgumentError, "No :resource_id provided" if options[:resource_id].nil? || options[:resource_id].empty?
raise ArgumentError, "No :tag provided" if options[:tag].nil? || options[:tag].empty?

params = pathlist("ResourceId", options[:resource_id] )
params.merge!(pathkvlist('Tag', options[:tag], 'Key', 'Value', {}))
return response_generator(:action => "DeleteTags", :params => params)
end
end
end
end

0 comments on commit 098e528

Please sign in to comment.