| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Rob White (@wimnat) |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: glue_connection |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Manage an AWS Glue connection |
| 12 |
description: |
| 13 |
- Manage an AWS Glue connection. See U(https://aws.amazon.com/glue/) for details. |
| 14 |
- Prior to release 5.0.0 this module was called C(community.aws.aws_glue_connection). |
| 15 |
The usage did not change. |
| 16 |
author: |
| 17 |
- "Rob White (@wimnat)" |
| 18 |
options: |
| 19 |
availability_zone: |
| 20 |
description: |
| 21 |
- Availability Zone used by the connection |
| 22 |
- Required when I(connection_type=NETWORK). |
| 23 |
type: str |
| 24 |
version_added: 1.5.0 |
| 25 |
catalog_id: |
| 26 |
description: |
| 27 |
- The ID of the Data Catalog in which to create the connection. If none is supplied, |
| 28 |
the AWS account ID is used by default. |
| 29 |
type: str |
| 30 |
connection_properties: |
| 31 |
description: |
| 32 |
- A dict of key-value pairs used as parameters for this connection. |
| 33 |
- Required when I(state=present). |
| 34 |
type: dict |
| 35 |
connection_type: |
| 36 |
description: |
| 37 |
- The type of the connection. Currently, SFTP is not supported. |
| 38 |
default: JDBC |
| 39 |
choices: [ 'CUSTOM', 'JDBC', 'KAFKA', 'MARKETPLACE', 'MONGODB', 'NETWORK' ] |
| 40 |
type: str |
| 41 |
description: |
| 42 |
description: |
| 43 |
- The description of the connection. |
| 44 |
type: str |
| 45 |
match_criteria: |
| 46 |
description: |
| 47 |
- A list of UTF-8 strings that specify the criteria that you can use in selecting this connection. |
| 48 |
type: list |
| 49 |
elements: str |
| 50 |
name: |
| 51 |
description: |
| 52 |
- The name of the connection. |
| 53 |
required: true |
| 54 |
type: str |
| 55 |
security_groups: |
| 56 |
description: |
| 57 |
- A list of security groups to be used by the connection. Use either security group name or ID. |
| 58 |
- Required when I(connection_type=NETWORK). |
| 59 |
type: list |
| 60 |
elements: str |
| 61 |
state: |
| 62 |
description: |
| 63 |
- Create or delete the AWS Glue connection. |
| 64 |
required: true |
| 65 |
choices: [ 'present', 'absent' ] |
| 66 |
type: str |
| 67 |
subnet_id: |
| 68 |
description: |
| 69 |
- The subnet ID used by the connection. |
| 70 |
- Required when I(connection_type=NETWORK). |
| 71 |
type: str |
| 72 |
extends_documentation_fragment: |
| 73 |
- amazon.aws.common.modules |
| 74 |
- amazon.aws.region.modules |
| 75 |
- amazon.aws.boto3 |
| 76 |
""" |
| 77 |
|
| 78 |
EXAMPLES = r""" |
| 79 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 80 |
|
| 81 |
# Create an AWS Glue connection |
| 82 |
- community.aws.glue_connection: |
| 83 |
name: my-glue-connection |
| 84 |
connection_properties: |
| 85 |
JDBC_CONNECTION_URL: jdbc:mysql://mydb:3306/databasename |
| 86 |
USERNAME: my-username |
| 87 |
PASSWORD: my-password |
| 88 |
state: present |
| 89 |
|
| 90 |
# Create an AWS Glue network connection |
| 91 |
- community.aws.glue_connection: |
| 92 |
name: my-glue-network-connection |
| 93 |
availability_zone: us-east-1a |
| 94 |
connection_properties: |
| 95 |
JDBC_ENFORCE_SSL: "false" |
| 96 |
connection_type: NETWORK |
| 97 |
description: Test connection |
| 98 |
security_groups: |
| 99 |
- sg-glue |
| 100 |
subnet_id: subnet-123abc |
| 101 |
state: present |
| 102 |
|
| 103 |
# Delete an AWS Glue connection |
| 104 |
- community.aws.glue_connection: |
| 105 |
name: my-glue-connection |
| 106 |
state: absent |
| 107 |
""" |
| 108 |
|
| 109 |
RETURN = r""" |
| 110 |
connection_type: |
| 111 |
description: The type of the connection. |
| 112 |
returned: when state is present |
| 113 |
type: str |
| 114 |
sample: JDBC |
| 115 |
creation_time: |
| 116 |
description: The time this connection definition was created. |
| 117 |
returned: when state is present |
| 118 |
type: str |
| 119 |
sample: "2018-04-21T05:19:58.326000+00:00" |
| 120 |
description: |
| 121 |
description: Description of the job being defined. |
| 122 |
returned: when state is present |
| 123 |
type: str |
| 124 |
sample: My first Glue job |
| 125 |
last_updated_time: |
| 126 |
description: The last time this connection definition was updated. |
| 127 |
returned: when state is present |
| 128 |
type: str |
| 129 |
sample: "2018-04-21T05:19:58.326000+00:00" |
| 130 |
match_criteria: |
| 131 |
description: A list of criteria that can be used in selecting this connection. |
| 132 |
returned: when state is present |
| 133 |
type: list |
| 134 |
sample: [] |
| 135 |
name: |
| 136 |
description: The name of the connection definition. |
| 137 |
returned: when state is present |
| 138 |
type: str |
| 139 |
sample: my-glue-connection |
| 140 |
physical_connection_requirements: |
| 141 |
description: A dict of physical connection requirements, such as VPC and SecurityGroup, |
| 142 |
needed for making this connection successfully. |
| 143 |
returned: when state is present |
| 144 |
type: dict |
| 145 |
sample: {'subnet-id':'subnet-aabbccddee'} |
| 146 |
raw_connection_properties: |
| 147 |
description: A dict of key-value pairs used as parameters for this connection. |
| 148 |
returned: when state is present |
| 149 |
type: dict |
| 150 |
sample: {'JDBC_CONNECTION_URL':'jdbc:mysql://mydb:3306/databasename','USERNAME':'x','PASSWORD':'y'} |
| 151 |
""" |
| 152 |
|
| 153 |
import copy |
| 154 |
import time |
| 155 |
|
| 156 |
try: |
| 157 |
import botocore |
| 158 |
except ImportError: |
| 159 |
pass |
| 160 |
|
| 161 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 162 |
|
| 163 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 164 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import get_ec2_security_group_ids_from_names |
| 165 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 166 |
|
| 167 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 168 |
|
| 169 |
|
| 170 |
def _get_glue_connection(connection, module): |
| 171 |
""" |
| 172 |
Get an AWS Glue connection based on name. If not found, return None. |
| 173 |
|
| 174 |
:param connection: AWS boto3 glue connection |
| 175 |
:param module: Ansible module |
| 176 |
:return: boto3 Glue connection dict or None if not found |
| 177 |
""" |
| 178 |
|
| 179 |
connection_name = module.params.get("name") |
| 180 |
connection_catalog_id = module.params.get("catalog_id") |
| 181 |
|
| 182 |
params = {"Name": connection_name} |
| 183 |
if connection_catalog_id is not None: |
| 184 |
params["CatalogId"] = connection_catalog_id |
| 185 |
|
| 186 |
try: |
| 187 |
return connection.get_connection(aws_retry=True, **params)["Connection"] |
| 188 |
except is_boto3_error_code("EntityNotFoundException"): |
| 189 |
return None |
| 190 |
|
| 191 |
|
| 192 |
def _compare_glue_connection_params(user_params, current_params): |
| 193 |
""" |
| 194 |
Compare Glue connection params. If there is a difference, return True immediately else return False |
| 195 |
|
| 196 |
:param user_params: the Glue connection parameters passed by the user |
| 197 |
:param current_params: the Glue connection parameters currently configured |
| 198 |
:return: True if any parameter is mismatched else False |
| 199 |
""" |
| 200 |
|
| 201 |
# Weirdly, boto3 doesn't return some keys if the value is empty e.g. Description |
| 202 |
# To counter this, add the key if it's missing with a blank value |
| 203 |
|
| 204 |
if "Description" not in current_params: |
| 205 |
current_params["Description"] = "" |
| 206 |
if "MatchCriteria" not in current_params: |
| 207 |
current_params["MatchCriteria"] = list() |
| 208 |
if "PhysicalConnectionRequirements" not in current_params: |
| 209 |
current_params["PhysicalConnectionRequirements"] = dict() |
| 210 |
current_params["PhysicalConnectionRequirements"]["SecurityGroupIdList"] = [] |
| 211 |
current_params["PhysicalConnectionRequirements"]["SubnetId"] = "" |
| 212 |
|
| 213 |
if ( |
| 214 |
"ConnectionProperties" in user_params["ConnectionInput"] |
| 215 |
and user_params["ConnectionInput"]["ConnectionProperties"] != current_params["ConnectionProperties"] |
| 216 |
): |
| 217 |
return True |
| 218 |
if ( |
| 219 |
"ConnectionType" in user_params["ConnectionInput"] |
| 220 |
and user_params["ConnectionInput"]["ConnectionType"] != current_params["ConnectionType"] |
| 221 |
): |
| 222 |
return True |
| 223 |
if ( |
| 224 |
"Description" in user_params["ConnectionInput"] |
| 225 |
and user_params["ConnectionInput"]["Description"] != current_params["Description"] |
| 226 |
): |
| 227 |
return True |
| 228 |
if "MatchCriteria" in user_params["ConnectionInput"] and set( |
| 229 |
user_params["ConnectionInput"]["MatchCriteria"] |
| 230 |
) != set(current_params["MatchCriteria"]): |
| 231 |
return True |
| 232 |
if "PhysicalConnectionRequirements" in user_params["ConnectionInput"]: |
| 233 |
if "SecurityGroupIdList" in user_params["ConnectionInput"]["PhysicalConnectionRequirements"] and set( |
| 234 |
user_params["ConnectionInput"]["PhysicalConnectionRequirements"]["SecurityGroupIdList"] |
| 235 |
) != set(current_params["PhysicalConnectionRequirements"]["SecurityGroupIdList"]): |
| 236 |
return True |
| 237 |
if ( |
| 238 |
"SubnetId" in user_params["ConnectionInput"]["PhysicalConnectionRequirements"] |
| 239 |
and user_params["ConnectionInput"]["PhysicalConnectionRequirements"]["SubnetId"] |
| 240 |
!= current_params["PhysicalConnectionRequirements"]["SubnetId"] |
| 241 |
): |
| 242 |
return True |
| 243 |
if ( |
| 244 |
"AvailabilityZone" in user_params["ConnectionInput"]["PhysicalConnectionRequirements"] |
| 245 |
and user_params["ConnectionInput"]["PhysicalConnectionRequirements"]["AvailabilityZone"] |
| 246 |
!= current_params["PhysicalConnectionRequirements"]["AvailabilityZone"] |
| 247 |
): |
| 248 |
return True |
| 249 |
|
| 250 |
return False |
| 251 |
|
| 252 |
|
| 253 |
# Glue module doesn't appear to have any waiters, unlike EC2 or RDS |
| 254 |
def _await_glue_connection(connection, module): |
| 255 |
start_time = time.time() |
| 256 |
wait_timeout = start_time + 30 |
| 257 |
check_interval = 5 |
| 258 |
|
| 259 |
while wait_timeout > time.time(): |
| 260 |
glue_connection = _get_glue_connection(connection, module) |
| 261 |
if glue_connection and glue_connection.get("Name"): |
| 262 |
return glue_connection |
| 263 |
time.sleep(check_interval) |
| 264 |
|
| 265 |
module.fail_json(msg=f"Timeout waiting for Glue connection {module.params.get('name')}") |
| 266 |
|
| 267 |
|
| 268 |
def create_or_update_glue_connection(connection, connection_ec2, module, glue_connection): |
| 269 |
""" |
| 270 |
Create or update an AWS Glue connection |
| 271 |
|
| 272 |
:param connection: AWS boto3 glue connection |
| 273 |
:param module: Ansible module |
| 274 |
:param glue_connection: a dict of AWS Glue connection parameters or None |
| 275 |
:return: |
| 276 |
""" |
| 277 |
changed = False |
| 278 |
|
| 279 |
params = dict() |
| 280 |
params["ConnectionInput"] = dict() |
| 281 |
params["ConnectionInput"]["Name"] = module.params.get("name") |
| 282 |
params["ConnectionInput"]["ConnectionType"] = module.params.get("connection_type") |
| 283 |
params["ConnectionInput"]["ConnectionProperties"] = module.params.get("connection_properties") |
| 284 |
if module.params.get("catalog_id") is not None: |
| 285 |
params["CatalogId"] = module.params.get("catalog_id") |
| 286 |
if module.params.get("description") is not None: |
| 287 |
params["ConnectionInput"]["Description"] = module.params.get("description") |
| 288 |
if module.params.get("match_criteria") is not None: |
| 289 |
params["ConnectionInput"]["MatchCriteria"] = module.params.get("match_criteria") |
| 290 |
if module.params.get("security_groups") is not None or module.params.get("subnet_id") is not None: |
| 291 |
params["ConnectionInput"]["PhysicalConnectionRequirements"] = dict() |
| 292 |
if module.params.get("security_groups") is not None: |
| 293 |
# Get security group IDs from names |
| 294 |
security_group_ids = get_ec2_security_group_ids_from_names(module.params.get("security_groups"), connection_ec2) |
| 295 |
params["ConnectionInput"]["PhysicalConnectionRequirements"]["SecurityGroupIdList"] = security_group_ids |
| 296 |
if module.params.get("subnet_id") is not None: |
| 297 |
params["ConnectionInput"]["PhysicalConnectionRequirements"]["SubnetId"] = module.params.get("subnet_id") |
| 298 |
if module.params.get("availability_zone") is not None: |
| 299 |
params["ConnectionInput"]["PhysicalConnectionRequirements"]["AvailabilityZone"] = module.params.get( |
| 300 |
"availability_zone" |
| 301 |
) |
| 302 |
|
| 303 |
# If glue_connection is not None then check if it needs to be modified, else create it |
| 304 |
if glue_connection: |
| 305 |
if _compare_glue_connection_params(params, glue_connection): |
| 306 |
try: |
| 307 |
# We need to slightly modify the params for an update |
| 308 |
update_params = copy.deepcopy(params) |
| 309 |
update_params["Name"] = update_params["ConnectionInput"]["Name"] |
| 310 |
if not module.check_mode: |
| 311 |
connection.update_connection(aws_retry=True, **update_params) |
| 312 |
changed = True |
| 313 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 314 |
module.fail_json_aws(e) |
| 315 |
else: |
| 316 |
try: |
| 317 |
if not module.check_mode: |
| 318 |
connection.create_connection(aws_retry=True, **params) |
| 319 |
changed = True |
| 320 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 321 |
module.fail_json_aws(e) |
| 322 |
|
| 323 |
# If changed, get the Glue connection again |
| 324 |
if changed and not module.check_mode: |
| 325 |
glue_connection = _await_glue_connection(connection, module) |
| 326 |
|
| 327 |
if glue_connection: |
| 328 |
glue_connection["RawConnectionProperties"] = glue_connection["ConnectionProperties"] |
| 329 |
glue_connection.pop("ConnectionProperties") |
| 330 |
|
| 331 |
module.exit_json( |
| 332 |
changed=changed, **camel_dict_to_snake_dict(glue_connection or {}, ignore_list=["RawConnectionProperties"]) |
| 333 |
) |
| 334 |
|
| 335 |
|
| 336 |
def delete_glue_connection(connection, module, glue_connection): |
| 337 |
""" |
| 338 |
Delete an AWS Glue connection |
| 339 |
|
| 340 |
:param connection: AWS boto3 glue connection |
| 341 |
:param module: Ansible module |
| 342 |
:param glue_connection: a dict of AWS Glue connection parameters or None |
| 343 |
:return: |
| 344 |
""" |
| 345 |
changed = False |
| 346 |
|
| 347 |
params = {"ConnectionName": module.params.get("name")} |
| 348 |
if module.params.get("catalog_id") is not None: |
| 349 |
params["CatalogId"] = module.params.get("catalog_id") |
| 350 |
|
| 351 |
if glue_connection: |
| 352 |
try: |
| 353 |
if not module.check_mode: |
| 354 |
connection.delete_connection(aws_retry=True, **params) |
| 355 |
changed = True |
| 356 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 357 |
module.fail_json_aws(e) |
| 358 |
|
| 359 |
module.exit_json(changed=changed) |
| 360 |
|
| 361 |
|
| 362 |
def main(): |
| 363 |
argument_spec = dict( |
| 364 |
availability_zone=dict(type="str"), |
| 365 |
catalog_id=dict(type="str"), |
| 366 |
connection_properties=dict(type="dict"), |
| 367 |
connection_type=dict( |
| 368 |
type="str", default="JDBC", choices=["CUSTOM", "JDBC", "KAFKA", "MARKETPLACE", "MONGODB", "NETWORK"] |
| 369 |
), |
| 370 |
description=dict(type="str"), |
| 371 |
match_criteria=dict(type="list", elements="str"), |
| 372 |
name=dict(required=True, type="str"), |
| 373 |
security_groups=dict(type="list", elements="str"), |
| 374 |
state=dict(required=True, choices=["present", "absent"], type="str"), |
| 375 |
subnet_id=dict(type="str"), |
| 376 |
) |
| 377 |
|
| 378 |
module = AnsibleAWSModule( |
| 379 |
argument_spec=argument_spec, |
| 380 |
required_if=[ |
| 381 |
("state", "present", ["connection_properties"]), |
| 382 |
("connection_type", "NETWORK", ["availability_zone", "security_groups", "subnet_id"]), |
| 383 |
], |
| 384 |
supports_check_mode=True, |
| 385 |
) |
| 386 |
|
| 387 |
retry_decorator = AWSRetry.jittered_backoff(retries=10) |
| 388 |
connection_glue = module.client("glue", retry_decorator=retry_decorator) |
| 389 |
connection_ec2 = module.client("ec2", retry_decorator=retry_decorator) |
| 390 |
|
| 391 |
glue_connection = _get_glue_connection(connection_glue, module) |
| 392 |
|
| 393 |
if module.params.get("state") == "present": |
| 394 |
create_or_update_glue_connection(connection_glue, connection_ec2, module, glue_connection) |
| 395 |
else: |
| 396 |
delete_glue_connection(connection_glue, module, glue_connection) |
| 397 |
|
| 398 |
|
| 399 |
if __name__ == "__main__": |
| 400 |
main() |