Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2259 from mindprince/add_tags
Browse files Browse the repository at this point in the history
Add method TaggedEC2Object.add_tags(). Fixes #2259.
  • Loading branch information
danielgtaylor committed Jun 6, 2014
2 parents 4d44530 + 1bb84b8 commit eea5467
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 1 deletion.
23 changes: 22 additions & 1 deletion boto/ec2/ec2object.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ def add_tag(self, key, value='', dry_run=False):
self.tags = TagSet()
self.tags[key] = value

def add_tags(self, tags, dry_run=False):
"""
Add tags to this object. Tags are stored by AWS and can be used
to organize and filter resources. Adding tags involves a round-trip
to the EC2 service.
:type tags: dict
:param tags: A dictionary of key-value pairs for the tags being stored.
If for some tags you want only the name and no value, the
corresponding value for that tag name should be an empty
string.
"""
status = self.connection.create_tags(
[self.id],
tags,
dry_run=dry_run
)
if self.tags is None:
self.tags = TagSet()
self.tags.update(tags)

def remove_tag(self, key, value=None, dry_run=False):
"""
Remove a tag from this object. Removing a tag involves a round-trip
Expand All @@ -102,7 +123,7 @@ def remove_tag(self, key, value=None, dry_run=False):
NOTE: There is an important distinction between
a value of '' and a value of None.
"""
if value:
if value is not None:
tags = {key : value}
else:
tags = [key]
Expand Down
145 changes: 145 additions & 0 deletions tests/unit/ec2/test_ec2object.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#!/usr/bin/env python

from tests.unit import unittest
from tests.unit import AWSMockServiceTestCase

from boto.ec2.connection import EC2Connection
from boto.ec2.ec2object import TaggedEC2Object


CREATE_TAGS_RESPONSE = r"""<?xml version="1.0" encoding="UTF-8"?>
<CreateTagsResponse xmlns="http://ec2.amazonaws.com/doc/2014-05-01/">
<requestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</requestId>
<return>true</return>
</CreateTagsResponse>
"""


DELETE_TAGS_RESPONSE = r"""<?xml version="1.0" encoding="UTF-8"?>
<DeleteTagsResponse xmlns="http://ec2.amazonaws.com/doc/2014-05-01/">
<requestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</requestId>
<return>true</return>
</DeleteTagsResponse>
"""


class TestAddTags(AWSMockServiceTestCase):
connection_class = EC2Connection

def default_body(self):
return CREATE_TAGS_RESPONSE

def test_add_tag(self):
self.set_http_response(status_code=200)
taggedEC2Object = TaggedEC2Object(self.service_connection)
taggedEC2Object.id = "i-abcd1234"
taggedEC2Object.tags["already_present_key"] = "already_present_value"

taggedEC2Object.add_tag("new_key", "new_value")

self.assert_request_parameters({
'ResourceId.1': 'i-abcd1234',
'Action': 'CreateTags',
'Tag.1.Key': 'new_key',
'Tag.1.Value': 'new_value'},
ignore_params_values=['AWSAccessKeyId', 'SignatureMethod',
'SignatureVersion', 'Timestamp',
'Version'])

self.assertEqual(taggedEC2Object.tags, {
"already_present_key":"already_present_value",
"new_key":"new_value"})

def test_add_tags(self):
self.set_http_response(status_code=200)
taggedEC2Object = TaggedEC2Object(self.service_connection)
taggedEC2Object.id = "i-abcd1234"
taggedEC2Object.tags["already_present_key"] = "already_present_value"

taggedEC2Object.add_tags({"key1":"value1", "key2":"value2"})

self.assert_request_parameters({
'ResourceId.1': 'i-abcd1234',
'Action': 'CreateTags',
'Tag.1.Key': 'key1',
'Tag.1.Value': 'value1',
'Tag.2.Key': 'key2',
'Tag.2.Value': 'value2'},
ignore_params_values=['AWSAccessKeyId', 'SignatureMethod',
'SignatureVersion', 'Timestamp',
'Version'])

self.assertEqual(taggedEC2Object.tags, {
"already_present_key":"already_present_value",
"key1":"value1",
"key2": "value2"})


class TestRemoveTags(AWSMockServiceTestCase):
connection_class = EC2Connection

def default_body(self):
return DELETE_TAGS_RESPONSE

def test_remove_tag(self):
self.set_http_response(status_code=200)
taggedEC2Object = TaggedEC2Object(self.service_connection)
taggedEC2Object.id = "i-abcd1234"
taggedEC2Object.tags["key1"] = "value1"
taggedEC2Object.tags["key2"] = "value2"

taggedEC2Object.remove_tag("key1", "value1")

self.assert_request_parameters({
'ResourceId.1': 'i-abcd1234',
'Action': 'DeleteTags',
'Tag.1.Key': 'key1',
'Tag.1.Value': 'value1'},
ignore_params_values=['AWSAccessKeyId', 'SignatureMethod',
'SignatureVersion', 'Timestamp',
'Version'])

self.assertEqual(taggedEC2Object.tags, {"key2":"value2"})

def test_remove_tag_no_value(self):
self.set_http_response(status_code=200)
taggedEC2Object = TaggedEC2Object(self.service_connection)
taggedEC2Object.id = "i-abcd1234"
taggedEC2Object.tags["key1"] = "value1"
taggedEC2Object.tags["key2"] = "value2"

taggedEC2Object.remove_tag("key1")

self.assert_request_parameters({
'ResourceId.1': 'i-abcd1234',
'Action': 'DeleteTags',
'Tag.1.Key': 'key1'},
ignore_params_values=['AWSAccessKeyId', 'SignatureMethod',
'SignatureVersion', 'Timestamp',
'Version'])

self.assertEqual(taggedEC2Object.tags, {"key2":"value2"})

def test_remove_tag_empty_value(self):
self.set_http_response(status_code=200)
taggedEC2Object = TaggedEC2Object(self.service_connection)
taggedEC2Object.id = "i-abcd1234"
taggedEC2Object.tags["key1"] = "value1"
taggedEC2Object.tags["key2"] = "value2"

taggedEC2Object.remove_tag("key1", "")

self.assert_request_parameters({
'ResourceId.1': 'i-abcd1234',
'Action': 'DeleteTags',
'Tag.1.Key': 'key1',
'Tag.1.Value': ''},
ignore_params_values=['AWSAccessKeyId', 'SignatureMethod',
'SignatureVersion', 'Timestamp',
'Version'])

self.assertEqual(taggedEC2Object.tags, {"key2":"value2"})


if __name__ == '__main__':
unittest.main()

0 comments on commit eea5467

Please sign in to comment.