Permalink
Browse files

Created separate function for the key-value parameter structure that …

…tags need; added create_tags method
  • Loading branch information...
1 parent 6e1d8e2 commit 8cd676b1261f257963a94ba6f419007f0164c0c1 Thomas Dudziak committed Oct 16, 2010
Showing with 50 additions and 3 deletions.
  1. +33 −0 lib/AWS.rb
  2. +17 −3 lib/AWS/EC2/tags.rb
View
@@ -232,6 +232,38 @@ def pathhashlist(key, arr_of_hashes, mappings)
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
+ params
+ end
+
# Make the connection to AWS EC2 passing in our request. This is generally called from
# within a 'Response' class object or one of its sub-classes so the response is interpreted
# in its proper context. See lib/EC2/responses.rb
@@ -259,6 +291,7 @@ def make_request(action, params, data='')
req.content_type = 'application/x-www-form-urlencoded'
req['User-Agent'] = "github-amazon-ec2-ruby-gem"
+puts(query)
response = @http.request(req, query)
# Make a call to see if we need to throw an error based on the response given by EC2
View
@@ -2,14 +2,28 @@ 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] :filters ([]) An array of Hashes representing the filters. e.g. [{:key => 'name'}, {:resource_type => 'instance'},...]
+ # @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[:filters]
- params.merge!(pathhashlist('Filter', options[:filters], {:key => 'key', :resource_id => 'resource-id', :resource_type => 'resource-type', :value => 'value' }))
+ 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

0 comments on commit 8cd676b

Please sign in to comment.