Repository: cloud-custodian/cloud-custodian

Issue:
ec2 tag renaming works but gives error
when running policy.yml::
```
policies:
  - name: ec2-fix-costCenter
    resource: ec2
    description:
      if costCenter tag is mislabeled, fix it to camelCase
    filters:
     - or:
       - "tag:CostCenter": present
       - "tag:cost Center": present
    actions:
     - type: rename-tag
       old_key: CostCenter
       new_key: costCenter
```

I receive below output:
```
2017-01-11 20:38:00,458: custodian.policy:INFO Running policy ec2-fix-costCenter resource: ec2 region:default c7n:0.8.22.0
2017-01-11 20:38:01,539: custodian.policy:INFO policy: ec2-fix-costCenter resource:ec2 has count:2 time:1.08
2017-01-11 20:38:01,540: custodian.actions:INFO Filtered from 2 resources to 2
2017-01-11 20:38:01,540: custodian.actions:INFO Renaming tag on 2 instances
2017-01-11 20:38:02,126: custodian.actions:ERROR Exception renaming tag set
 An error occurred (MissingParameter) when calling the CreateTags operation: The request must contain the parameter resourceIdSet
2017-01-11 20:38:02,127: custodian.policy:INFO policy: ec2-fix-costCenter action: renametag resources: 2 execution_time: 0.59
```

in fact, using the rename-tag at all seems to always result in in an error, but still applies correctly


Discussion:
yeah the code for this action looks suspect, its looks like its trying to do some logic based on number of tags to see if it can create the new tag before deleting the old to stay under 50 tag count limit, but ends up trying to create the tags twice, the second time with an empty set of ids which causes this error. introduced in #625 



Resolving patch:
diff --git a/c7n/tags.py b/c7n/tags.py
--- a/c7n/tags.py
+++ b/c7n/tags.py
@@ -382,6 +382,8 @@ class RenameTag(Action):
 
     permissions = ('ec2:CreateTags', 'ec2:DeleteTags')
 
+    tag_count_max = 50
+
     def delete_tag(self, client, ids, key, value):
         client.delete_tags(
             Resources=ids,
@@ -406,20 +408,20 @@ def process_rename(self, tag_value, resource_set):
 
         c = utils.local_session(self.manager.session_factory).client('ec2')
 
-        self.create_tag(
-            c,
-            [r[self.id_key] for r in resource_set if len(
-                r.get('Tags', [])) < 50],
-            new_key, tag_value)
+        # We have a preference to creating the new tag when possible first
+        resource_ids = [r[self.id_key] for r in resource_set if len(
+            r.get('Tags', [])) < self.tag_count_max]
+        if resource_ids:
+            self.create_tag(c, resource_ids, new_key, tag_value)
 
         self.delete_tag(
             c, [r[self.id_key] for r in resource_set], old_key, tag_value)
 
-        self.create_tag(
-            c,
-            [r[self.id_key] for r in resource_set if len(
-                r.get('Tags', [])) > 49],
-            new_key, tag_value)
+        # For resources with 50 tags, we need to delete first and then create.
+        resource_ids = [r[self.id_key] for r in resource_set if len(
+            r.get('Tags', [])) > self.tag_count_max - 1]
+        if resource_ids:
+            self.create_tag(c, resource_ids, new_key, tag_value)
 
     def create_set(self, instances):
         old_key = self.data.get('old_key', None)


Test patch: