| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2017 Ansible Project |
| 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: data_pipeline |
| 10 |
version_added: 1.0.0 |
| 11 |
author: |
| 12 |
- Raghu Udiyar (@raags) <[email protected]> |
| 13 |
- Sloane Hertel (@s-hertel) <[email protected]> |
| 14 |
short_description: Create and manage AWS Datapipelines |
| 15 |
description: |
| 16 |
- Create and manage AWS Datapipelines. Creation is not idempotent in AWS, so the C(uniqueId) is created by hashing the options (minus objects) |
| 17 |
given to the datapipeline. |
| 18 |
- The pipeline definition must be in the format given here |
| 19 |
U(https://docs.aws.amazon.com/datapipeline/latest/APIReference/API_PutPipelineDefinition.html#API_PutPipelineDefinition_RequestSyntax). |
| 20 |
- Operations will wait for a configurable amount of time to ensure the pipeline is in the requested state. |
| 21 |
options: |
| 22 |
name: |
| 23 |
description: |
| 24 |
- The name of the Datapipeline to create/modify/delete. |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
description: |
| 28 |
description: |
| 29 |
- An optional description for the pipeline being created. |
| 30 |
default: '' |
| 31 |
type: str |
| 32 |
objects: |
| 33 |
type: list |
| 34 |
elements: dict |
| 35 |
default: [] |
| 36 |
description: |
| 37 |
- A list of pipeline object definitions, each of which is a dict that takes the keys I(id), I(name) and I(fields). |
| 38 |
suboptions: |
| 39 |
id: |
| 40 |
description: |
| 41 |
- The ID of the object. |
| 42 |
type: str |
| 43 |
name: |
| 44 |
description: |
| 45 |
- The name of the object. |
| 46 |
type: str |
| 47 |
fields: |
| 48 |
description: |
| 49 |
- Key-value pairs that define the properties of the object. |
| 50 |
- The value is specified as a reference to another object I(refValue) or as a string value I(stringValue) |
| 51 |
but not as both. |
| 52 |
type: list |
| 53 |
elements: dict |
| 54 |
suboptions: |
| 55 |
key: |
| 56 |
type: str |
| 57 |
description: |
| 58 |
- The field identifier. |
| 59 |
stringValue: |
| 60 |
type: str |
| 61 |
description: |
| 62 |
- The field value. |
| 63 |
- Exactly one of I(stringValue) and I(refValue) may be specified. |
| 64 |
refValue: |
| 65 |
type: str |
| 66 |
description: |
| 67 |
- The field value, expressed as the identifier of another object. |
| 68 |
- Exactly one of I(stringValue) and I(refValue) may be specified. |
| 69 |
parameters: |
| 70 |
description: |
| 71 |
- A list of parameter objects (dicts) in the pipeline definition. |
| 72 |
type: list |
| 73 |
elements: dict |
| 74 |
default: [] |
| 75 |
suboptions: |
| 76 |
id: |
| 77 |
description: |
| 78 |
- The ID of the parameter object. |
| 79 |
attributes: |
| 80 |
description: |
| 81 |
- A list of attributes (dicts) of the parameter object. |
| 82 |
type: list |
| 83 |
elements: dict |
| 84 |
suboptions: |
| 85 |
key: |
| 86 |
description: The field identifier. |
| 87 |
type: str |
| 88 |
stringValue: |
| 89 |
description: The field value. |
| 90 |
type: str |
| 91 |
|
| 92 |
values: |
| 93 |
description: |
| 94 |
- A list of parameter values (dicts) in the pipeline definition. |
| 95 |
type: list |
| 96 |
elements: dict |
| 97 |
default: [] |
| 98 |
suboptions: |
| 99 |
id: |
| 100 |
description: The ID of the parameter value |
| 101 |
type: str |
| 102 |
stringValue: |
| 103 |
description: The field value |
| 104 |
type: str |
| 105 |
timeout: |
| 106 |
description: |
| 107 |
- Time in seconds to wait for the pipeline to transition to the requested state, fail otherwise. |
| 108 |
default: 300 |
| 109 |
type: int |
| 110 |
state: |
| 111 |
description: |
| 112 |
- The requested state of the pipeline. |
| 113 |
choices: ['present', 'absent', 'active', 'inactive'] |
| 114 |
default: present |
| 115 |
type: str |
| 116 |
tags: |
| 117 |
description: |
| 118 |
- A dict of key:value pair(s) to add to the pipeline. |
| 119 |
type: dict |
| 120 |
default: {} |
| 121 |
aliases: ['resource_tags'] |
| 122 |
extends_documentation_fragment: |
| 123 |
- amazon.aws.common.modules |
| 124 |
- amazon.aws.region.modules |
| 125 |
- amazon.aws.boto3 |
| 126 |
""" |
| 127 |
|
| 128 |
EXAMPLES = r""" |
| 129 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 130 |
|
| 131 |
# Create pipeline |
| 132 |
- community.aws.data_pipeline: |
| 133 |
name: test-dp |
| 134 |
region: us-west-2 |
| 135 |
objects: "{{pipelineObjects}}" |
| 136 |
parameters: "{{pipelineParameters}}" |
| 137 |
values: "{{pipelineValues}}" |
| 138 |
tags: |
| 139 |
key1: val1 |
| 140 |
key2: val2 |
| 141 |
state: present |
| 142 |
|
| 143 |
# Example populating and activating a pipeline that demonstrates two ways of providing pipeline objects |
| 144 |
- community.aws.data_pipeline: |
| 145 |
name: test-dp |
| 146 |
objects: |
| 147 |
- id: "DefaultSchedule" |
| 148 |
name: "Every 1 day" |
| 149 |
fields: |
| 150 |
- "key": "period" |
| 151 |
"stringValue": "1 days" |
| 152 |
- "key": "type" |
| 153 |
"stringValue": "Schedule" |
| 154 |
- "key": "startAt" |
| 155 |
"stringValue": "FIRST_ACTIVATION_DATE_TIME" |
| 156 |
- id: "Default" |
| 157 |
name: "Default" |
| 158 |
fields: |
| 159 |
- "key": "resourceRole" |
| 160 |
"stringValue": "my_resource_role" |
| 161 |
- "key": "role" |
| 162 |
"stringValue": "DataPipelineDefaultRole" |
| 163 |
- "key": "pipelineLogUri" |
| 164 |
"stringValue": "s3://my_s3_log.txt" |
| 165 |
- "key": "scheduleType" |
| 166 |
"stringValue": "cron" |
| 167 |
- "key": "schedule" |
| 168 |
"refValue": "DefaultSchedule" |
| 169 |
- "key": "failureAndRerunMode" |
| 170 |
"stringValue": "CASCADE" |
| 171 |
state: active |
| 172 |
|
| 173 |
# Activate pipeline |
| 174 |
- community.aws.data_pipeline: |
| 175 |
name: test-dp |
| 176 |
region: us-west-2 |
| 177 |
state: active |
| 178 |
|
| 179 |
# Delete pipeline |
| 180 |
- community.aws.data_pipeline: |
| 181 |
name: test-dp |
| 182 |
region: us-west-2 |
| 183 |
state: absent |
| 184 |
""" |
| 185 |
|
| 186 |
RETURN = r""" |
| 187 |
changed: |
| 188 |
description: whether the data pipeline has been modified |
| 189 |
type: bool |
| 190 |
returned: always |
| 191 |
sample: |
| 192 |
changed: true |
| 193 |
result: |
| 194 |
description: |
| 195 |
- Contains the data pipeline data (data_pipeline) and a return message (msg). |
| 196 |
If the data pipeline exists data_pipeline will contain the keys description, name, |
| 197 |
pipeline_id, state, tags, and unique_id. If the data pipeline does not exist then |
| 198 |
data_pipeline will be an empty dict. The msg describes the status of the operation. |
| 199 |
returned: always |
| 200 |
type: dict |
| 201 |
""" |
| 202 |
|
| 203 |
import hashlib |
| 204 |
import json |
| 205 |
import time |
| 206 |
|
| 207 |
try: |
| 208 |
import botocore |
| 209 |
except ImportError: |
| 210 |
pass # Handled by AnsibleAWSModule |
| 211 |
|
| 212 |
from ansible.module_utils._text import to_text |
| 213 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 214 |
|
| 215 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 216 |
|
| 217 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 218 |
|
| 219 |
DP_ACTIVE_STATES = ["ACTIVE", "SCHEDULED"] |
| 220 |
DP_INACTIVE_STATES = ["INACTIVE", "PENDING", "FINISHED", "DELETING"] |
| 221 |
DP_ACTIVATING_STATE = "ACTIVATING" |
| 222 |
DP_DEACTIVATING_STATE = "DEACTIVATING" |
| 223 |
PIPELINE_DOESNT_EXIST = "^.*Pipeline with id: {0} does not exist$" |
| 224 |
|
| 225 |
|
| 226 |
class DataPipelineNotFound(Exception): |
| 227 |
pass |
| 228 |
|
| 229 |
|
| 230 |
class TimeOutException(Exception): |
| 231 |
pass |
| 232 |
|
| 233 |
|
| 234 |
def pipeline_id(client, name): |
| 235 |
"""Return pipeline id for the given pipeline name |
| 236 |
|
| 237 |
:param object client: boto3 datapipeline client |
| 238 |
:param string name: pipeline name |
| 239 |
:returns: pipeline id |
| 240 |
:raises: DataPipelineNotFound |
| 241 |
|
| 242 |
""" |
| 243 |
pipelines = client.list_pipelines() |
| 244 |
for dp in pipelines["pipelineIdList"]: |
| 245 |
if dp["name"] == name: |
| 246 |
return dp["id"] |
| 247 |
raise DataPipelineNotFound |
| 248 |
|
| 249 |
|
| 250 |
def pipeline_description(client, dp_id): |
| 251 |
"""Return pipeline description list |
| 252 |
|
| 253 |
:param object client: boto3 datapipeline client |
| 254 |
:returns: pipeline description dictionary |
| 255 |
:raises: DataPipelineNotFound |
| 256 |
|
| 257 |
""" |
| 258 |
try: |
| 259 |
return client.describe_pipelines(pipelineIds=[dp_id]) |
| 260 |
except is_boto3_error_code(["PipelineNotFoundException", "PipelineDeletedException"]): |
| 261 |
raise DataPipelineNotFound |
| 262 |
|
| 263 |
|
| 264 |
def pipeline_field(client, dp_id, field): |
| 265 |
"""Return a pipeline field from the pipeline description. |
| 266 |
|
| 267 |
The available fields are listed in describe_pipelines output. |
| 268 |
|
| 269 |
:param object client: boto3 datapipeline client |
| 270 |
:param string dp_id: pipeline id |
| 271 |
:param string field: pipeline description field |
| 272 |
:returns: pipeline field information |
| 273 |
|
| 274 |
""" |
| 275 |
dp_description = pipeline_description(client, dp_id) |
| 276 |
for field_key in dp_description["pipelineDescriptionList"][0]["fields"]: |
| 277 |
if field_key["key"] == field: |
| 278 |
return field_key["stringValue"] |
| 279 |
raise KeyError(f"Field key {field} not found!") |
| 280 |
|
| 281 |
|
| 282 |
def run_with_timeout(timeout, func, *func_args, **func_kwargs): |
| 283 |
"""Run func with the provided args and kwargs, and wait until |
| 284 |
timeout for truthy return value |
| 285 |
|
| 286 |
:param int timeout: time to wait for status |
| 287 |
:param function func: function to run, should return True or False |
| 288 |
:param args func_args: function args to pass to func |
| 289 |
:param kwargs func_kwargs: function key word args |
| 290 |
:returns: True if func returns truthy within timeout |
| 291 |
:raises: TimeOutException |
| 292 |
|
| 293 |
""" |
| 294 |
|
| 295 |
for count in range(timeout // 10): |
| 296 |
if func(*func_args, **func_kwargs): |
| 297 |
return True |
| 298 |
else: |
| 299 |
# check every 10s |
| 300 |
time.sleep(10) |
| 301 |
|
| 302 |
raise TimeOutException |
| 303 |
|
| 304 |
|
| 305 |
def check_dp_exists(client, dp_id): |
| 306 |
"""Check if datapipeline exists |
| 307 |
|
| 308 |
:param object client: boto3 datapipeline client |
| 309 |
:param string dp_id: pipeline id |
| 310 |
:returns: True or False |
| 311 |
|
| 312 |
""" |
| 313 |
try: |
| 314 |
# pipeline_description raises DataPipelineNotFound |
| 315 |
return bool(pipeline_description(client, dp_id)) |
| 316 |
except DataPipelineNotFound: |
| 317 |
return False |
| 318 |
|
| 319 |
|
| 320 |
def check_dp_status(client, dp_id, status): |
| 321 |
"""Checks if datapipeline matches states in status list |
| 322 |
|
| 323 |
:param object client: boto3 datapipeline client |
| 324 |
:param string dp_id: pipeline id |
| 325 |
:param list status: list of states to check against |
| 326 |
:returns: True or False |
| 327 |
|
| 328 |
""" |
| 329 |
if not isinstance(status, list): |
| 330 |
raise AssertionError() |
| 331 |
return bool(pipeline_field(client, dp_id, field="@pipelineState") in status) |
| 332 |
|
| 333 |
|
| 334 |
def pipeline_status_timeout(client, dp_id, status, timeout): |
| 335 |
args = (client, dp_id, status) |
| 336 |
return run_with_timeout(timeout, check_dp_status, *args) |
| 337 |
|
| 338 |
|
| 339 |
def pipeline_exists_timeout(client, dp_id, timeout): |
| 340 |
args = (client, dp_id) |
| 341 |
return run_with_timeout(timeout, check_dp_exists, *args) |
| 342 |
|
| 343 |
|
| 344 |
def activate_pipeline(client, module): |
| 345 |
"""Activates pipeline""" |
| 346 |
dp_name = module.params.get("name") |
| 347 |
timeout = module.params.get("timeout") |
| 348 |
|
| 349 |
try: |
| 350 |
dp_id = pipeline_id(client, dp_name) |
| 351 |
except DataPipelineNotFound: |
| 352 |
module.fail_json(msg=f"Data Pipeline {dp_name} not found") |
| 353 |
|
| 354 |
if pipeline_field(client, dp_id, field="@pipelineState") in DP_ACTIVE_STATES: |
| 355 |
changed = False |
| 356 |
else: |
| 357 |
try: |
| 358 |
client.activate_pipeline(pipelineId=dp_id) |
| 359 |
except is_boto3_error_code("InvalidRequestException"): |
| 360 |
module.fail_json(msg="You need to populate your pipeline before activation.") |
| 361 |
try: |
| 362 |
pipeline_status_timeout(client, dp_id, status=DP_ACTIVE_STATES, timeout=timeout) |
| 363 |
except TimeOutException: |
| 364 |
if pipeline_field(client, dp_id, field="@pipelineState") == "FINISHED": |
| 365 |
# activated but completed more rapidly than it was checked |
| 366 |
pass |
| 367 |
else: |
| 368 |
module.fail_json( |
| 369 |
msg=f"Data Pipeline {dp_name} failed to activate within timeout {timeout} seconds", |
| 370 |
) |
| 371 |
changed = True |
| 372 |
|
| 373 |
data_pipeline = get_result(client, dp_id) |
| 374 |
result = { |
| 375 |
"data_pipeline": data_pipeline, |
| 376 |
"msg": f"Data Pipeline {dp_name} activated.", |
| 377 |
} |
| 378 |
|
| 379 |
return (changed, result) |
| 380 |
|
| 381 |
|
| 382 |
def deactivate_pipeline(client, module): |
| 383 |
"""Deactivates pipeline""" |
| 384 |
dp_name = module.params.get("name") |
| 385 |
timeout = module.params.get("timeout") |
| 386 |
|
| 387 |
try: |
| 388 |
dp_id = pipeline_id(client, dp_name) |
| 389 |
except DataPipelineNotFound: |
| 390 |
module.fail_json(msg=f"Data Pipeline {dp_name} not found") |
| 391 |
|
| 392 |
if pipeline_field(client, dp_id, field="@pipelineState") in DP_INACTIVE_STATES: |
| 393 |
changed = False |
| 394 |
else: |
| 395 |
client.deactivate_pipeline(pipelineId=dp_id) |
| 396 |
try: |
| 397 |
pipeline_status_timeout(client, dp_id, status=DP_INACTIVE_STATES, timeout=timeout) |
| 398 |
except TimeOutException: |
| 399 |
module.fail_json( |
| 400 |
msg=f"Data Pipeline {dp_name} failed to deactivate within timeout {timeout} seconds", |
| 401 |
) |
| 402 |
changed = True |
| 403 |
|
| 404 |
data_pipeline = get_result(client, dp_id) |
| 405 |
result = { |
| 406 |
"data_pipeline": data_pipeline, |
| 407 |
"msg": f"Data Pipeline {dp_name} deactivated.", |
| 408 |
} |
| 409 |
|
| 410 |
return (changed, result) |
| 411 |
|
| 412 |
|
| 413 |
def _delete_dp_with_check(dp_id, client, timeout): |
| 414 |
client.delete_pipeline(pipelineId=dp_id) |
| 415 |
try: |
| 416 |
pipeline_status_timeout(client=client, dp_id=dp_id, status=[PIPELINE_DOESNT_EXIST], timeout=timeout) |
| 417 |
except DataPipelineNotFound: |
| 418 |
return True |
| 419 |
|
| 420 |
|
| 421 |
def delete_pipeline(client, module): |
| 422 |
"""Deletes pipeline""" |
| 423 |
dp_name = module.params.get("name") |
| 424 |
timeout = module.params.get("timeout") |
| 425 |
|
| 426 |
try: |
| 427 |
dp_id = pipeline_id(client, dp_name) |
| 428 |
_delete_dp_with_check(dp_id, client, timeout) |
| 429 |
changed = True |
| 430 |
except DataPipelineNotFound: |
| 431 |
changed = False |
| 432 |
except TimeOutException: |
| 433 |
module.fail_json( |
| 434 |
msg=f"Data Pipeline {dp_name} failed to delete within timeout {timeout} seconds", |
| 435 |
) |
| 436 |
result = { |
| 437 |
"data_pipeline": {}, |
| 438 |
"msg": f"Data Pipeline {dp_name} deleted", |
| 439 |
} |
| 440 |
|
| 441 |
return (changed, result) |
| 442 |
|
| 443 |
|
| 444 |
def build_unique_id(module): |
| 445 |
data = dict(module.params) |
| 446 |
# removing objects from the unique id so we can update objects or populate the pipeline after creation without needing to make a new pipeline |
| 447 |
[data.pop(each, None) for each in ("objects", "timeout")] |
| 448 |
json_data = json.dumps(data, sort_keys=True).encode("utf-8") |
| 449 |
hashed_data = hashlib.md5(json_data).hexdigest() |
| 450 |
return hashed_data |
| 451 |
|
| 452 |
|
| 453 |
def format_tags(tags): |
| 454 |
"""Reformats tags |
| 455 |
|
| 456 |
:param dict tags: dict of data pipeline tags (e.g. {key1: val1, key2: val2, key3: val3}) |
| 457 |
:returns: list of dicts (e.g. [{key: key1, value: val1}, {key: key2, value: val2}, {key: key3, value: val3}]) |
| 458 |
|
| 459 |
""" |
| 460 |
return [dict(key=k, value=v) for k, v in tags.items()] |
| 461 |
|
| 462 |
|
| 463 |
def get_result(client, dp_id): |
| 464 |
"""Get the current state of the data pipeline and reformat it to snake_case for exit_json |
| 465 |
|
| 466 |
:param object client: boto3 datapipeline client |
| 467 |
:param string dp_id: pipeline id |
| 468 |
:returns: reformatted dict of pipeline description |
| 469 |
|
| 470 |
""" |
| 471 |
# pipeline_description returns a pipelineDescriptionList of length 1 |
| 472 |
# dp is a dict with keys "description" (str), "fields" (list), "name" (str), "pipelineId" (str), "tags" (dict) |
| 473 |
dp = pipeline_description(client, dp_id)["pipelineDescriptionList"][0] |
| 474 |
|
| 475 |
# Get uniqueId and pipelineState in fields to add to the exit_json result |
| 476 |
dp["unique_id"] = pipeline_field(client, dp_id, field="uniqueId") |
| 477 |
dp["pipeline_state"] = pipeline_field(client, dp_id, field="@pipelineState") |
| 478 |
|
| 479 |
# Remove fields; can't make a list snake_case and most of the data is redundant |
| 480 |
del dp["fields"] |
| 481 |
|
| 482 |
# Note: tags is already formatted fine so we don't need to do anything with it |
| 483 |
|
| 484 |
# Reformat data pipeline and add reformatted fields back |
| 485 |
dp = camel_dict_to_snake_dict(dp) |
| 486 |
return dp |
| 487 |
|
| 488 |
|
| 489 |
def diff_pipeline(client, module, objects, unique_id, dp_name): |
| 490 |
"""Check if there's another pipeline with the same unique_id and if so, checks if the object needs to be updated""" |
| 491 |
result = {} |
| 492 |
changed = False |
| 493 |
create_dp = False |
| 494 |
|
| 495 |
# See if there is already a pipeline with the same unique_id |
| 496 |
unique_id = build_unique_id(module) |
| 497 |
try: |
| 498 |
dp_id = pipeline_id(client, dp_name) |
| 499 |
dp_unique_id = to_text(pipeline_field(client, dp_id, field="uniqueId")) |
| 500 |
if dp_unique_id != unique_id: |
| 501 |
# A change is expected but not determined. Updated to a bool in create_pipeline(). |
| 502 |
changed = "NEW_VERSION" |
| 503 |
create_dp = True |
| 504 |
# Unique ids are the same - check if pipeline needs modification |
| 505 |
else: |
| 506 |
dp_objects = client.get_pipeline_definition(pipelineId=dp_id)["pipelineObjects"] |
| 507 |
# Definition needs to be updated |
| 508 |
if dp_objects != objects: |
| 509 |
changed, msg = define_pipeline(client, module, objects, dp_id) |
| 510 |
# No changes |
| 511 |
else: |
| 512 |
msg = f"Data Pipeline {dp_name} is present" |
| 513 |
data_pipeline = get_result(client, dp_id) |
| 514 |
result = { |
| 515 |
"data_pipeline": data_pipeline, |
| 516 |
"msg": msg, |
| 517 |
} |
| 518 |
except DataPipelineNotFound: |
| 519 |
create_dp = True |
| 520 |
|
| 521 |
return create_dp, changed, result |
| 522 |
|
| 523 |
|
| 524 |
def define_pipeline(client, module, objects, dp_id): |
| 525 |
"""Puts pipeline definition""" |
| 526 |
dp_name = module.params.get("name") |
| 527 |
|
| 528 |
if pipeline_field(client, dp_id, field="@pipelineState") == "FINISHED": |
| 529 |
msg = f"Data Pipeline {dp_name} is unable to be updated while in state FINISHED." |
| 530 |
changed = False |
| 531 |
|
| 532 |
elif objects: |
| 533 |
parameters = module.params.get("parameters") |
| 534 |
values = module.params.get("values") |
| 535 |
|
| 536 |
try: |
| 537 |
client.put_pipeline_definition( |
| 538 |
pipelineId=dp_id, pipelineObjects=objects, parameterObjects=parameters, parameterValues=values |
| 539 |
) |
| 540 |
msg = f"Data Pipeline {dp_name} has been updated." |
| 541 |
changed = True |
| 542 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 543 |
module.fail_json_aws( |
| 544 |
e, |
| 545 |
msg=( |
| 546 |
f"Failed to put the definition for pipeline {dp_name}. Check that string/reference fields" |
| 547 |
"are not empty and that the number of objects in the pipeline does not exceed maximum allowed" |
| 548 |
"objects" |
| 549 |
), |
| 550 |
) |
| 551 |
else: |
| 552 |
changed = False |
| 553 |
msg = "" |
| 554 |
|
| 555 |
return changed, msg |
| 556 |
|
| 557 |
|
| 558 |
def create_pipeline(client, module): |
| 559 |
"""Creates datapipeline. Uses uniqueId to achieve idempotency.""" |
| 560 |
dp_name = module.params.get("name") |
| 561 |
objects = module.params.get("objects", None) |
| 562 |
description = module.params.get("description", "") |
| 563 |
tags = module.params.get("tags") |
| 564 |
timeout = module.params.get("timeout") |
| 565 |
|
| 566 |
unique_id = build_unique_id(module) |
| 567 |
create_dp, changed, result = diff_pipeline(client, module, objects, unique_id, dp_name) |
| 568 |
|
| 569 |
if changed == "NEW_VERSION": |
| 570 |
# delete old version |
| 571 |
changed, creation_result = delete_pipeline(client, module) |
| 572 |
|
| 573 |
# There isn't a pipeline or it has different parameters than the pipeline in existence. |
| 574 |
if create_dp: |
| 575 |
# Make pipeline |
| 576 |
try: |
| 577 |
tags = format_tags(tags) |
| 578 |
dp = client.create_pipeline(name=dp_name, uniqueId=unique_id, description=description, tags=tags) |
| 579 |
dp_id = dp["pipelineId"] |
| 580 |
pipeline_exists_timeout(client, dp_id, timeout) |
| 581 |
except TimeOutException: |
| 582 |
module.fail_json( |
| 583 |
msg=f"Data Pipeline {dp_name} failed to create within timeout {timeout} seconds", |
| 584 |
) |
| 585 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 586 |
module.fail_json_aws( |
| 587 |
e, |
| 588 |
msg=f"Failed to create the data pipeline {dp_name}.", |
| 589 |
) |
| 590 |
# Put pipeline definition |
| 591 |
changed, msg = define_pipeline(client, module, objects, dp_id) |
| 592 |
|
| 593 |
changed = True |
| 594 |
data_pipeline = get_result(client, dp_id) |
| 595 |
result = { |
| 596 |
"data_pipeline": data_pipeline, |
| 597 |
"msg": f"Data Pipeline {dp_name} created." + msg, |
| 598 |
} |
| 599 |
|
| 600 |
return (changed, result) |
| 601 |
|
| 602 |
|
| 603 |
def main(): |
| 604 |
argument_spec = dict( |
| 605 |
name=dict(required=True), |
| 606 |
description=dict(required=False, default=""), |
| 607 |
objects=dict(required=False, type="list", default=[], elements="dict"), |
| 608 |
parameters=dict(required=False, type="list", default=[], elements="dict"), |
| 609 |
timeout=dict(required=False, type="int", default=300), |
| 610 |
state=dict(default="present", choices=["present", "absent", "active", "inactive"]), |
| 611 |
tags=dict(required=False, type="dict", default={}, aliases=["resource_tags"]), |
| 612 |
values=dict(required=False, type="list", default=[], elements="dict"), |
| 613 |
) |
| 614 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=False) |
| 615 |
|
| 616 |
try: |
| 617 |
client = module.client("datapipeline") |
| 618 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 619 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 620 |
|
| 621 |
state = module.params.get("state") |
| 622 |
if state == "present": |
| 623 |
changed, result = create_pipeline(client, module) |
| 624 |
elif state == "absent": |
| 625 |
changed, result = delete_pipeline(client, module) |
| 626 |
elif state == "active": |
| 627 |
changed, result = activate_pipeline(client, module) |
| 628 |
elif state == "inactive": |
| 629 |
changed, result = deactivate_pipeline(client, module) |
| 630 |
|
| 631 |
module.exit_json(result=result, changed=changed) |
| 632 |
|
| 633 |
|
| 634 |
if __name__ == "__main__": |
| 635 |
main() |