-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
192 lines (170 loc) · 4.74 KB
/
index.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
import { got } from "got";
import { exec as _exec } from "child_process";
import { promisify } from "util";
import inquirer from "inquirer";
import fs from "fs";
import inquirerSearchList from "inquirer-search-list";
import ora from "ora";
const exec = promisify(_exec);
inquirer.registerPrompt("search-list", inquirerSearchList);
const getToken = async () => {
const spinner = ora("Getting access token").start();
const { stdout } = await exec("gcloud auth print-access-token");
const token = stdout?.replace("\n", "");
if (!token) {
spinner.fail("Configure gcloud");
return null;
}
spinner.succeed();
return token;
};
const getNewIp = async () => {
const spinner = ora("Getting current IP address").start();
const newIp = await got.get("https://api.ipify.org");
if (!newIp?.body) {
spinner.fail();
return null;
}
spinner.succeed();
return `${newIp.body}/32`;
};
const getConfig = async () => {
let projectId = null;
let instanceId = null;
let config = null;
try {
config = JSON.parse(fs.readFileSync("./config.json", "utf8"));
projectId = config.projectId;
instanceId = config.instanceId;
console.log("PROJECT ID: ", projectId);
console.log("INSTANCE ID: ", instanceId);
const { keepConfig } = await inquirer.prompt([
{
name: "keepConfig",
type: "confirm",
message: "Do you want to use this config?",
},
]);
if (!keepConfig) throw new Error();
} catch (error) {
const answers = await inquirer.prompt([
{
name: "projectId",
type: "input",
message: "Enter project id: ",
},
{
name: "instanceId",
type: "input",
message: "Enter instance id: ",
},
]);
projectId = answers.projectId;
instanceId = answers.instanceId;
fs.writeFileSync(
"./config.json",
JSON.stringify({
...config,
projectId,
instanceId,
})
);
}
return { ...config, projectId, instanceId };
};
const getCurrentIps = async (url, headers) => {
const spinner = ora("Getting current whitelisted IP address").start();
const currentSettings = await got.get(`${url}?fields=settings`, {
headers,
});
const currentIps = JSON.parse(currentSettings?.body)?.settings
?.ipConfiguration?.authorizedNetworks;
if (!currentIps) {
spinner.fail();
return null;
}
spinner.succeed();
return currentIps;
};
const getName = async (config, currentIps) => {
if (config.name) {
const { keepConfig } = await inquirer.prompt([
{
name: "keepConfig",
type: "confirm",
message: `Do you want to update ${config.name}?`,
},
]);
if (keepConfig) return config.name;
}
const names = currentIps.map((ip) => ip.name);
let { name } = await inquirer.prompt([
{
name: "name",
type: "search-list",
message: "Which IP do you want to update?",
choices: [...names, "NEW VALUE"],
},
]);
if (name === "NEW VALUE") {
const answer = await inquirer.prompt([
{
name: "name",
type: "input",
message: "Enter new name: ",
},
]);
name = answer.name;
}
fs.writeFileSync(
"./config.json",
JSON.stringify({
...config,
name,
})
);
return name;
};
const updateIp = async (url, headers, name, newIp, currentIps) => {
if (currentIps.some((ip) => ip.value === newIp && ip.name === name))
return console.log("\x1b[32m%s\x1b[0m", `${name} IP is already up to date`);
const spinner = ora("Updating whitelisted IP addresses").start();
const newIps = [
...currentIps.filter((ip) => ip.value !== newIp && ip.name !== name),
{ kind: "sql#aclEntry", value: newIp, name },
];
const result = await got.patch(url, {
headers,
body: JSON.stringify({
settings: {
ipConfiguration: {
authorizedNetworks: newIps,
},
},
}),
});
if (!result || result?.statusCode !== 200) {
spinner.fail();
return null;
}
spinner.succeed();
console.log("\x1b[32m%s\x1b[0m", `${name} IP has been successfully updated`);
};
(async () => {
try {
const token = await getToken();
if (!token) return null;
const newIp = await getNewIp();
if (!newIp) return null;
const config = await getConfig();
const headers = { Authorization: `Bearer ${token}` };
const url = `https://sqladmin.googleapis.com/sql/v1beta4/projects/${config.projectId}/instances/${config.instanceId}`;
const currentIps = await getCurrentIps(url, headers);
if (!currentIps) return null;
const name = await getName(config, currentIps);
if (!name || !name.length) return ora("Empty name").fail();
return updateIp(url, headers, name, newIp, currentIps);
} catch (err) {
console.log(err);
}
})();