forked from awslabs/aws-lambda-redshift-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addAdditionalClusterEndpoint.js
226 lines (185 loc) · 6.4 KB
/
addAdditionalClusterEndpoint.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Amazon Software License (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/asl/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
/**
* Ask questions of the end user via STDIN and then setup the dynamo DB table
* entry for the configuration when done
*/
var readline = require('readline');
var aws = require('aws-sdk');
var dynamoDB;
require('./constants');
var kmsCrypto = require('./kmsCrypto');
var setRegion = 'us-east-1';
var common = require('./common');
var async = require('async');
// simple frame for the updated cluster config
var clusterConfig = {
M: {}
};
var updateRequest = {
Key: {
s3Prefix: undefined
},
TableName: configTable,
UpdateExpression: "SET loadClusters = list_append(loadClusters, :newLoadCluster),lastUpdate = :updateTime",
ExpressionAttributeValues: {
":newLoadCluster": null,
":updateTime": {
S: '' + common.readableTime(common.now())
}
}
};
/* configuration of question prompts and config assignment */
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var qs = [];
q_region = function (callback) {
rl.question('Enter the Region for the Configuration > ', function (answer) {
if (common.blank(answer) !== null) {
common.validateArrayContains(["ap-northeast-1", "ap-southeast-1", "ap-southeast-2", "eu-central-1", "eu-west-1", "sa-east-1", "us-east-1", "us-west-1", "us-west-2"], answer
.toLowerCase(), rl);
setRegion = answer.toLowerCase();
// configure dynamo db and kms to use this region
dynamoDB = new aws.DynamoDB({
apiVersion: '2012-08-10',
region: setRegion
});
kmsCrypto.setRegion(setRegion);
callback(null);
}
});
};
q_s3Prefix = function (callback) {
rl.question('Enter the Configuration S3 Bucket & Prefix > ', function (answer) {
common.validateNotNull(answer, 'You Must Provide an S3 Bucket Name, and optionally a Prefix', rl);
// setup prefix to be * if one was not provided
var stripped = answer.replace(new RegExp('s3://', 'g'), '');
var elements = stripped.split("/");
var setPrefix = undefined;
if (elements.length === 1) {
// bucket only so use "bucket" alone
setPrefix = elements[0];
} else {
// right trim "/"
setPrefix = stripped.replace(/\/$/, '');
}
// set the s3 prefix in the update request object
updateRequest.Key.s3Prefix = {
S: setPrefix
};
callback(null);
});
};
q_clusterEndpoint = function (callback) {
rl.question('Enter the Cluster Endpoint > ', function (answer) {
common.validateNotNull(answer, 'You Must Provide a Cluster Endpoint', rl);
clusterConfig.M.clusterEndpoint = {
S: answer
};
callback(null);
});
};
q_clusterPort = function (callback) {
rl.question('Enter the Cluster Port > ', function (answer) {
clusterConfig.M.clusterPort = {
N: '' + common.getIntValue(answer, rl)
};
callback(null);
});
};
q_clusterUseSSL = function (callback) {
rl.question('Does your cluster use SSL (Y/N) > ', function (answer) {
clusterConfig.M.useSSL = {
BOOL: common.getBooleanValue(answer)
};
callback(null);
});
};
q_clusterDB = function (callback) {
rl.question('Enter the Database Name > ', function (answer) {
if (common.blank(answer) !== null) {
clusterConfig.M.clusterDB = {
S: answer
};
callback(null);
}
});
};
q_userName = function (callback) {
rl.question('Enter the Database Username > ', function (answer) {
common.validateNotNull(answer, 'You Must Provide a Username', rl);
clusterConfig.M.connectUser = {
S: answer
};
callback(null);
});
};
q_userPwd = function (callback) {
rl.question('Enter the Database Password > ', function (answer) {
common.validateNotNull(answer, 'You Must Provide a Password', rl);
kmsCrypto.encrypt(answer, function (err, ciphertext) {
clusterConfig.M.connectPassword = {
S: kmsCrypto.toLambdaStringFormat(ciphertext)
};
callback(null);
});
});
};
q_table = function (callback) {
rl.question('Enter the Table to be Loaded > ', function (answer) {
common.validateNotNull(answer, 'You Must Provide a Table Name', rl);
clusterConfig.M.targetTable = {
S: answer
};
callback(null);
});
};
q_truncateTable = function (callback) {
rl.question('Should the Table be Truncated before Load? (Y/N) > ', function (answer) {
clusterConfig.M.truncateTarget = {
BOOL: common.getBooleanValue(answer)
};
callback(null);
});
};
last = function (callback) {
rl.close();
addClusterToPrefix(callback);
};
addClusterToPrefix = function (callback, overrideConfig) {
var useConfig = clusterConfig;
if (overrideConfig) {
useConfig = overrideConfig;
}
// add the Expression Attribute Value for the new config section to the
// request as a list of 1 Map item
updateRequest.ExpressionAttributeValues[":newLoadCluster"] = {
"L": [useConfig]
};
// update the configuration
common.retryableUpdate(dynamoDB, updateRequest, callback);
};
exports.addClusterToPrefix = addClusterToPrefix;
// setup the question list for async
qs.push(q_region);
qs.push(q_s3Prefix);
qs.push(q_clusterEndpoint);
qs.push(q_clusterPort);
qs.push(q_clusterUseSSL);
qs.push(q_clusterDB);
qs.push(q_table);
qs.push(q_truncateTable);
qs.push(q_userName);
qs.push(q_userPwd);
// always have to have the 'last' function added to halt the readline channel
// and run the setup
qs.push(last);
// call the first function in the function list, to invoke the callback
// reference chain
async.waterfall(qs);