From ab464f99456d12a9dcc18a216849a89ffd7ede5e Mon Sep 17 00:00:00 2001 From: Vincent Wong Date: Sat, 13 Sep 2014 12:14:26 -0700 Subject: Added method TaggedEC2Object.remove_tags() Pull Request #2259 already created an add_tags method to supplement the add_tag call. Not sure why there isn't an analogous remove_tags method, since the DeleteTags API also supports removng multiple tags at once. An immediate use case is to be able to delete all the tags of an instance; with only remove_tag, this will unnecessarily involve many round trips to AWS. --- boto/ec2/ec2object.py | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/boto/ec2/ec2object.py b/boto/ec2/ec2object.py index 9edf12ee..7cc7e424 100644 --- a/boto/ec2/ec2object.py +++ b/boto/ec2/ec2object.py @@ -65,7 +65,7 @@ class TaggedEC2Object(EC2Object): def add_tag(self, key, value='', dry_run=False): """ - Add a tag to this object. Tag's are stored by AWS and can be used + Add a tag to this object. Tags are stored by AWS and can be used to organize and filter resources. Adding a tag involves a round-trip to the EC2 service. @@ -117,12 +117,12 @@ class TaggedEC2Object(EC2Object): :type value: str :param value: An optional value that can be stored with the tag. - If a value is provided, it must match the value - currently stored in EC2. If not, the tag will not - be removed. If a value of None is provided, all - tags with the specified name will be deleted. - NOTE: There is an important distinction between - a value of '' and a value of None. + If a value is provided, it must match the value currently + stored in EC2. If not, the tag will not be removed. If + a value of None is provided, the tag will be + unconditionally deleted. + NOTE: There is an important distinction between a value + of '' and a value of None. """ if value is not None: tags = {key: value} @@ -135,3 +135,26 @@ class TaggedEC2Object(EC2Object): ) if key in self.tags: del self.tags[key] + + def remove_tags(self, tags, dry_run=False): + """ + Removes tags from this object. Removing tags involves a round-trip + to the EC2 service. + + :type tags: dict + :param tags: A dictionary of key-value pairs for the tags being removed. + For each key, the provided value must match the value + currently stored in EC2. If not, that particular tag will + not be removed. However, if a value of None is provided, + the tag will be unconditionally deleted. + NOTE: There is an important distinction between a value of + '' and a value of None. + """ + status = self.connection.delete_tags( + [self.id], + tags, + dry_run=dry_run + ) + for key, value in tags.iteritems(): + if key in self.tags: + del self.tags[key] -- cgit v1.2.1