You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/03/15 19:22:15 UTC

[GitHub] [airflow] mik-laj commented on a change in pull request #7731: [AIRFLOW-7068] Create EC2 Hook, Operator and Sensor

mik-laj commented on a change in pull request #7731: [AIRFLOW-7068] Create EC2 Hook, Operator and Sensor
URL: https://github.com/apache/airflow/pull/7731#discussion_r392703975
 
 

 ##########
 File path: airflow/providers/amazon/aws/operators/ec2.py
 ##########
 @@ -0,0 +1,102 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+import time
+from typing import Optional
+
+from airflow.exceptions import AirflowException
+from airflow.models import BaseOperator
+from airflow.providers.amazon.aws.hooks.ec2 import EC2Hook
+from airflow.utils.decorators import apply_defaults
+
+
+class EC2Operator(BaseOperator):
+    """
+    Manage AWS EC2 instance using boto3.
+    Change instance state by applying given operation.
+
+    :param operation: action to be taken on AWS EC2 instance
+        valid values: "start", "stop"
+    :type operation: str
+    :param instance_id: id of the AWS EC2 instance
+    :type instance_id: str
+    :param aws_conn_id: aws connection to use
+    :type aws_conn_id: str
+    :param region_name: (optional) aws region name associated with the client
+    :type region_name: Optional[str]
+    :param check_interval: time in seconds that the job should wait in
+        between each instance state checks until operation is completed
+    :type check_interval: float
+    """
+
+    template_fields = ["operation", "region_name"]
+    ui_color = "#eeaa11"
+    ui_fgcolor = "#ffffff"
+    valid_operations = ["start", "stop"]
+    operation_target_state_map = {
+        "start": "running",
+        "stop": "stopped",
+    }
+
+    @apply_defaults
+    def __init__(self,
+                 operation: str,
+                 instance_id: str,
+                 aws_conn_id: str = "aws_default",
+                 region_name: Optional[str] = None,
+                 check_interval: float = 15,
+                 *args,
+                 **kwargs):
+        if operation not in self.valid_operations:
+            raise AirflowException(f"Invalid operation: {operation}")
+        super().__init__(*args, **kwargs)
+        self.operation = operation
+        self.instance_id = instance_id
+        self.aws_conn_id = aws_conn_id
+        self.region_name = region_name
+        self.check_interval = check_interval
+        self.target_state = self.operation_target_state_map[self.operation]
+        self.hook = self.get_hook()
+
+    def execute(self, context):
+        self.log.info("Executing: %s %s", self.operation, self.instance_id)
+
+        instance = self.hook.get_conn().Instance(id=self.instance_id)
+
+        if self.operation == "start":
 
 Review comment:
   It is recommended to create one operator for each operation.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services