-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedshiftClusterManager.py
92 lines (80 loc) · 2.93 KB
/
RedshiftClusterManager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import boto3
class RedshiftClusterManager:
def __init__(self):
self.client = boto3.client('redshift')
def createClusterWithoutWaiting(self, clusterID, \
masterUser, masterPassword, arnRole, DBName, numberOfNodes):
clusterType = ''
if numberOfNodes == 1:
clusterType = 'single-node'
elif numberOfNodes > 1:
clusterType = 'multi-node'
elif numberOfNodes < 1:
numberOfNodes = 1
clusterType = 'single-node'
response = self.client.create_cluster(
DBName=DBName,
ClusterIdentifier=clusterID,
ClusterType=clusterType,
NodeType='dc2.large',
MasterUsername=masterUser,
MasterUserPassword=masterPassword,
Port=5439,
ClusterVersion='1.0',
NumberOfNodes=numberOfNodes,
PubliclyAccessible=True,
IamRoles=[
arnRole,
],
)
return response
def createClusterAndWait(self, clusterID, \
masterUser, masterPassword, arnRole, DBName, numberOfNodes):
waiter = self.client.get_waiter('cluster_available')
creationResponse = self.createClusterWithoutWaiting(clusterID, \
masterUser, masterPassword, arnRole, DBName, numberOfNodes)
print('Creating cluster started')
waiter.wait(
ClusterIdentifier=clusterID,
WaiterConfig={
'Delay': 30,
'MaxAttempts': 30
}
)
print('Creating cluster ended')
return creationResponse
def getClusterEvents(self, clusterID):
response = self.client.describe_events(
SourceIdentifier=clusterID,
SourceType='cluster'
)
return response
def describeClusters(self, clusterID):
return self.client.describe_clusters(ClusterIdentifier=clusterID)
def deleteClusterWithoutWaiting(self, clusterID):
return self.client.delete_cluster(
ClusterIdentifier = clusterID,
SkipFinalClusterSnapshot=True,
)
def deleteClusterAndWait(self, clusterID):
self.deleteClusterWithoutWaiting(clusterID)
waiter = self.client.get_waiter('cluster_deleted')
print('Deleting started')
waiter.wait(
ClusterIdentifier=clusterID,
WaiterConfig={
'Delay': 30,
'MaxAttempts': 30
}
)
print('Deleting ended')
def getCredentials(self, clusterID, DbUser, DbName):
print('DbUser: ' + DbUser)
print('DbName: ' + DbName)
return self.client.get_cluster_credentials(
DbUser=DbUser,
DbName=DbName,
ClusterIdentifier=clusterID,
AutoCreate=False)
def getEndpoint(self, clusterID):
return self.describeClusters(clusterID)['Clusters'][0]['Endpoint']['Address'].replace('"', '')