| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from copy import deepcopy |
| 7 |
|
| 8 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 9 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 10 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list |
| 11 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 12 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_specifications |
| 13 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import compare_aws_tags |
| 14 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 15 |
|
| 16 |
from ansible_collections.community.aws.plugins.module_utils.base import BaseResourceManager |
| 17 |
from ansible_collections.community.aws.plugins.module_utils.base import BaseWaiterFactory |
| 18 |
from ansible_collections.community.aws.plugins.module_utils.base import Boto3Mixin |
| 19 |
|
| 20 |
|
| 21 |
class Ec2WaiterFactory(BaseWaiterFactory): |
| 22 |
def __init__(self, module): |
| 23 |
# the AWSRetry wrapper doesn't support the wait functions (there's no |
| 24 |
# public call we can cleanly wrap) |
| 25 |
client = module.client("ec2") |
| 26 |
super(Ec2WaiterFactory, self).__init__(module, client) |
| 27 |
|
| 28 |
@property |
| 29 |
def _waiter_model_data(self): |
| 30 |
data = super(Ec2WaiterFactory, self)._waiter_model_data |
| 31 |
return data |
| 32 |
|
| 33 |
|
| 34 |
class Ec2Boto3Mixin(Boto3Mixin): |
| 35 |
@AWSRetry.jittered_backoff() |
| 36 |
def _paginated_describe_subnets(self, **params): |
| 37 |
paginator = self.client.get_paginator("describe_subnets") |
| 38 |
return paginator.paginate(**params).build_full_result() |
| 39 |
|
| 40 |
@Boto3Mixin.aws_error_handler("describe subnets") |
| 41 |
def _describe_subnets(self, **params): |
| 42 |
try: |
| 43 |
result = self._paginated_describe_subnets(**params) |
| 44 |
except is_boto3_error_code("SubnetID.NotFound"): |
| 45 |
return None |
| 46 |
return result.get("Subnets", None) |
| 47 |
|
| 48 |
|
| 49 |
class BaseEc2Manager(Ec2Boto3Mixin, BaseResourceManager): |
| 50 |
resource_id = None |
| 51 |
TAG_RESOURCE_TYPE = None |
| 52 |
# This can be overridden by a subclass *if* 'Tags' isn't returned as a part of |
| 53 |
# the standard Resource description |
| 54 |
TAGS_ON_RESOURCE = True |
| 55 |
# If the resource supports using "TagSpecifications" on creation we can |
| 56 |
TAGS_ON_CREATE = "TagSpecifications" |
| 57 |
|
| 58 |
def __init__(self, module, resource_id=None): |
| 59 |
r""" |
| 60 |
Parameters: |
| 61 |
module (AnsibleAWSModule): An Ansible module. |
| 62 |
""" |
| 63 |
super(BaseEc2Manager, self).__init__(module) |
| 64 |
self.client = self._create_client() |
| 65 |
self._tagging_updates = dict() |
| 66 |
self.resource_id = resource_id |
| 67 |
|
| 68 |
# Name parameter is unique (by region) and can not be modified. |
| 69 |
if self.resource_id: |
| 70 |
resource = deepcopy(self.get_resource()) |
| 71 |
self.original_resource = resource |
| 72 |
|
| 73 |
def _flush_update(self): |
| 74 |
changed = False |
| 75 |
changed |= self._do_tagging() |
| 76 |
changed |= super(BaseEc2Manager, self)._flush_update() |
| 77 |
return changed |
| 78 |
|
| 79 |
@Boto3Mixin.aws_error_handler("connect to AWS") |
| 80 |
def _create_client(self, client_name="ec2"): |
| 81 |
client = self.module.client(client_name, retry_decorator=AWSRetry.jittered_backoff()) |
| 82 |
return client |
| 83 |
|
| 84 |
@Boto3Mixin.aws_error_handler("set tags on resource") |
| 85 |
def _add_tags(self, **params): |
| 86 |
self.client.create_tags(aws_retry=True, **params) |
| 87 |
return True |
| 88 |
|
| 89 |
@Boto3Mixin.aws_error_handler("unset tags on resource") |
| 90 |
def _remove_tags(self, **params): |
| 91 |
self.client.delete_tags(aws_retry=True, **params) |
| 92 |
return True |
| 93 |
|
| 94 |
@AWSRetry.jittered_backoff() |
| 95 |
def _paginated_describe_tags(self, **params): |
| 96 |
paginator = self.client.get_paginator("describe_tags") |
| 97 |
return paginator.paginate(**params).build_full_result() |
| 98 |
|
| 99 |
@Boto3Mixin.aws_error_handler("list tags on resource") |
| 100 |
def _describe_tags(self, resource_id=None): |
| 101 |
if not resource_id: |
| 102 |
resource_id = self.resource_id |
| 103 |
filters = ansible_dict_to_boto3_filter_list({"resource-id": resource_id}) |
| 104 |
tags = self._paginated_describe_tags(Filters=filters) |
| 105 |
return tags |
| 106 |
|
| 107 |
def _get_tags(self, resource_id=None): |
| 108 |
if resource_id is None: |
| 109 |
resource_id = self.resource_id |
| 110 |
# If the Tags are available from the resource, then use them |
| 111 |
if self.TAGS_ON_RESOURCE: |
| 112 |
tags = self._preupdate_resource.get("Tags", []) |
| 113 |
# Otherwise we'll have to look them up |
| 114 |
else: |
| 115 |
tags = self._describe_tags(resource_id=resource_id) |
| 116 |
return boto3_tag_list_to_ansible_dict(tags) |
| 117 |
|
| 118 |
def _do_tagging(self): |
| 119 |
changed = False |
| 120 |
tags_to_add = self._tagging_updates.get("add") |
| 121 |
tags_to_remove = self._tagging_updates.get("remove") |
| 122 |
|
| 123 |
if tags_to_add: |
| 124 |
changed = True |
| 125 |
tags = ansible_dict_to_boto3_tag_list(tags_to_add) |
| 126 |
if not self.module.check_mode: |
| 127 |
self._add_tags(Resources=[self.resource_id], Tags=tags) |
| 128 |
if tags_to_remove: |
| 129 |
changed = True |
| 130 |
if not self.module.check_mode: |
| 131 |
tag_list = [dict(Key=tagkey) for tagkey in tags_to_remove] |
| 132 |
self._remove_tags(Resources=[self.resource_id], Tags=tag_list) |
| 133 |
|
| 134 |
return changed |
| 135 |
|
| 136 |
def _merge_resource_changes(self, filter_immutable=True, creation=False): |
| 137 |
resource = super(BaseEc2Manager, self)._merge_resource_changes( |
| 138 |
filter_immutable=filter_immutable, creation=creation |
| 139 |
) |
| 140 |
|
| 141 |
if creation: |
| 142 |
if not self.TAGS_ON_CREATE: |
| 143 |
resource.pop("Tags", None) |
| 144 |
elif self.TAGS_ON_CREATE == "TagSpecifications": |
| 145 |
tags = boto3_tag_list_to_ansible_dict(resource.pop("Tags", [])) |
| 146 |
tag_specs = boto3_tag_specifications(tags, types=[self.TAG_RESOURCE_TYPE]) |
| 147 |
if tag_specs: |
| 148 |
resource["TagSpecifications"] = tag_specs |
| 149 |
|
| 150 |
return resource |
| 151 |
|
| 152 |
def set_tags(self, tags, purge_tags): |
| 153 |
if tags is None: |
| 154 |
return False |
| 155 |
changed = False |
| 156 |
|
| 157 |
# Tags are returned as a part of the resource, but have to be updated |
| 158 |
# via dedicated tagging methods |
| 159 |
current_tags = self._get_tags() |
| 160 |
|
| 161 |
# So that diff works in check mode we need to know the full target state |
| 162 |
if purge_tags: |
| 163 |
desired_tags = deepcopy(tags) |
| 164 |
else: |
| 165 |
desired_tags = deepcopy(current_tags) |
| 166 |
desired_tags.update(tags) |
| 167 |
|
| 168 |
tags_to_add, tags_to_remove = compare_aws_tags(current_tags, tags, purge_tags) |
| 169 |
|
| 170 |
if tags_to_add: |
| 171 |
self._tagging_updates["add"] = tags_to_add |
| 172 |
changed = True |
| 173 |
if tags_to_remove: |
| 174 |
self._tagging_updates["remove"] = tags_to_remove |
| 175 |
changed = True |
| 176 |
|
| 177 |
if changed: |
| 178 |
# Tags are a stored as a list, but treated like a list, the |
| 179 |
# simplisic '==' in _set_resource_value doesn't do the comparison |
| 180 |
# properly |
| 181 |
return self._set_resource_value("Tags", ansible_dict_to_boto3_tag_list(desired_tags)) |
| 182 |
|
| 183 |
return False |