-
Notifications
You must be signed in to change notification settings - Fork 12
/
create.ts
281 lines (242 loc) · 7.66 KB
/
create.ts
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import ora from 'ora';
import inquirer from 'inquirer';
import { ux } from '@oclif/core';
import { Flags } from '@oclif/core';
import Command from '../../base.js';
import { createDebugLogger } from '../../utils/output.js';
export default class Create extends Command {
static description = 'create a new database';
static PATH = 'v1/databases';
static flags = {
...Command.flags,
name: Flags.string({
char: 'n',
description: 'name of your database',
}),
plan: Flags.string({
description: 'plan',
}),
'public-network': Flags.boolean({
description: 'use public network or not',
}),
type: Flags.string({
char: 't',
description: 'choose which database to use',
}),
version: Flags.string({
char: 'v',
description: 'version of the database',
}),
yes: Flags.boolean({
char: 'y',
description: 'say yes to continue prompt',
}),
network: Flags.string({
description: 'network',
}),
};
async run(): Promise<void> {
this.spinner = ora();
const { flags } = await this.parse(Create);
const debug = createDebugLogger(flags.debug);
await this.setGotConfig(flags);
const account = await this.getCurrentAccount();
((account && account.region === 'germany') || flags.region === 'germany') &&
this.error('We do not support germany any more.');
const hostname = flags.name || (await this.promptHostname());
const type = flags.type || (await this.promptType());
const version = flags.version || (await this.promptVersion(type));
const network = flags.network
? await this.getNetwork(flags.network)
: await this.promptNetwork();
const publicNetwork =
flags['public-network'] || (await this.promptPublicNetwork()) === 'y';
const planID = flags.plan || (await this.promptPlan(type));
const sayYes = flags.yes;
try {
const tableData = [
{
type,
version,
hostname,
network: network?.name,
publicNetwork: publicNetwork.toString(),
},
];
const tableConfig = {
Hostname: { get: (row: any) => row.hostname },
Type: { get: (row: any) => row.type },
Version: { get: (row: any) => row.version },
Network: { get: (row: any) => row.network },
'Public Network': { get: (row: any) => row.publicNetwork },
};
ux.table(tableData, tableConfig, {
title: 'Database Specification',
});
if (!sayYes && (await this.promptContinue()) === 'n') {
this.log('Operation cancelled');
return;
}
await this.got.post(Create.PATH, {
json: {
hostname,
planID,
publicNetwork,
type,
version,
network: network?._id,
},
});
this.log(`Database ${hostname} created.`);
} catch (error) {
debug(error.message);
if (error.response && error.response.body) {
debug(JSON.stringify(error.response.body));
}
if (error.response && error.response.statusCode === 404) {
this.error(`Could not create the database.`);
}
if (error.response && error.response.statusCode === 409) {
this.error(
`The database already exists. Please use a unique name for your database.`
);
}
if (
error.response &&
error.response.statusCode === 403 &&
error.response.body
) {
const body = JSON.parse(error.response.body);
if (body.data.code === 'free_plan_platform') {
this.error(`The free plan is not available for ${type} database.`);
}
if (body.data.code === 'free_plan_count') {
this.error(
`You are allowed to create only one database on the free plan`
);
}
}
this.error(`Could not create the database. Please try again.`);
}
}
async promptHostname() {
const { name } = (await inquirer.prompt({
name: 'name',
type: 'input',
message: 'Enter name:',
validate: (input) => input.length > 2,
})) as { name: string };
return name;
}
async promptPlan(databaseType: string) {
this.spinner.start('Loading...');
try {
const { plans } = await this.got('v1/me').json<{ plans: any }>();
this.spinner.stop();
const { plan } = (await inquirer.prompt({
name: 'plan',
type: 'list',
message: 'Please select a plan:',
choices: [
...Object.keys(plans.databases)
.filter((plan) => {
if (
(plan === 'free' || plan.includes('g2')) &&
plans.databases[plan].available &&
plans.databases[plan].supports.includes(databaseType)
) {
return true;
}
})
.map((plan) => {
const availablePlan = plans.databases[plan];
const ram = availablePlan.RAM.amount;
const cpu = availablePlan.CPU.amount;
const disk = availablePlan.volume;
const price = availablePlan.price * 720;
const storageClass = availablePlan.storageClass;
return {
value: plan,
name: `RAM: ${ram}${' '.repeat(
5 - ram.toString().length
)} GB, CPU: ${cpu}${' '.repeat(
6 - cpu.toString().length
)}Core, Disk: ${disk}${
' '.repeat(5 - disk.toString().length) + 'GB'
}${storageClass || 'SSD'}, Price: ${price.toLocaleString()}${
price
? ' '.repeat(7 - Math.floor(price).toString().length) +
'Tomans/Month'
: ''
}`,
};
}),
],
})) as { plan: string };
return plan;
} catch (error) {
this.spinner.stop();
throw error;
}
}
async promptPublicNetwork() {
const { is_public } = (await inquirer.prompt({
name: 'is_public',
type: 'input',
message: 'use public network?(y/n):',
validate: (input) => input === 'y' || input === 'n',
})) as { is_public: string };
return is_public;
}
async promptType() {
this.spinner.start('Loading...');
try {
const { databaseVersions } = await this.got('v1/me').json<{
databaseVersions: any;
}>();
this.spinner.stop();
const { databaseType } = (await inquirer.prompt({
name: 'databaseType',
type: 'list',
message: 'Please select a database type:',
choices: [...Object.keys(databaseVersions)],
})) as { databaseType: string };
return databaseType;
} catch (error) {
this.spinner.stop();
throw error;
}
}
async promptVersion(type: string) {
this.spinner.start('Loading...');
try {
const { databaseVersions } = await this.got('v1/me').json<{
databaseVersions: any;
}>();
this.spinner.stop();
const { databaseVersion } = (await inquirer.prompt({
name: 'databaseVersion',
type: 'list',
message: 'Please select a version:',
choices: [
...databaseVersions[type].map(
(obj: { label: string; value: string }) => obj.value
),
],
})) as { databaseVersion: string };
return databaseVersion;
} catch (error) {
this.spinner.stop();
throw error;
}
}
async promptContinue() {
const { yes } = (await inquirer.prompt({
name: 'yes',
type: 'input',
message: 'continue? (y/n):',
validate: (input) => input === 'y' || input === 'n',
})) as { yes: string };
return yes;
}
}