| 1 |
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 |
# may not use this file except in compliance with the License. A copy of |
| 5 |
# the License is located at |
| 6 |
# |
| 7 |
# https://aws.amazon.com/apache2.0/ |
| 8 |
# |
| 9 |
# or in the "license" file accompanying this file. This file is |
| 10 |
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 |
# ANY KIND, either express or implied. See the License for the specific |
| 12 |
# language governing permissions and limitations under the License. |
| 13 |
|
| 14 |
|
| 15 |
def inject_create_tags(event_name, class_attributes, **kwargs): |
| 16 |
"""This injects a custom create_tags method onto the ec2 service resource |
| 17 |
|
| 18 |
This is needed because the resource model is not able to express |
| 19 |
creating multiple tag resources based on the fact you can apply a set |
| 20 |
of tags to multiple ec2 resources. |
| 21 |
""" |
| 22 |
class_attributes['create_tags'] = create_tags |
| 23 |
|
| 24 |
|
| 25 |
def create_tags(self, **kwargs): |
| 26 |
# Call the client method |
| 27 |
self.meta.client.create_tags(**kwargs) |
| 28 |
resources = kwargs.get('Resources', []) |
| 29 |
tags = kwargs.get('Tags', []) |
| 30 |
tag_resources = [] |
| 31 |
|
| 32 |
# Generate all of the tag resources that just were created with the |
| 33 |
# preceding client call. |
| 34 |
for resource in resources: |
| 35 |
for tag in tags: |
| 36 |
# Add each tag from the tag set for each resource to the list |
| 37 |
# that is returned by the method. |
| 38 |
tag_resource = self.Tag(resource, tag['Key'], tag['Value']) |
| 39 |
tag_resources.append(tag_resource) |
| 40 |
return tag_resources |