Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add two operators in AWS Providers: RedshiftResumeClusterOperator and RedshiftPauseClusterOperator #19665

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
580dd93
This commit adds new features to the airflow aws redshift module. Sim…
Nov 17, 2021
7a921c5
Add missing requirements for Static checks. Add license to all python…
Nov 18, 2021
0edb98d
Adding changes for pre-commit success
Nov 18, 2021
9911c72
Adding `ClusterStates` Enum to redshift hook to avoid having magic st…
Nov 20, 2021
a68cbbf
Adding documentation to existing AWS operator docs
Nov 22, 2021
b20a38e
Moving the cast to `ClusterStates` Enum to inside the `cluster_status…
Nov 22, 2021
81f2f75
Fixed logging to redshift sensor to show the actual value of the ENUM…
Nov 22, 2021
61b2935
Fixed logging to redshift sensor to show the actual value of the ENUM…
Nov 29, 2021
3b3f1e0
Adding `seealso` to Operator documentation
Nov 30, 2021
b151337
Update airflow/providers/amazon/aws/operators/redshift_resume_cluster.py
dbarrundiag Nov 30, 2021
ec92273
Update airflow/providers/amazon/aws/operators/redshift_pause_cluster.py
dbarrundiag Nov 30, 2021
3ce29da
Removing unused `check_interval` from operator
Nov 30, 2021
456cb73
Merge remote-tracking branch 'origin/redshift-resume-pause-cluster-op…
Nov 30, 2021
4d6e402
Update airflow/providers/amazon/aws/sensors/redshift.py
dbarrundiag Nov 30, 2021
57c4c35
Update docs/apache-airflow-providers-amazon/operators/redshift.rst
dbarrundiag Nov 30, 2021
e03031f
Update docs/apache-airflow-providers-amazon/operators/redshift.rst
dbarrundiag Nov 30, 2021
92cfcf2
Update tests/providers/amazon/aws/sensors/test_redshift.py
dbarrundiag Nov 30, 2021
06a7e3d
Rolling back changes to not use enum and move all Operators into one …
Dec 7, 2021
d20662a
Merge remote-tracking branch 'origin/redshift-resume-pause-cluster-op…
Dec 7, 2021
e72bec0
Rolling back changes to not use enum and move all Operators into one …
Dec 7, 2021
e659253
Rolling back changes to not use enum and move all Operators into one …
Dec 7, 2021
b303dc7
Rolling back changes to not use enum and move all Operators into one …
Dec 7, 2021
6c63fed
Update airflow/providers/amazon/aws/operators/redshift.py
dbarrundiag Dec 7, 2021
3ac8f33
Update airflow/providers/amazon/aws/operators/redshift.py
dbarrundiag Dec 7, 2021
50becc6
Removing no longer required subclass
Dec 7, 2021
653d6b2
Merge remote-tracking branch 'origin/redshift-resume-pause-cluster-op…
Dec 7, 2021
f012c29
Add unittests for TestPauseClusterOperator and TestResumeClusterOperator
Dec 9, 2021
a7ff6ca
Clean unittest for redshift Operators
Dec 9, 2021
f4ac7a4
Small fixups
Dec 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions airflow/providers/amazon/aws/hooks/redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class ClusterStates(Enum):
REBOOTING = 'rebooting'
RENAMING = 'renaming'
RESIZING = 'resizing'
NONEXISTENT = 'nonexistent'


class RedshiftHook(AwsBaseHook):
Expand All @@ -68,7 +69,7 @@ def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)

# TODO: Wrap create_cluster_snapshot
def cluster_status(self, cluster_identifier: str) -> str:
def cluster_status(self, cluster_identifier: str) -> ClusterStates:
"""
Return status of a cluster

Expand All @@ -81,9 +82,9 @@ def cluster_status(self, cluster_identifier: str) -> str:
"""
try:
response = self.get_conn().describe_clusters(ClusterIdentifier=cluster_identifier)['Clusters']
return response[0]['ClusterStatus'] if response else None
return ClusterStates(response[0]['ClusterStatus']) if response else None
except self.get_conn().exceptions.ClusterNotFoundFault:
return 'cluster_not_found'
dstandish marked this conversation as resolved.
Show resolved Hide resolved
return ClusterStates.NONEXISTENT

def delete_cluster(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ def __init__(
def execute(self, context):
redshift_hook = RedshiftHook(aws_conn_id=self.aws_conn_id)
self.log.info("Pausing Redshift cluster %s", self.cluster_identifier)
cluster_state = ClusterStates(redshift_hook.cluster_status(cluster_identifier=self.cluster_identifier))
cluster_state = redshift_hook.cluster_status(cluster_identifier=self.cluster_identifier)
if cluster_state == ClusterStates.AVAILABLE:
redshift_hook.get_conn().pause_cluster(ClusterIdentifier=self.cluster_identifier)
dbarrundiag marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ def __init__(
def execute(self, context):
redshift_hook = RedshiftHook(aws_conn_id=self.aws_conn_id)
self.log.info("Starting Redshift cluster %s", self.cluster_identifier)
cluster_state = ClusterStates(redshift_hook.cluster_status(cluster_identifier=self.cluster_identifier))
cluster_state = redshift_hook.cluster_status(cluster_identifier=self.cluster_identifier)
if cluster_state == ClusterStates.PAUSED:
redshift_hook.get_conn().resume_cluster(ClusterIdentifier=self.cluster_identifier)
7 changes: 5 additions & 2 deletions airflow/providers/amazon/aws/sensors/redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# under the License.
from typing import Optional

from airflow.providers.amazon.aws.hooks.redshift import RedshiftHook
from airflow.providers.amazon.aws.hooks.redshift import ClusterStates, RedshiftHook
from airflow.sensors.base import BaseSensorOperator


Expand All @@ -43,7 +43,10 @@ def __init__(
):
super().__init__(**kwargs)
self.cluster_identifier = cluster_identifier
self.target_status = target_status
self.target_status = (
target_status if isinstance(target_status, ClusterStates) else ClusterStates(str(target_status))
)

self.aws_conn_id = aws_conn_id
self.hook: Optional[RedshiftHook] = None

dbarrundiag marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
6 changes: 3 additions & 3 deletions tests/providers/amazon/aws/hooks/test_redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

from airflow.models import Connection
from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook
from airflow.providers.amazon.aws.hooks.redshift import RedshiftHook, RedshiftSQLHook
from airflow.providers.amazon.aws.hooks.redshift import ClusterStates, RedshiftHook, RedshiftSQLHook

try:
from moto import mock_redshift
Expand Down Expand Up @@ -98,15 +98,15 @@ def test_cluster_status_returns_cluster_not_found(self):
self._create_clusters()
hook = RedshiftHook(aws_conn_id='aws_default')
status = hook.cluster_status('test_cluster_not_here')
assert status == 'cluster_not_found'
assert status == ClusterStates.NONEXISTENT

@unittest.skipIf(mock_redshift is None, 'mock_redshift package not present')
@mock_redshift
def test_cluster_status_returns_available_cluster(self):
self._create_clusters()
hook = RedshiftHook(aws_conn_id='aws_default')
status = hook.cluster_status('test_cluster')
assert status == 'available'
assert status == ClusterStates.AVAILABLE


class TestRedshiftSQLHookConn(unittest.TestCase):
Expand Down
17 changes: 16 additions & 1 deletion tests/providers/amazon/aws/sensors/test_redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import boto3

from airflow.providers.amazon.aws.hooks.redshift import ClusterStates
from airflow.providers.amazon.aws.sensors.redshift import AwsRedshiftClusterSensor

try:
Expand Down Expand Up @@ -56,6 +57,20 @@ def test_poke(self):
)
assert op.poke(None)

@unittest.skipIf(mock_redshift is None, 'mock_redshift package not present')
@mock_redshift
def test_poke_with_cluster_state(self):
self._create_cluster()
op = AwsRedshiftClusterSensor(
task_id='test_cluster_sensor',
poke_interval=1,
timeout=5,
aws_conn_id='aws_default',
cluster_identifier='test_cluster',
target_status=ClusterStates.AVAILABLE,
)
assert op.poke(None)

@unittest.skipIf(mock_redshift is None, 'mock_redshift package not present')
@mock_redshift
def test_poke_false(self):
Expand All @@ -81,7 +96,7 @@ def test_poke_cluster_not_found(self):
timeout=5,
aws_conn_id='aws_default',
cluster_identifier='test_cluster_not_found',
target_status='cluster_not_found',
target_status=ClusterStates.NONEXISTENT,
)

assert op.poke(None)
dbarrundiag marked this conversation as resolved.
Show resolved Hide resolved