| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright 2014 Jens Carl, Hothead Games Inc. |
| 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 |
author: |
| 10 |
- "Jens Carl (@j-carl), Hothead Games Inc." |
| 11 |
- "Rafael Driutti (@rafaeldriutti)" |
| 12 |
module: redshift |
| 13 |
version_added: 1.0.0 |
| 14 |
short_description: create, delete, or modify an Amazon Redshift instance |
| 15 |
description: |
| 16 |
- Creates, deletes, or modifies Amazon Redshift cluster instances. |
| 17 |
options: |
| 18 |
command: |
| 19 |
description: |
| 20 |
- Specifies the action to take. |
| 21 |
required: true |
| 22 |
choices: [ 'create', 'facts', 'delete', 'modify' ] |
| 23 |
type: str |
| 24 |
identifier: |
| 25 |
description: |
| 26 |
- Redshift cluster identifier. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
node_type: |
| 30 |
description: |
| 31 |
- The node type of the cluster. |
| 32 |
- Require when I(command=create). |
| 33 |
choices: ['ds1.xlarge', 'ds1.8xlarge', 'ds2.xlarge', 'ds2.8xlarge', 'dc1.large','dc2.large', |
| 34 |
'dc1.8xlarge', 'dw1.xlarge', 'dw1.8xlarge', 'dw2.large', 'dw2.8xlarge'] |
| 35 |
type: str |
| 36 |
username: |
| 37 |
description: |
| 38 |
- Master database username. |
| 39 |
- Used only when I(command=create). |
| 40 |
type: str |
| 41 |
password: |
| 42 |
description: |
| 43 |
- Master database password. |
| 44 |
- Used only when I(command=create). |
| 45 |
type: str |
| 46 |
cluster_type: |
| 47 |
description: |
| 48 |
- The type of cluster. |
| 49 |
choices: ['multi-node', 'single-node' ] |
| 50 |
default: 'single-node' |
| 51 |
type: str |
| 52 |
db_name: |
| 53 |
description: |
| 54 |
- Name of the database. |
| 55 |
type: str |
| 56 |
availability_zone: |
| 57 |
description: |
| 58 |
- Availability zone in which to launch cluster. |
| 59 |
aliases: ['zone', 'aws_zone'] |
| 60 |
type: str |
| 61 |
number_of_nodes: |
| 62 |
description: |
| 63 |
- Number of nodes. |
| 64 |
- Only used when I(cluster_type=multi-node). |
| 65 |
type: int |
| 66 |
cluster_subnet_group_name: |
| 67 |
description: |
| 68 |
- Which subnet to place the cluster. |
| 69 |
aliases: ['subnet'] |
| 70 |
type: str |
| 71 |
cluster_security_groups: |
| 72 |
description: |
| 73 |
- In which security group the cluster belongs. |
| 74 |
type: list |
| 75 |
elements: str |
| 76 |
aliases: ['security_groups'] |
| 77 |
vpc_security_group_ids: |
| 78 |
description: |
| 79 |
- VPC security group |
| 80 |
aliases: ['vpc_security_groups'] |
| 81 |
type: list |
| 82 |
elements: str |
| 83 |
skip_final_cluster_snapshot: |
| 84 |
description: |
| 85 |
- Skip a final snapshot before deleting the cluster. |
| 86 |
- Used only when I(command=delete). |
| 87 |
aliases: ['skip_final_snapshot'] |
| 88 |
default: false |
| 89 |
type: bool |
| 90 |
final_cluster_snapshot_identifier: |
| 91 |
description: |
| 92 |
- Identifier of the final snapshot to be created before deleting the cluster. |
| 93 |
- If this parameter is provided, I(skip_final_cluster_snapshot) must be C(false). |
| 94 |
- Used only when I(command=delete). |
| 95 |
aliases: ['final_snapshot_id'] |
| 96 |
type: str |
| 97 |
preferred_maintenance_window: |
| 98 |
description: |
| 99 |
- 'Maintenance window in format of C(ddd:hh24:mi-ddd:hh24:mi). (Example: C(Mon:22:00-Mon:23:15))' |
| 100 |
- Times are specified in UTC. |
| 101 |
- If not specified then a random 30 minute maintenance window is assigned. |
| 102 |
aliases: ['maintance_window', 'maint_window'] |
| 103 |
type: str |
| 104 |
cluster_parameter_group_name: |
| 105 |
description: |
| 106 |
- Name of the cluster parameter group. |
| 107 |
aliases: ['param_group_name'] |
| 108 |
type: str |
| 109 |
automated_snapshot_retention_period: |
| 110 |
description: |
| 111 |
- The number of days that automated snapshots are retained. |
| 112 |
aliases: ['retention_period'] |
| 113 |
type: int |
| 114 |
port: |
| 115 |
description: |
| 116 |
- Which port the cluster is listening on. |
| 117 |
type: int |
| 118 |
cluster_version: |
| 119 |
description: |
| 120 |
- Which version the cluster should have. |
| 121 |
aliases: ['version'] |
| 122 |
choices: ['1.0'] |
| 123 |
type: str |
| 124 |
allow_version_upgrade: |
| 125 |
description: |
| 126 |
- When I(allow_version_upgrade=true) the cluster may be automatically |
| 127 |
upgraded during the maintenance window. |
| 128 |
aliases: ['version_upgrade'] |
| 129 |
default: true |
| 130 |
type: bool |
| 131 |
publicly_accessible: |
| 132 |
description: |
| 133 |
- If the cluster is accessible publicly or not. |
| 134 |
default: false |
| 135 |
type: bool |
| 136 |
encrypted: |
| 137 |
description: |
| 138 |
- If the cluster is encrypted or not. |
| 139 |
default: false |
| 140 |
type: bool |
| 141 |
elastic_ip: |
| 142 |
description: |
| 143 |
- An Elastic IP to use for the cluster. |
| 144 |
type: str |
| 145 |
new_cluster_identifier: |
| 146 |
description: |
| 147 |
- Only used when command=modify. |
| 148 |
aliases: ['new_identifier'] |
| 149 |
type: str |
| 150 |
wait: |
| 151 |
description: |
| 152 |
- When I(command=create), I(command=modify) or I(command=restore) then wait for the database to enter the 'available' state. |
| 153 |
- When I(command=delete) wait for the database to be terminated. |
| 154 |
type: bool |
| 155 |
default: false |
| 156 |
wait_timeout: |
| 157 |
description: |
| 158 |
- When I(wait=true) defines how long in seconds before giving up. |
| 159 |
default: 300 |
| 160 |
type: int |
| 161 |
enhanced_vpc_routing: |
| 162 |
description: |
| 163 |
- Whether the cluster should have enhanced VPC routing enabled. |
| 164 |
default: false |
| 165 |
type: bool |
| 166 |
notes: |
| 167 |
- Support for I(tags) and I(purge_tags) was added in release 1.3.0. |
| 168 |
extends_documentation_fragment: |
| 169 |
- amazon.aws.common.modules |
| 170 |
- amazon.aws.region.modules |
| 171 |
- amazon.aws.tags |
| 172 |
- amazon.aws.boto3 |
| 173 |
""" |
| 174 |
|
| 175 |
EXAMPLES = r""" |
| 176 |
- name: Basic cluster provisioning example |
| 177 |
community.aws.redshift: |
| 178 |
command: create |
| 179 |
node_type: ds1.xlarge |
| 180 |
identifier: new_cluster |
| 181 |
username: cluster_admin |
| 182 |
password: 1nsecure |
| 183 |
|
| 184 |
- name: Cluster delete example |
| 185 |
community.aws.redshift: |
| 186 |
command: delete |
| 187 |
identifier: new_cluster |
| 188 |
skip_final_cluster_snapshot: true |
| 189 |
wait: true |
| 190 |
""" |
| 191 |
|
| 192 |
RETURN = r""" |
| 193 |
cluster: |
| 194 |
description: dictionary containing all the cluster information |
| 195 |
returned: success |
| 196 |
type: complex |
| 197 |
contains: |
| 198 |
identifier: |
| 199 |
description: Id of the cluster. |
| 200 |
returned: success |
| 201 |
type: str |
| 202 |
sample: "new_redshift_cluster" |
| 203 |
create_time: |
| 204 |
description: Time of the cluster creation as timestamp. |
| 205 |
returned: success |
| 206 |
type: float |
| 207 |
sample: 1430158536.308 |
| 208 |
status: |
| 209 |
description: Status of the cluster. |
| 210 |
returned: success |
| 211 |
type: str |
| 212 |
sample: "available" |
| 213 |
db_name: |
| 214 |
description: Name of the database. |
| 215 |
returned: success |
| 216 |
type: str |
| 217 |
sample: "new_db_name" |
| 218 |
availability_zone: |
| 219 |
description: Amazon availability zone where the cluster is located. "None" until cluster is available. |
| 220 |
returned: success |
| 221 |
type: str |
| 222 |
sample: "us-east-1b" |
| 223 |
maintenance_window: |
| 224 |
description: Time frame when maintenance/upgrade are done. |
| 225 |
returned: success |
| 226 |
type: str |
| 227 |
sample: "sun:09:30-sun:10:00" |
| 228 |
private_ip_address: |
| 229 |
description: Private IP address of the main node. |
| 230 |
returned: success |
| 231 |
type: str |
| 232 |
sample: "10.10.10.10" |
| 233 |
public_ip_address: |
| 234 |
description: Public IP address of the main node. "None" when enhanced_vpc_routing is enabled. |
| 235 |
returned: success |
| 236 |
type: str |
| 237 |
sample: "0.0.0.0" |
| 238 |
port: |
| 239 |
description: Port of the cluster. "None" until cluster is available. |
| 240 |
returned: success |
| 241 |
type: int |
| 242 |
sample: 5439 |
| 243 |
url: |
| 244 |
description: FQDN of the main cluster node. "None" until cluster is available. |
| 245 |
returned: success |
| 246 |
type: str |
| 247 |
sample: "new-redshift_cluster.jfkdjfdkj.us-east-1.redshift.amazonaws.com" |
| 248 |
enhanced_vpc_routing: |
| 249 |
description: status of the enhanced vpc routing feature. |
| 250 |
returned: success |
| 251 |
type: bool |
| 252 |
tags: |
| 253 |
description: aws tags for cluster. |
| 254 |
returned: success |
| 255 |
type: dict |
| 256 |
""" |
| 257 |
|
| 258 |
try: |
| 259 |
import botocore |
| 260 |
except ImportError: |
| 261 |
pass # caught by AnsibleAWSModule |
| 262 |
|
| 263 |
from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict |
| 264 |
|
| 265 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 266 |
from ansible_collections.amazon.aws.plugins.module_utils.iam import get_aws_account_info |
| 267 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 268 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list |
| 269 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 270 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import compare_aws_tags |
| 271 |
|
| 272 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 273 |
|
| 274 |
|
| 275 |
def _ensure_tags(redshift, identifier, existing_tags, module): |
| 276 |
"""Compares and update resource tags""" |
| 277 |
|
| 278 |
account_id, partition = get_aws_account_info(module) |
| 279 |
region = module.region |
| 280 |
resource_arn = f"arn:{partition}:redshift:{region}:{account_id}:cluster:{identifier}" |
| 281 |
tags = module.params.get("tags") |
| 282 |
purge_tags = module.params.get("purge_tags") |
| 283 |
|
| 284 |
tags_to_add, tags_to_remove = compare_aws_tags(boto3_tag_list_to_ansible_dict(existing_tags), tags, purge_tags) |
| 285 |
|
| 286 |
if tags_to_add: |
| 287 |
try: |
| 288 |
redshift.create_tags(ResourceName=resource_arn, Tags=ansible_dict_to_boto3_tag_list(tags_to_add)) |
| 289 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 290 |
module.fail_json_aws(e, msg="Failed to add tags to cluster") |
| 291 |
|
| 292 |
if tags_to_remove: |
| 293 |
try: |
| 294 |
redshift.delete_tags(ResourceName=resource_arn, TagKeys=tags_to_remove) |
| 295 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 296 |
module.fail_json_aws(e, msg="Failed to delete tags on cluster") |
| 297 |
|
| 298 |
changed = bool(tags_to_add or tags_to_remove) |
| 299 |
return changed |
| 300 |
|
| 301 |
|
| 302 |
def _collect_facts(resource): |
| 303 |
"""Transform cluster information to dict.""" |
| 304 |
facts = { |
| 305 |
"identifier": resource["ClusterIdentifier"], |
| 306 |
"status": resource["ClusterStatus"], |
| 307 |
"username": resource["MasterUsername"], |
| 308 |
"db_name": resource["DBName"], |
| 309 |
"maintenance_window": resource["PreferredMaintenanceWindow"], |
| 310 |
"enhanced_vpc_routing": resource["EnhancedVpcRouting"], |
| 311 |
} |
| 312 |
|
| 313 |
for node in resource["ClusterNodes"]: |
| 314 |
if node["NodeRole"] in ("SHARED", "LEADER"): |
| 315 |
facts["private_ip_address"] = node["PrivateIPAddress"] |
| 316 |
if facts["enhanced_vpc_routing"] is False: |
| 317 |
facts["public_ip_address"] = node["PublicIPAddress"] |
| 318 |
else: |
| 319 |
facts["public_ip_address"] = None |
| 320 |
break |
| 321 |
|
| 322 |
# Some parameters are not ready instantly if you don't wait for available |
| 323 |
# cluster status |
| 324 |
facts["create_time"] = None |
| 325 |
facts["url"] = None |
| 326 |
facts["port"] = None |
| 327 |
facts["availability_zone"] = None |
| 328 |
facts["tags"] = {} |
| 329 |
|
| 330 |
if resource["ClusterStatus"] != "creating": |
| 331 |
facts["create_time"] = resource["ClusterCreateTime"] |
| 332 |
facts["url"] = resource["Endpoint"]["Address"] |
| 333 |
facts["port"] = resource["Endpoint"]["Port"] |
| 334 |
facts["availability_zone"] = resource["AvailabilityZone"] |
| 335 |
facts["tags"] = boto3_tag_list_to_ansible_dict(resource["Tags"]) |
| 336 |
|
| 337 |
return facts |
| 338 |
|
| 339 |
|
| 340 |
@AWSRetry.jittered_backoff() |
| 341 |
def _describe_cluster(redshift, identifier): |
| 342 |
""" |
| 343 |
Basic wrapper around describe_clusters with a retry applied |
| 344 |
""" |
| 345 |
return redshift.describe_clusters(ClusterIdentifier=identifier)["Clusters"][0] |
| 346 |
|
| 347 |
|
| 348 |
@AWSRetry.jittered_backoff() |
| 349 |
def _create_cluster(redshift, **kwargs): |
| 350 |
""" |
| 351 |
Basic wrapper around create_cluster with a retry applied |
| 352 |
""" |
| 353 |
return redshift.create_cluster(**kwargs) |
| 354 |
|
| 355 |
|
| 356 |
# Simple wrapper around delete, try to avoid throwing an error if some other |
| 357 |
# operation is in progress |
| 358 |
@AWSRetry.jittered_backoff(catch_extra_error_codes=["InvalidClusterState"]) |
| 359 |
def _delete_cluster(redshift, **kwargs): |
| 360 |
""" |
| 361 |
Basic wrapper around delete_cluster with a retry applied. |
| 362 |
Explicitly catches 'InvalidClusterState' (~ Operation in progress) so that |
| 363 |
we can still delete a cluster if some kind of change operation was in |
| 364 |
progress. |
| 365 |
""" |
| 366 |
return redshift.delete_cluster(**kwargs) |
| 367 |
|
| 368 |
|
| 369 |
@AWSRetry.jittered_backoff(catch_extra_error_codes=["InvalidClusterState"]) |
| 370 |
def _modify_cluster(redshift, **kwargs): |
| 371 |
""" |
| 372 |
Basic wrapper around modify_cluster with a retry applied. |
| 373 |
Explicitly catches 'InvalidClusterState' (~ Operation in progress) for cases |
| 374 |
where another modification is still in progress |
| 375 |
""" |
| 376 |
return redshift.modify_cluster(**kwargs) |
| 377 |
|
| 378 |
|
| 379 |
def create_cluster(module, redshift): |
| 380 |
""" |
| 381 |
Create a new cluster |
| 382 |
|
| 383 |
module: AnsibleAWSModule object |
| 384 |
redshift: authenticated redshift connection object |
| 385 |
|
| 386 |
Returns: |
| 387 |
""" |
| 388 |
|
| 389 |
identifier = module.params.get("identifier") |
| 390 |
node_type = module.params.get("node_type") |
| 391 |
username = module.params.get("username") |
| 392 |
password = module.params.get("password") |
| 393 |
d_b_name = module.params.get("db_name") |
| 394 |
wait = module.params.get("wait") |
| 395 |
wait_timeout = module.params.get("wait_timeout") |
| 396 |
tags = module.params.get("tags") |
| 397 |
|
| 398 |
changed = True |
| 399 |
# Package up the optional parameters |
| 400 |
params = {} |
| 401 |
for p in ( |
| 402 |
"cluster_type", |
| 403 |
"cluster_security_groups", |
| 404 |
"vpc_security_group_ids", |
| 405 |
"cluster_subnet_group_name", |
| 406 |
"availability_zone", |
| 407 |
"preferred_maintenance_window", |
| 408 |
"cluster_parameter_group_name", |
| 409 |
"automated_snapshot_retention_period", |
| 410 |
"port", |
| 411 |
"cluster_version", |
| 412 |
"allow_version_upgrade", |
| 413 |
"number_of_nodes", |
| 414 |
"publicly_accessible", |
| 415 |
"encrypted", |
| 416 |
"elastic_ip", |
| 417 |
"enhanced_vpc_routing", |
| 418 |
): |
| 419 |
# https://github.com/boto/boto3/issues/400 |
| 420 |
if module.params.get(p) is not None: |
| 421 |
params[p] = module.params.get(p) |
| 422 |
|
| 423 |
if d_b_name: |
| 424 |
params["d_b_name"] = d_b_name |
| 425 |
if tags: |
| 426 |
tags = ansible_dict_to_boto3_tag_list(tags) |
| 427 |
params["tags"] = tags |
| 428 |
|
| 429 |
try: |
| 430 |
_describe_cluster(redshift, identifier) |
| 431 |
changed = False |
| 432 |
except is_boto3_error_code("ClusterNotFound"): |
| 433 |
try: |
| 434 |
_create_cluster( |
| 435 |
redshift, |
| 436 |
ClusterIdentifier=identifier, |
| 437 |
NodeType=node_type, |
| 438 |
MasterUsername=username, |
| 439 |
MasterUserPassword=password, |
| 440 |
**snake_dict_to_camel_dict(params, capitalize_first=True), |
| 441 |
) |
| 442 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 443 |
module.fail_json_aws(e, msg="Failed to create cluster") |
| 444 |
except ( |
| 445 |
botocore.exceptions.BotoCoreError, |
| 446 |
botocore.exceptions.ClientError, |
| 447 |
) as e: # pylint: disable=duplicate-except |
| 448 |
module.fail_json_aws(e, msg="Failed to describe cluster") |
| 449 |
if wait: |
| 450 |
attempts = wait_timeout // 60 |
| 451 |
waiter = redshift.get_waiter("cluster_available") |
| 452 |
try: |
| 453 |
waiter.wait(ClusterIdentifier=identifier, WaiterConfig=dict(MaxAttempts=attempts)) |
| 454 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 455 |
module.fail_json_aws(e, msg="Timeout waiting for the cluster creation") |
| 456 |
try: |
| 457 |
resource = _describe_cluster(redshift, identifier) |
| 458 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 459 |
module.fail_json_aws(e, msg="Failed to describe cluster") |
| 460 |
|
| 461 |
if tags: |
| 462 |
if _ensure_tags(redshift, identifier, resource["Tags"], module): |
| 463 |
changed = True |
| 464 |
resource = _describe_cluster(redshift, identifier) |
| 465 |
|
| 466 |
return changed, _collect_facts(resource) |
| 467 |
|
| 468 |
|
| 469 |
def describe_cluster(module, redshift): |
| 470 |
""" |
| 471 |
Collect data about the cluster. |
| 472 |
|
| 473 |
module: Ansible module object |
| 474 |
redshift: authenticated redshift connection object |
| 475 |
""" |
| 476 |
identifier = module.params.get("identifier") |
| 477 |
|
| 478 |
try: |
| 479 |
resource = _describe_cluster(redshift, identifier) |
| 480 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 481 |
module.fail_json_aws(e, msg="Error describing cluster") |
| 482 |
|
| 483 |
return True, _collect_facts(resource) |
| 484 |
|
| 485 |
|
| 486 |
def delete_cluster(module, redshift): |
| 487 |
""" |
| 488 |
Delete a cluster. |
| 489 |
|
| 490 |
module: Ansible module object |
| 491 |
redshift: authenticated redshift connection object |
| 492 |
""" |
| 493 |
|
| 494 |
identifier = module.params.get("identifier") |
| 495 |
wait = module.params.get("wait") |
| 496 |
wait_timeout = module.params.get("wait_timeout") |
| 497 |
|
| 498 |
params = {} |
| 499 |
for p in ("skip_final_cluster_snapshot", "final_cluster_snapshot_identifier"): |
| 500 |
if p in module.params: |
| 501 |
# https://github.com/boto/boto3/issues/400 |
| 502 |
if module.params.get(p) is not None: |
| 503 |
params[p] = module.params.get(p) |
| 504 |
|
| 505 |
try: |
| 506 |
_delete_cluster( |
| 507 |
redshift, ClusterIdentifier=identifier, **snake_dict_to_camel_dict(params, capitalize_first=True) |
| 508 |
) |
| 509 |
except is_boto3_error_code("ClusterNotFound"): |
| 510 |
return False, {} |
| 511 |
except ( |
| 512 |
botocore.exceptions.ClientError, |
| 513 |
botocore.exceptions.BotoCoreError, |
| 514 |
) as e: # pylint: disable=duplicate-except |
| 515 |
module.fail_json_aws(e, msg="Failed to delete cluster") |
| 516 |
|
| 517 |
if wait: |
| 518 |
attempts = wait_timeout // 60 |
| 519 |
waiter = redshift.get_waiter("cluster_deleted") |
| 520 |
try: |
| 521 |
waiter.wait(ClusterIdentifier=identifier, WaiterConfig=dict(MaxAttempts=attempts)) |
| 522 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 523 |
module.fail_json_aws(e, msg="Timeout deleting the cluster") |
| 524 |
|
| 525 |
return True, {} |
| 526 |
|
| 527 |
|
| 528 |
def modify_cluster(module, redshift): |
| 529 |
""" |
| 530 |
Modify an existing cluster. |
| 531 |
|
| 532 |
module: Ansible module object |
| 533 |
redshift: authenticated redshift connection object |
| 534 |
""" |
| 535 |
|
| 536 |
identifier = module.params.get("identifier") |
| 537 |
wait = module.params.get("wait") |
| 538 |
wait_timeout = module.params.get("wait_timeout") |
| 539 |
|
| 540 |
# Package up the optional parameters |
| 541 |
params = {} |
| 542 |
for p in ( |
| 543 |
"cluster_type", |
| 544 |
"cluster_security_groups", |
| 545 |
"vpc_security_group_ids", |
| 546 |
"cluster_subnet_group_name", |
| 547 |
"availability_zone", |
| 548 |
"preferred_maintenance_window", |
| 549 |
"cluster_parameter_group_name", |
| 550 |
"automated_snapshot_retention_period", |
| 551 |
"port", |
| 552 |
"cluster_version", |
| 553 |
"allow_version_upgrade", |
| 554 |
"number_of_nodes", |
| 555 |
"new_cluster_identifier", |
| 556 |
): |
| 557 |
# https://github.com/boto/boto3/issues/400 |
| 558 |
if module.params.get(p) is not None: |
| 559 |
params[p] = module.params.get(p) |
| 560 |
|
| 561 |
# enhanced_vpc_routing parameter change needs an exclusive request |
| 562 |
if module.params.get("enhanced_vpc_routing") is not None: |
| 563 |
try: |
| 564 |
_modify_cluster( |
| 565 |
redshift, ClusterIdentifier=identifier, EnhancedVpcRouting=module.params.get("enhanced_vpc_routing") |
| 566 |
) |
| 567 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 568 |
module.fail_json_aws(e, msg=f"Couldn't modify redshift cluster {identifier} ") |
| 569 |
if wait: |
| 570 |
attempts = wait_timeout // 60 |
| 571 |
waiter = redshift.get_waiter("cluster_available") |
| 572 |
try: |
| 573 |
waiter.wait(ClusterIdentifier=identifier, WaiterConfig=dict(MaxAttempts=attempts)) |
| 574 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 575 |
module.fail_json_aws(e, msg="Timeout waiting for cluster enhanced vpc routing modification") |
| 576 |
|
| 577 |
# change the rest |
| 578 |
try: |
| 579 |
_modify_cluster( |
| 580 |
redshift, ClusterIdentifier=identifier, **snake_dict_to_camel_dict(params, capitalize_first=True) |
| 581 |
) |
| 582 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 583 |
module.fail_json_aws(e, msg=f"Couldn't modify redshift cluster {identifier} ") |
| 584 |
|
| 585 |
if module.params.get("new_cluster_identifier"): |
| 586 |
identifier = module.params.get("new_cluster_identifier") |
| 587 |
|
| 588 |
if wait: |
| 589 |
attempts = wait_timeout // 60 |
| 590 |
waiter2 = redshift.get_waiter("cluster_available") |
| 591 |
try: |
| 592 |
waiter2.wait(ClusterIdentifier=identifier, WaiterConfig=dict(MaxAttempts=attempts)) |
| 593 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 594 |
module.fail_json_aws(e, msg="Timeout waiting for cluster modification") |
| 595 |
try: |
| 596 |
resource = _describe_cluster(redshift, identifier) |
| 597 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 598 |
module.fail_json_aws(e, msg=f"Couldn't modify redshift cluster {identifier} ") |
| 599 |
|
| 600 |
if _ensure_tags(redshift, identifier, resource["Tags"], module): |
| 601 |
resource = redshift.describe_clusters(ClusterIdentifier=identifier)["Clusters"][0] |
| 602 |
|
| 603 |
return True, _collect_facts(resource) |
| 604 |
|
| 605 |
|
| 606 |
def main(): |
| 607 |
argument_spec = dict( |
| 608 |
command=dict(choices=["create", "facts", "delete", "modify"], required=True), |
| 609 |
identifier=dict(required=True), |
| 610 |
node_type=dict( |
| 611 |
choices=[ |
| 612 |
"ds1.xlarge", |
| 613 |
"ds1.8xlarge", |
| 614 |
"ds2.xlarge", |
| 615 |
"ds2.8xlarge", |
| 616 |
"dc1.large", |
| 617 |
"dc2.large", |
| 618 |
"dc1.8xlarge", |
| 619 |
"dw1.xlarge", |
| 620 |
"dw1.8xlarge", |
| 621 |
"dw2.large", |
| 622 |
"dw2.8xlarge", |
| 623 |
], |
| 624 |
required=False, |
| 625 |
), |
| 626 |
username=dict(required=False), |
| 627 |
password=dict(no_log=True, required=False), |
| 628 |
db_name=dict(required=False), |
| 629 |
cluster_type=dict(choices=["multi-node", "single-node"], default="single-node"), |
| 630 |
cluster_security_groups=dict(aliases=["security_groups"], type="list", elements="str"), |
| 631 |
vpc_security_group_ids=dict(aliases=["vpc_security_groups"], type="list", elements="str"), |
| 632 |
skip_final_cluster_snapshot=dict(aliases=["skip_final_snapshot"], type="bool", default=False), |
| 633 |
final_cluster_snapshot_identifier=dict(aliases=["final_snapshot_id"], required=False), |
| 634 |
cluster_subnet_group_name=dict(aliases=["subnet"]), |
| 635 |
availability_zone=dict(aliases=["aws_zone", "zone"]), |
| 636 |
preferred_maintenance_window=dict(aliases=["maintance_window", "maint_window"]), |
| 637 |
cluster_parameter_group_name=dict(aliases=["param_group_name"]), |
| 638 |
automated_snapshot_retention_period=dict(aliases=["retention_period"], type="int"), |
| 639 |
port=dict(type="int"), |
| 640 |
cluster_version=dict(aliases=["version"], choices=["1.0"]), |
| 641 |
allow_version_upgrade=dict(aliases=["version_upgrade"], type="bool", default=True), |
| 642 |
number_of_nodes=dict(type="int"), |
| 643 |
publicly_accessible=dict(type="bool", default=False), |
| 644 |
encrypted=dict(type="bool", default=False), |
| 645 |
elastic_ip=dict(required=False), |
| 646 |
new_cluster_identifier=dict(aliases=["new_identifier"]), |
| 647 |
enhanced_vpc_routing=dict(type="bool", default=False), |
| 648 |
wait=dict(type="bool", default=False), |
| 649 |
wait_timeout=dict(type="int", default=300), |
| 650 |
tags=dict(type="dict", aliases=["resource_tags"]), |
| 651 |
purge_tags=dict(type="bool", default=True), |
| 652 |
) |
| 653 |
|
| 654 |
required_if = [ |
| 655 |
("command", "delete", ["skip_final_cluster_snapshot"]), |
| 656 |
("command", "create", ["node_type", "username", "password"]), |
| 657 |
] |
| 658 |
|
| 659 |
module = AnsibleAWSModule( |
| 660 |
argument_spec=argument_spec, |
| 661 |
required_if=required_if, |
| 662 |
) |
| 663 |
|
| 664 |
command = module.params.get("command") |
| 665 |
skip_final_cluster_snapshot = module.params.get("skip_final_cluster_snapshot") |
| 666 |
final_cluster_snapshot_identifier = module.params.get("final_cluster_snapshot_identifier") |
| 667 |
# can't use module basic required_if check for this case |
| 668 |
if command == "delete" and skip_final_cluster_snapshot is False and final_cluster_snapshot_identifier is None: |
| 669 |
module.fail_json( |
| 670 |
msg="Need to specify final_cluster_snapshot_identifier if skip_final_cluster_snapshot is False" |
| 671 |
) |
| 672 |
|
| 673 |
conn = module.client("redshift") |
| 674 |
|
| 675 |
changed = True |
| 676 |
if command == "create": |
| 677 |
(changed, cluster) = create_cluster(module, conn) |
| 678 |
|
| 679 |
elif command == "facts": |
| 680 |
(changed, cluster) = describe_cluster(module, conn) |
| 681 |
|
| 682 |
elif command == "delete": |
| 683 |
(changed, cluster) = delete_cluster(module, conn) |
| 684 |
|
| 685 |
elif command == "modify": |
| 686 |
(changed, cluster) = modify_cluster(module, conn) |
| 687 |
|
| 688 |
module.exit_json(changed=changed, cluster=cluster) |
| 689 |
|
| 690 |
|
| 691 |
if __name__ == "__main__": |
| 692 |
main() |