o-nikolas commented on code in PR #35790:
URL: https://github.com/apache/airflow/pull/35790#discussion_r1402634064


##########
airflow/providers/amazon/aws/operators/ec2.py:
##########
@@ -254,3 +255,128 @@ def execute(self, context: Context):
                         "MaxAttempts": self.max_attempts,
                     },
                 )
+
+
+class EC2RebootInstanceOperator(BaseOperator):
+    """
+    Reboot Amazon EC2 instance.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the 
guide:
+        :ref:`howto/operator:EC2RebootInstanceOperator`
+
+    :param instance_ids: ID of the instance(s) to be terminated.
+    :param aws_conn_id: AWS connection to use
+    :param region_name: AWS region name associated with the client.
+    :param poll_interval: Number of seconds to wait before attempting to
+        check state of instance. Only used if wait_for_completion is True. 
Default is 20.
+    :param max_attempts: Maximum number of attempts when checking state of 
instance.
+        Only used if wait_for_completion is True. Default is 20.
+    :param wait_for_completion: If True, the operator will wait for the 
instance to be
+        in the `running` state before returning.
+    """
+
+    template_fields: Sequence[str] = ("instance_ids", "region_name")
+    ui_color = "#eeaa11"
+    ui_fgcolor = "#ffffff"
+
+    def __init__(
+        self,
+        *,
+        instance_ids: str | list[str],
+        aws_conn_id: str = "aws_default",
+        region_name: str | None = None,
+        poll_interval: int = 20,
+        max_attempts: int = 20,
+        wait_for_completion: bool = False,
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.instance_ids = instance_ids
+        self.aws_conn_id = aws_conn_id
+        self.region_name = region_name
+        self.poll_interval = poll_interval
+        self.max_attempts = max_attempts
+        self.wait_for_completion = wait_for_completion
+
+    def execute(self, context: Context):
+        if isinstance(self.instance_ids, str):
+            self.instance_ids = [self.instance_ids]
+        ec2_hook = EC2Hook(aws_conn_id=self.aws_conn_id, 
region_name=self.region_name, api_type="client_type")
+        self.log.info("Rebooting EC2 instances %s", ", 
".join(self.instance_ids))
+        ec2_hook.conn.reboot_instances(InstanceIds=self.instance_ids)
+
+        if self.wait_for_completion:
+            ec2_hook.get_waiter("instance_running").wait(
+                InstanceIds=self.instance_ids,
+                WaiterConfig={
+                    "Delay": self.poll_interval,
+                    "MaxAttempts": self.max_attempts,
+                },
+            )
+
+
+class EC2HibernateInstanceOperator(BaseOperator):
+    """
+    Hibernate Amazon EC2 instance.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the 
guide:
+        :ref:`howto/operator:EC2HibernateInstanceOperator`
+
+    :param instance_ids: ID of the instance(s) to be terminated.

Review Comment:
   ```suggestion
       :param instance_ids: ID of the instance(s) to be hibernated.
   ```



##########
airflow/providers/amazon/aws/operators/ec2.py:
##########
@@ -254,3 +255,128 @@ def execute(self, context: Context):
                         "MaxAttempts": self.max_attempts,
                     },
                 )
+
+
+class EC2RebootInstanceOperator(BaseOperator):
+    """
+    Reboot Amazon EC2 instance.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the 
guide:
+        :ref:`howto/operator:EC2RebootInstanceOperator`
+
+    :param instance_ids: ID of the instance(s) to be terminated.

Review Comment:
   ```suggestion
       :param instance_ids: ID of the instance(s) to be rebooted.
   ```



##########
tests/system/providers/amazon/aws/example_ec2.py:
##########
@@ -142,6 +145,22 @@ def parse_response(instance_ids: list):
     )
     # [END howto_sensor_ec2_instance_state]
 
+    # [START howto_operator_ec2_reboot_instance]
+    reboot_instance = EC2RebootInstanceOperator(
+        task_id="reboot_instace",
+        instance_ids=instance_id,
+    )
+    # [END howto_operator_ec2_reboot_instance]
+    reboot_instance.trigger_rule = TriggerRule.ALL_DONE

Review Comment:
   We don't need `TriggerRule.ALL_DONE` here (or on the hibernate), that's only 
for steps that are needed for cleanup, to ensure they run and don't leave 
behind resources. So it's only really needed on the terminate instance 
operator. 



##########
airflow/providers/amazon/aws/operators/ec2.py:
##########
@@ -254,3 +255,128 @@ def execute(self, context: Context):
                         "MaxAttempts": self.max_attempts,
                     },
                 )
+
+
+class EC2RebootInstanceOperator(BaseOperator):
+    """
+    Reboot Amazon EC2 instance.

Review Comment:
   ```suggestion
       Reboot Amazon EC2 instances.
   ```



##########
airflow/providers/amazon/aws/operators/ec2.py:
##########
@@ -254,3 +255,128 @@ def execute(self, context: Context):
                         "MaxAttempts": self.max_attempts,
                     },
                 )
+
+
+class EC2RebootInstanceOperator(BaseOperator):
+    """
+    Reboot Amazon EC2 instance.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the 
guide:
+        :ref:`howto/operator:EC2RebootInstanceOperator`
+
+    :param instance_ids: ID of the instance(s) to be terminated.
+    :param aws_conn_id: AWS connection to use
+    :param region_name: AWS region name associated with the client.
+    :param poll_interval: Number of seconds to wait before attempting to
+        check state of instance. Only used if wait_for_completion is True. 
Default is 20.
+    :param max_attempts: Maximum number of attempts when checking state of 
instance.
+        Only used if wait_for_completion is True. Default is 20.
+    :param wait_for_completion: If True, the operator will wait for the 
instance to be
+        in the `running` state before returning.
+    """
+
+    template_fields: Sequence[str] = ("instance_ids", "region_name")
+    ui_color = "#eeaa11"
+    ui_fgcolor = "#ffffff"
+
+    def __init__(
+        self,
+        *,
+        instance_ids: str | list[str],
+        aws_conn_id: str = "aws_default",
+        region_name: str | None = None,
+        poll_interval: int = 20,
+        max_attempts: int = 20,
+        wait_for_completion: bool = False,
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.instance_ids = instance_ids
+        self.aws_conn_id = aws_conn_id
+        self.region_name = region_name
+        self.poll_interval = poll_interval
+        self.max_attempts = max_attempts
+        self.wait_for_completion = wait_for_completion
+
+    def execute(self, context: Context):
+        if isinstance(self.instance_ids, str):
+            self.instance_ids = [self.instance_ids]
+        ec2_hook = EC2Hook(aws_conn_id=self.aws_conn_id, 
region_name=self.region_name, api_type="client_type")
+        self.log.info("Rebooting EC2 instances %s", ", 
".join(self.instance_ids))
+        ec2_hook.conn.reboot_instances(InstanceIds=self.instance_ids)
+
+        if self.wait_for_completion:
+            ec2_hook.get_waiter("instance_running").wait(
+                InstanceIds=self.instance_ids,
+                WaiterConfig={
+                    "Delay": self.poll_interval,
+                    "MaxAttempts": self.max_attempts,
+                },
+            )
+
+
+class EC2HibernateInstanceOperator(BaseOperator):
+    """
+    Hibernate Amazon EC2 instance.

Review Comment:
   ```suggestion
       Hibernate Amazon EC2 instances.
   ```



-- 
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.

To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to