-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The Tag resource was incorrectly modeled such that it is not a collection on an Instance. As a result, there is no way to batch delete tags from an Instance resource. This adds that functionality in, in a way that won't break on future EC2 resource changes. In the long term there are plans to codify SDK specific customizations while maintaining a canonically correct resource model, but that will sometime in the future as part of a different story. Fixes #381
- Loading branch information
1 parent
8ce95c8
commit cf3308f
Showing
9 changed files
with
198 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
from boto3.utils import inject_attribute | ||
from boto3.resources.model import Action | ||
from boto3.docs.docstring import ActionDocstring | ||
|
||
|
||
def inject_delete_tags(class_attributes, service_context, emitter, **kwargs): | ||
action_model = { | ||
'request': { | ||
'operation': 'DeleteTags', | ||
'params': [{ | ||
'target': 'Resources[0]', | ||
'source': 'identifier', | ||
'name': 'Id' | ||
}] | ||
} | ||
} | ||
action = Action('delete_tags', action_model, {}) | ||
|
||
delete_tags_action = delete_tags | ||
delete_tags_action.__doc__ = ActionDocstring( | ||
resource_name='Instance', | ||
event_emitter=emitter, | ||
action_model=action, | ||
service_model=service_context.service_model, | ||
include_signature=False | ||
) | ||
|
||
inject_attribute(class_attributes, 'delete_tags', delete_tags_action) | ||
|
||
|
||
def delete_tags(self, **kwargs): | ||
kwargs['Resources'] = [self.id] | ||
return self.meta.client.delete_tags(**kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
from tests.functional.docs import BaseDocsFunctionalTests | ||
|
||
from boto3.session import Session | ||
from boto3.docs.service import ServiceDocumenter | ||
|
||
|
||
class TestInstanceDeleteTags(BaseDocsFunctionalTests): | ||
def setUp(self): | ||
self.documenter = ServiceDocumenter( | ||
'ec2', session=Session(region_name='us-east-1')) | ||
self.generated_contents = self.documenter.document_service() | ||
self.generated_contents = self.generated_contents.decode('utf-8') | ||
|
||
def test_delete_tags_method_is_documented(self): | ||
contents = self.get_class_document_block( | ||
'EC2.Instance', self.generated_contents) | ||
method_contents = self.get_method_document_block( | ||
'delete_tags', contents) | ||
self.assert_contains_lines_in_order([ | ||
'response = instance.delete_tags(', | ||
'DryRun=True|False,', | ||
'Tags=[', | ||
], method_contents) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
import unittest | ||
|
||
import boto3.session | ||
from botocore.stub import Stubber | ||
|
||
|
||
class TestInstanceDeleteTags(unittest.TestCase): | ||
def setUp(self): | ||
self.session = boto3.session.Session(region_name='us-west-2') | ||
self.service_resource = self.session.resource('ec2') | ||
self.instance_resource = self.service_resource.Instance('i-abc123') | ||
|
||
def test_delete_tags_injected(self): | ||
self.assertTrue(hasattr(self.instance_resource, 'delete_tags'), | ||
'delete_tags was not injected onto Instance resource.') | ||
|
||
def test_delete_tags(self): | ||
stubber = Stubber(self.instance_resource.meta.client) | ||
stubber.add_response('delete_tags', {}) | ||
stubber.activate() | ||
response = self.instance_resource.delete_tags(Tags=[{'Key': 'foo'}]) | ||
stubber.assert_no_pending_responses() | ||
self.assertEqual(response, {}) | ||
stubber.deactivate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the 'License'). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the 'license' file accompanying this file. This file is | ||
# distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
import unittest | ||
import mock | ||
|
||
from boto3.ec2.deletetags import delete_tags | ||
|
||
|
||
class TestDeleteTags(unittest.TestCase): | ||
def setUp(self): | ||
self.client = mock.Mock() | ||
self.resource = mock.Mock() | ||
self.resource.meta.client = self.client | ||
self.instance_id = 'instance_id' | ||
self.resource.id = self.instance_id | ||
|
||
def test_delete_tags(self): | ||
tags = { | ||
'Tags': [ | ||
{'Key': 'key1', 'Value': 'value1'}, | ||
{'Key': 'key2', 'Value': 'value2'}, | ||
{'Key': 'key3', 'Value': 'value3'} | ||
] | ||
} | ||
|
||
delete_tags(self.resource, **tags) | ||
|
||
kwargs = tags | ||
kwargs['Resources'] = [self.instance_id] | ||
self.client.delete_tags.assert_called_with(**kwargs) |