Source code for airflow.providers.amazon.aws.hooks.redshift_cluster
# 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.from__future__importannotationsimportwarningsfromtypingimportAny,Sequencefrombotocore.exceptionsimportClientErrorfromairflow.providers.amazon.aws.hooks.base_awsimportAwsBaseHook
[docs]classRedshiftHook(AwsBaseHook):""" Interact with Amazon Redshift. Provide thin wrapper around :external+boto3:py:class:`boto3.client("redshift") <Redshift.Client>`. Additional arguments (such as ``aws_conn_id``) may be specified and are passed down to the underlying AwsBaseHook. .. seealso:: - :class:`airflow.providers.amazon.aws.hooks.base_aws.AwsBaseHook` """
[docs]defcreate_cluster(self,cluster_identifier:str,node_type:str,master_username:str,master_user_password:str,params:dict[str,Any],)->dict[str,Any]:""" Creates a new cluster with the specified parameters .. seealso:: - :external+boto3:py:meth:`Redshift.Client.create_cluster` :param cluster_identifier: A unique identifier for the cluster. :param node_type: The node type to be provisioned for the cluster. Valid Values: ``ds2.xlarge``, ``ds2.8xlarge``, ``dc1.large``, ``dc1.8xlarge``, ``dc2.large``, ``dc2.8xlarge``, ``ra3.xlplus``, ``ra3.4xlarge``, and ``ra3.16xlarge``. :param master_username: The username associated with the admin user account for the cluster that is being created. :param master_user_password: password associated with the admin user account for the cluster that is being created. :param params: Remaining AWS Create cluster API params. """try:response=self.get_conn().create_cluster(ClusterIdentifier=cluster_identifier,NodeType=node_type,MasterUsername=master_username,MasterUserPassword=master_user_password,**params,)returnresponseexceptClientErrorase:raisee
# TODO: Wrap create_cluster_snapshot
[docs]defcluster_status(self,cluster_identifier:str)->str:""" Return status of a cluster .. seealso:: - :external+boto3:py:meth:`Redshift.Client.describe_clusters` :param cluster_identifier: unique identifier of a cluster :param skip_final_cluster_snapshot: determines cluster snapshot creation :param final_cluster_snapshot_identifier: Optional[str] """try:response=self.get_conn().describe_clusters(ClusterIdentifier=cluster_identifier)["Clusters"]returnresponse[0]["ClusterStatus"]ifresponseelseNoneexceptself.get_conn().exceptions.ClusterNotFoundFault:return"cluster_not_found"
[docs]defdelete_cluster(self,cluster_identifier:str,skip_final_cluster_snapshot:bool=True,final_cluster_snapshot_identifier:str|None=None,):""" Delete a cluster and optionally create a snapshot .. seealso:: - :external+boto3:py:meth:`Redshift.Client.delete_cluster` :param cluster_identifier: unique identifier of a cluster :param skip_final_cluster_snapshot: determines cluster snapshot creation :param final_cluster_snapshot_identifier: name of final cluster snapshot """final_cluster_snapshot_identifier=final_cluster_snapshot_identifieror""response=self.get_conn().delete_cluster(ClusterIdentifier=cluster_identifier,SkipFinalClusterSnapshot=skip_final_cluster_snapshot,FinalClusterSnapshotIdentifier=final_cluster_snapshot_identifier,)returnresponse["Cluster"]ifresponse["Cluster"]elseNone
[docs]defdescribe_cluster_snapshots(self,cluster_identifier:str)->list[str]|None:""" Gets a list of snapshots for a cluster .. seealso:: - :external+boto3:py:meth:`Redshift.Client.describe_cluster_snapshots` :param cluster_identifier: unique identifier of a cluster """response=self.get_conn().describe_cluster_snapshots(ClusterIdentifier=cluster_identifier)if"Snapshots"notinresponse:returnNonesnapshots=response["Snapshots"]snapshots=[snapshotforsnapshotinsnapshotsifsnapshot["Status"]]snapshots.sort(key=lambdax:x["SnapshotCreateTime"],reverse=True)returnsnapshots
[docs]defrestore_from_cluster_snapshot(self,cluster_identifier:str,snapshot_identifier:str)->str:""" Restores a cluster from its snapshot .. seealso:: - :external+boto3:py:meth:`Redshift.Client.restore_from_cluster_snapshot` :param cluster_identifier: unique identifier of a cluster :param snapshot_identifier: unique identifier for a snapshot of a cluster """response=self.get_conn().restore_from_cluster_snapshot(ClusterIdentifier=cluster_identifier,SnapshotIdentifier=snapshot_identifier)returnresponse["Cluster"]ifresponse["Cluster"]elseNone
[docs]defcreate_cluster_snapshot(self,snapshot_identifier:str,cluster_identifier:str,retention_period:int=-1)->str:""" Creates a snapshot of a cluster .. seealso:: - :external+boto3:py:meth:`Redshift.Client.create_cluster_snapshot` :param snapshot_identifier: unique identifier for a snapshot of a cluster :param cluster_identifier: unique identifier of a cluster :param retention_period: The number of days that a manual snapshot is retained. If the value is -1, the manual snapshot is retained indefinitely. """response=self.get_conn().create_cluster_snapshot(SnapshotIdentifier=snapshot_identifier,ClusterIdentifier=cluster_identifier,ManualSnapshotRetentionPeriod=retention_period,)returnresponse["Snapshot"]ifresponse["Snapshot"]elseNone
[docs]defget_cluster_snapshot_status(self,snapshot_identifier:str,cluster_identifier:str|None=None):""" Return Redshift cluster snapshot status. If cluster snapshot not found return ``None`` :param snapshot_identifier: A unique identifier for the snapshot that you are requesting :param cluster_identifier: (deprecated) The unique identifier of the cluster the snapshot was created from """ifcluster_identifier:warnings.warn("Parameter `cluster_identifier` is deprecated.""This option will be removed in a future version.",DeprecationWarning,stacklevel=2,)try:response=self.get_conn().describe_cluster_snapshots(SnapshotIdentifier=snapshot_identifier,)snapshot=response.get("Snapshots")[0]snapshot_status:str=snapshot.get("Status")returnsnapshot_statusexceptself.get_conn().exceptions.ClusterSnapshotNotFoundFault:returnNone