# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Purpose Shows how to use the AWS SDK for Python (Boto3) to create and manage Amazon Redshift clusters. """ import boto3 from botocore.exceptions import ClientError import logging # snippet-start:[python.example_code.redshift.RedshiftWrapper] class RedshiftWrapper: """ Encapsulates Amazon Redshift cluster operations. """ def __init__(self, redshift_client): """ :param redshift_client: A Boto3 Redshift client. """ self.client = redshift_client # snippet-end:[python.example_code.redshift.RedshiftWrapper] @classmethod def from_client(cls): """ Creates a wrapper object from a Boto3 client. :return: The wrapper object. """ client = boto3.client("redshift") return cls(client) # snippet-start:[python.example_code.redshift.CreateCluster] def create_cluster( self, cluster_identifier, node_type, master_username, master_user_password, publicly_accessible, number_of_nodes, ): """ Creates a cluster. :param cluster_identifier: The name of the cluster. :param node_type: The type of node in the cluster. :param master_username: The master username. :param master_user_password: The master user password. :param publicly_accessible: Whether the cluster is publicly accessible. :param number_of_nodes: The number of nodes in the cluster. :return: The cluster. """ try: cluster = self.client.create_cluster( ClusterIdentifier=cluster_identifier, NodeType=node_type, MasterUsername=master_username, MasterUserPassword=master_user_password, PubliclyAccessible=publicly_accessible, NumberOfNodes=number_of_nodes, ) return cluster except ClientError as err: logging.error( "Couldn't create a cluster. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise # snippet-end:[python.example_code.redshift.CreateCluster] # snippet-start:[python.example_code.redshift.DeleteCluster] def delete_cluster(self, cluster_identifier): """ Deletes a cluster. :param cluster_identifier: The cluster identifier. """ try: self.client.delete_cluster( ClusterIdentifier=cluster_identifier, SkipFinalClusterSnapshot=True ) except ClientError as err: logging.error( "Couldn't delete a cluster. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise # snippet-end:[python.example_code.redshift.DeleteCluster] # snippet-start:[python.example_code.redshift.ModifyCluster] def modify_cluster(self, cluster_identifier, preferred_maintenance_window): """ Modifies a cluster. :param cluster_identifier: The cluster identifier. :param preferred_maintenance_window: The preferred maintenance window. """ try: self.client.modify_cluster( ClusterIdentifier=cluster_identifier, PreferredMaintenanceWindow=preferred_maintenance_window, ) except ClientError as err: logging.error( "Couldn't modify a cluster. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise # snippet-end:[python.example_code.redshift.ModifyCluster] # snippet-start:[python.example_code.redshift.DescribeClusters] def describe_clusters(self, cluster_identifier): """ Describes a cluster. :param cluster_identifier: The cluster identifier. :return: A list of clusters. """ try: kwargs = {} if cluster_identifier: kwargs["ClusterIdentifier"] = cluster_identifier paginator = self.client.get_paginator("describe_clusters") clusters = [] for page in paginator.paginate(**kwargs): clusters.extend(page["Clusters"]) return clusters except ClientError as err: logging.error( "Couldn't describe a cluster. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise # snippet-end:[python.example_code.redshift.DescribeClusters] if __name__ == "__main__": # Demonstrates how to initiate the wrapper object and use it. # snippet-start:[python.example_code.redshift.RedshiftWrapper.instantiation] client = boto3.client("redshift") redhift_wrapper = RedshiftWrapper(client) # snippet-end:[python.example_code.redshift.RedshiftWrapper.instantiation] redhift_wrapper.describe_clusters(cluster_identifier=None)