Skip to content

Commit

Permalink
Merge pull request #2971 from bpandola/fix-2967
Browse files Browse the repository at this point in the history
Add `Redshift.ClusterAlreadyExists` Error
  • Loading branch information
bblommers committed May 7, 2020
2 parents 2052a07 + 55f2070 commit ff23489
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions moto/redshift/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,10 @@ def __init__(self, cluster_identifier):
cluster_identifier
),
)


class ClusterAlreadyExistsFaultError(RedshiftClientError):
def __init__(self):
super(ClusterAlreadyExistsFaultError, self).__init__(
"ClusterAlreadyExists", "Cluster already exists"
)
3 changes: 3 additions & 0 deletions moto/redshift/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from moto.core.utils import iso_8601_datetime_with_milliseconds
from moto.ec2 import ec2_backends
from .exceptions import (
ClusterAlreadyExistsFaultError,
ClusterNotFoundError,
ClusterParameterGroupNotFoundError,
ClusterSecurityGroupNotFoundError,
Expand Down Expand Up @@ -580,6 +581,8 @@ def modify_snapshot_copy_retention_period(

def create_cluster(self, **cluster_kwargs):
cluster_identifier = cluster_kwargs["cluster_identifier"]
if cluster_identifier in self.clusters:
raise ClusterAlreadyExistsFaultError()
cluster = Cluster(self, **cluster_kwargs)
self.clusters[cluster_identifier] = cluster
return cluster
Expand Down
22 changes: 22 additions & 0 deletions tests/test_redshift/test_redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,11 @@ def test_create_cluster_from_snapshot():
ClusterIdentifier=original_cluster_identifier,
)

client.restore_from_cluster_snapshot.when.called_with(
ClusterIdentifier=original_cluster_identifier,
SnapshotIdentifier=original_snapshot_identifier,
).should.throw(ClientError, "ClusterAlreadyExists")

response = client.restore_from_cluster_snapshot(
ClusterIdentifier=new_cluster_identifier,
SnapshotIdentifier=original_snapshot_identifier,
Expand Down Expand Up @@ -1333,3 +1338,20 @@ def test_modify_snapshot_copy_retention_period():
response = client.describe_clusters(ClusterIdentifier="test")
cluster_snapshot_copy_status = response["Clusters"][0]["ClusterSnapshotCopyStatus"]
cluster_snapshot_copy_status["RetentionPeriod"].should.equal(5)


@mock_redshift
def test_create_duplicate_cluster_fails():
kwargs = {
"ClusterIdentifier": "test",
"ClusterType": "single-node",
"DBName": "test",
"MasterUsername": "user",
"MasterUserPassword": "password",
"NodeType": "ds2.xlarge",
}
client = boto3.client("redshift", region_name="us-east-1")
client.create_cluster(**kwargs)
client.create_cluster.when.called_with(**kwargs).should.throw(
ClientError, "ClusterAlreadyExists"
)

0 comments on commit ff23489

Please sign in to comment.