Debian Codegraph @

ansible_collections/community/aws/plugins/modules/stepfunctions_state_machine_execution.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2019, Prasad Katti (@prasadkatti)
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: stepfunctions_state_machine_execution
10 version_added: 1.0.0
11
12 short_description: Start or stop execution of an AWS Step Functions state machine
13
14 description:
15 - Start or stop execution of a state machine in AWS Step Functions.
16 - Prior to release 5.0.0 this module was called C(community.aws.aws_step_functions_state_machine_execution).
17 The usage did not change.
18
19 options:
20 action:
21 description: Desired action (C(start) or C(stop)) for a state machine execution.
22 default: start
23 choices: [ start, stop ]
24 type: str
25 name:
26 description: Name of the execution.
27 type: str
28 execution_input:
29 description: The JSON input data for the execution.
30 type: json
31 default: {}
32 state_machine_arn:
33 description: The ARN of the state machine that will be executed.
34 type: str
35 execution_arn:
36 description: The ARN of the execution you wish to stop.
37 type: str
38 cause:
39 description: A detailed explanation of the cause for stopping the execution.
40 type: str
41 default: ''
42 error:
43 description: The error code of the failure to pass in when stopping the execution.
44 type: str
45 default: ''
46
47 author:
48 - Prasad Katti (@prasadkatti)
49
50 extends_documentation_fragment:
51 - amazon.aws.common.modules
52 - amazon.aws.region.modules
53 - amazon.aws.boto3
54 """
55
56 EXAMPLES = r"""
57 - name: Start an execution of a state machine
58 community.aws.stepfunctions_state_machine_execution:
59 name: an_execution_name
60 execution_input: '{ "IsHelloWorldExample": true }'
61 state_machine_arn: "arn:aws:states:us-west-2:123456789012:stateMachine:HelloWorldStateMachine"
62
63 - name: Stop an execution of a state machine
64 community.aws.stepfunctions_state_machine_execution:
65 action: stop
66 execution_arn: "arn:aws:states:us-west-2:123456789012:execution:HelloWorldStateMachineCopy:a1e8e2b5-5dfe-d40e-d9e3-6201061047c8"
67 cause: "cause of task failure"
68 error: "error code of the failure"
69 """
70
71 RETURN = r"""
72 execution_arn:
73 description: ARN of the AWS Step Functions state machine execution.
74 type: str
75 returned: if action == start and changed == True
76 sample: "arn:aws:states:us-west-2:123456789012:execution:HelloWorldStateMachineCopy:a1e8e2b5-5dfe-d40e-d9e3-6201061047c8"
77 start_date:
78 description: The date the execution is started.
79 type: str
80 returned: if action == start and changed == True
81 sample: "2019-11-02T22:39:49.071000-07:00"
82 stop_date:
83 description: The date the execution is stopped.
84 type: str
85 returned: if action == stop
86 sample: "2019-11-02T22:39:49.071000-07:00"
87 """
88
89
90 try:
91 import botocore
92 except ImportError:
93 pass # caught by AnsibleAWSModule
94
95 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
96
97 from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code
98
99 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
100
101
102 def start_execution(module, sfn_client):
103 """
104 start_execution uses execution name to determine if a previous execution already exists.
105 If an execution by the provided name exists, call client.start_execution will not be called.
106 """
107
108 state_machine_arn = module.params.get("state_machine_arn")
109 name = module.params.get("name")
110 execution_input = module.params.get("execution_input")
111
112 try:
113 # list_executions is eventually consistent
114 page_iterators = sfn_client.get_paginator("list_executions").paginate(stateMachineArn=state_machine_arn)
115
116 for execution in page_iterators.build_full_result()["executions"]:
117 if name == execution["name"]:
118 check_mode(module, msg="State machine execution already exists.", changed=False)
119 module.exit_json(changed=False)
120
121 check_mode(module, msg="State machine execution would be started.", changed=True)
122 res_execution = sfn_client.start_execution(stateMachineArn=state_machine_arn, name=name, input=execution_input)
123 except is_boto3_error_code("ExecutionAlreadyExists"):
124 # this will never be executed anymore
125 module.exit_json(changed=False)
126 except (
127 botocore.exceptions.ClientError,
128 botocore.exceptions.BotoCoreError,
129 ) as e: # pylint: disable=duplicate-except
130 module.fail_json_aws(e, msg="Failed to start execution.")
131
132 module.exit_json(changed=True, **camel_dict_to_snake_dict(res_execution))
133
134
135 def stop_execution(module, sfn_client):
136 cause = module.params.get("cause")
137 error = module.params.get("error")
138 execution_arn = module.params.get("execution_arn")
139
140 try:
141 # describe_execution is eventually consistent
142 execution_status = sfn_client.describe_execution(executionArn=execution_arn)["status"]
143 if execution_status != "RUNNING":
144 check_mode(module, msg="State machine execution is not running.", changed=False)
145 module.exit_json(changed=False)
146
147 check_mode(module, msg="State machine execution would be stopped.", changed=True)
148 res = sfn_client.stop_execution(executionArn=execution_arn, cause=cause, error=error)
149 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
150 module.fail_json_aws(e, msg="Failed to stop execution.")
151
152 module.exit_json(changed=True, **camel_dict_to_snake_dict(res))
153
154
155 def check_mode(module, msg="", changed=False):
156 if module.check_mode:
157 module.exit_json(changed=changed, output=msg)
158
159
160 def main():
161 module_args = dict(
162 action=dict(choices=["start", "stop"], default="start"),
163 name=dict(type="str"),
164 execution_input=dict(type="json", default={}),
165 state_machine_arn=dict(type="str"),
166 cause=dict(type="str", default=""),
167 error=dict(type="str", default=""),
168 execution_arn=dict(type="str"),
169 )
170 module = AnsibleAWSModule(
171 argument_spec=module_args,
172 required_if=[
173 ("action", "start", ["name", "state_machine_arn"]),
174 ("action", "stop", ["execution_arn"]),
175 ],
176 supports_check_mode=True,
177 )
178
179 sfn_client = module.client("stepfunctions")
180
181 action = module.params.get("action")
182 if action == "start":
183 start_execution(module, sfn_client)
184 else:
185 stop_execution(module, sfn_client)
186
187
188 if __name__ == "__main__":
189 main()