Skip to content
This repository
Browse code

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

…tags need; added create_tags method
  • Loading branch information...
commit 8cd676b1261f257963a94ba6f419007f0164c0c1 1 parent 6e1d8e2
authored
33  lib/AWS.rb
@@ -232,6 +232,38 @@ def pathhashlist(key, arr_of_hashes, mappings)
232 232
         params
233 233
       end
234 234
 
  235
+      # Same as _pathhashlist_ except it generates explicit <prefix>.Key= and <prefix>.Value or <prefix>.Value.1, <prefix>.Value.2
  236
+      # depending on whether the value is a scalar or an array.
  237
+      #
  238
+      # So if you pass in args
  239
+      # ("People", [{:name=>'jon'}, {:names=>['chris', 'bob']} with key_name = 'Key' and value_name = 'Value',
  240
+      # you should get
  241
+      # {"People.1.Key"=>"name", "People.1.Value"=>'jon', "People.2.Key"=>'names', 'People.2.Value.1'=>'chris', 'People.2.Value.2'=>'bob'}
  242
+      def pathkvlist(key, arr_of_hashes, key_name, value_name, mappings)
  243
+        raise ArgumentError, "expected a key that is a String" unless key.is_a? String
  244
+        raise ArgumentError, "expected a arr_of_hashes that is an Array" unless arr_of_hashes.is_a? Array
  245
+        arr_of_hashes.each{|h| raise ArgumentError, "expected each element of arr_of_hashes to be a Hash" unless h.is_a?(Hash)}
  246
+        raise ArgumentError, "expected a key_nam that is a String" unless key_name.is_a? String
  247
+        raise ArgumentError, "expected a value_name that is a String" unless value_name.is_a? String
  248
+        raise ArgumentError, "expected a mappings that is an Hash" unless mappings.is_a? Hash
  249
+        params = {}
  250
+        arr_of_hashes.each_with_index do |hash, i|
  251
+          hash.each do |attribute, value|
  252
+            params["#{key}.#{i+1}.#{key_name}"] = mappings.fetch(attribute, attribute)
  253
+            if !value.nil?
  254
+              if value.is_a? Array
  255
+                value.each_with_index do |item, j|
  256
+                  params["#{key}.#{i+1}.#{value_name}.#{j+1}"] = item.to_s
  257
+                end
  258
+              else
  259
+                params["#{key}.#{i+1}.#{value_name}"] = value.to_s
  260
+              end
  261
+            end
  262
+          end
  263
+        end
  264
+        params
  265
+      end
  266
+
235 267
       # Make the connection to AWS EC2 passing in our request.  This is generally called from
236 268
       # within a 'Response' class object or one of its sub-classes so the response is interpreted
237 269
       # in its proper context.  See lib/EC2/responses.rb
@@ -259,6 +291,7 @@ def make_request(action, params, data='')
259 291
           req.content_type = 'application/x-www-form-urlencoded'
260 292
           req['User-Agent'] = "github-amazon-ec2-ruby-gem"
261 293
 
  294
+puts(query)
262 295
           response = @http.request(req, query)
263 296
 
264 297
           # Make a call to see if we need to throw an error based on the response given by EC2
20  lib/AWS/EC2/tags.rb
@@ -2,14 +2,28 @@ module AWS
2 2
   module EC2
3 3
     class Base < AWS::Base
4 4
 
  5
+      # The CreateTags operation adds or overwrites tags for the specified resource(s).
  6
+      #
  7
+      # @option options [Array] :resource_id ([]) The ids of the resource(s) to tag
  8
+      # @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
  9
+      #
  10
+      def create_tags( options = {} )
  11
+        raise ArgumentError, "No :resource_id provided" if options[:resource_id].nil? || options[:resource_id].empty?
  12
+        raise ArgumentError, "No :tag provided" if options[:tag].nil? || options[:tag].empty?
  13
+
  14
+        params = pathlist("ResourceId", options[:resource_id] )
  15
+        params.merge!(pathkvlist('Tag', options[:tag], 'Key', 'Value', {}))
  16
+        return response_generator(:action => "CreateTags", :params => params)
  17
+      end
  18
+
5 19
       # The DescribeTags operation lists the tags, If you do not specify any filters, all tags will be returned.
6 20
       #
7  
-      # @option options [optional, Array] :filters ([]) An array of Hashes representing the filters.  e.g. [{:key => 'name'}, {:resource_type => 'instance'},...]
  21
+      # @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']},...]
8 22
       #
9 23
       def describe_tags( options = {} )
10 24
         params = {}
11  
-        if options[:filters]
12  
-          params.merge!(pathhashlist('Filter', options[:filters], {:key => 'key', :resource_id => 'resource-id', :resource_type => 'resource-type', :value => 'value' }))
  25
+        if options[:filter]
  26
+          params.merge!(pathkvlist('Filter', options[:filter], 'Name', 'Value', {:resource_id => 'resource-id', :resource_type => 'resource-type' }))
13 27
         end
14 28
         return response_generator(:action => "DescribeTags", :params => params)
15 29
       end

0 notes on commit 8cd676b

Please sign in to comment.
Something went wrong with that request. Please try again.