|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | + |
| 19 | +import json |
| 20 | +from typing import Optional, Union |
| 21 | + |
| 22 | +from airflow.exceptions import AirflowException |
| 23 | +from airflow.models import BaseOperator |
| 24 | +from airflow.providers.amazon.aws.hooks.step_function import StepFunctionHook |
| 25 | + |
| 26 | + |
| 27 | +class StepFunctionStartExecutionOperator(BaseOperator): |
| 28 | + """ |
| 29 | + An Operator that begins execution of an Step Function State Machine |
| 30 | +
|
| 31 | + Additional arguments may be specified and are passed down to the underlying BaseOperator. |
| 32 | +
|
| 33 | + .. seealso:: |
| 34 | + :class:`~airflow.models.BaseOperator` |
| 35 | +
|
| 36 | + :param state_machine_arn: ARN of the Step Function State Machine |
| 37 | + :type state_machine_arn: str |
| 38 | + :param name: The name of the execution. |
| 39 | + :type name: Optional[str] |
| 40 | + :param state_machine_input: JSON data input to pass to the State Machine |
| 41 | + :type state_machine_input: Union[Dict[str, any], str, None] |
| 42 | + :param aws_conn_id: aws connection to uses |
| 43 | + :type aws_conn_id: str |
| 44 | + :param do_xcom_push: if True, execution_arn is pushed to XCom with key execution_arn. |
| 45 | + :type do_xcom_push: bool |
| 46 | + """ |
| 47 | + |
| 48 | + template_fields = ['state_machine_arn', 'name', 'input'] |
| 49 | + template_ext = () |
| 50 | + ui_color = '#f9c915' |
| 51 | + |
| 52 | + def __init__( |
| 53 | + self, |
| 54 | + *, |
| 55 | + state_machine_arn: str, |
| 56 | + name: Optional[str] = None, |
| 57 | + state_machine_input: Union[dict, str, None] = None, |
| 58 | + aws_conn_id: str = 'aws_default', |
| 59 | + region_name: Optional[str] = None, |
| 60 | + **kwargs, |
| 61 | + ): |
| 62 | + super().__init__(**kwargs) |
| 63 | + self.state_machine_arn = state_machine_arn |
| 64 | + self.name = name |
| 65 | + self.input = state_machine_input |
| 66 | + self.aws_conn_id = aws_conn_id |
| 67 | + self.region_name = region_name |
| 68 | + |
| 69 | + def execute(self, context): |
| 70 | + hook = StepFunctionHook(aws_conn_id=self.aws_conn_id, region_name=self.region_name) |
| 71 | + |
| 72 | + execution_arn = hook.start_execution(self.state_machine_arn, self.name, self.input) |
| 73 | + |
| 74 | + if execution_arn is None: |
| 75 | + raise AirflowException(f'Failed to start State Machine execution for: {self.state_machine_arn}') |
| 76 | + |
| 77 | + self.log.info('Started State Machine execution for %s: %s', self.state_machine_arn, execution_arn) |
| 78 | + |
| 79 | + return execution_arn |
| 80 | + |
| 81 | + |
| 82 | +class StepFunctionGetExecutionOutputOperator(BaseOperator): |
| 83 | + """ |
| 84 | + An Operator that begins execution of an Step Function State Machine |
| 85 | +
|
| 86 | + Additional arguments may be specified and are passed down to the underlying BaseOperator. |
| 87 | +
|
| 88 | + .. seealso:: |
| 89 | + :class:`~airflow.models.BaseOperator` |
| 90 | +
|
| 91 | + :param execution_arn: ARN of the Step Function State Machine Execution |
| 92 | + :type execution_arn: str |
| 93 | + :param aws_conn_id: aws connection to use, defaults to 'aws_default' |
| 94 | + :type aws_conn_id: str |
| 95 | + """ |
| 96 | + |
| 97 | + template_fields = ['execution_arn'] |
| 98 | + template_ext = () |
| 99 | + ui_color = '#f9c915' |
| 100 | + |
| 101 | + def __init__( |
| 102 | + self, |
| 103 | + *, |
| 104 | + execution_arn: str, |
| 105 | + aws_conn_id: str = 'aws_default', |
| 106 | + region_name: Optional[str] = None, |
| 107 | + **kwargs, |
| 108 | + ): |
| 109 | + super().__init__(**kwargs) |
| 110 | + self.execution_arn = execution_arn |
| 111 | + self.aws_conn_id = aws_conn_id |
| 112 | + self.region_name = region_name |
| 113 | + |
| 114 | + def execute(self, context): |
| 115 | + hook = StepFunctionHook(aws_conn_id=self.aws_conn_id, region_name=self.region_name) |
| 116 | + |
| 117 | + execution_status = hook.describe_execution(self.execution_arn) |
| 118 | + execution_output = json.loads(execution_status['output']) if 'output' in execution_status else None |
| 119 | + |
| 120 | + self.log.info('Got State Machine Execution output for %s', self.execution_arn) |
| 121 | + |
| 122 | + return execution_output |
0 commit comments