Skip to content

Commit 53373ee

Browse files
committed
fix(cynosdb): support enable/disable public access
1 parent cfc8b4e commit 53373ee

File tree

4 files changed

+106
-27
lines changed

4 files changed

+106
-27
lines changed

__tests__/cynosdb.test.js

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -105,25 +105,36 @@ describe('Cynosdb', () => {
105105
({ clusterId } = res);
106106
});
107107

108-
test('[SERVERLESS] remove', async () => {
109-
await sleep(300);
110-
const res = await client.remove({ clusterId });
111-
112-
const detail = await getClusterDetail(client.capi, clusterId);
113-
expect(res).toEqual(true);
114-
expect(detail.Status).toBe('isolated');
115-
});
108+
test('[SERVERLESS] should enable public access', async () => {
109+
inputs.clusterId = clusterId;
110+
inputs.enablePublicAccess = true;
116111

117-
test('[SERVERLESS] offline', async () => {
118-
await sleep(300);
119-
const res = await offlineCluster(client.capi, clusterId);
120-
expect(res).toBeUndefined();
112+
const res = await client.deploy(inputs);
113+
expect(res).toEqual({
114+
dbMode: 'SERVERLESS',
115+
region: inputs.region,
116+
zone: inputs.zone,
117+
vpcConfig: inputs.vpcConfig,
118+
instanceCount: 1,
119+
// adminPassword: expect.stringMatching(pwdReg),
120+
clusterId: expect.stringContaining('cynosdbmysql-'),
121+
minCpu: 0.5,
122+
maxCpu: 2,
123+
connection: {
124+
ip: expect.any(String),
125+
port: 3306,
126+
readList: expect.any(Array),
127+
},
128+
publicConnection: {
129+
domain: expect.any(String),
130+
ip: expect.any(String),
131+
port: expect.any(Number),
132+
},
133+
});
121134
});
122135

123-
test('[SERVERLESS with minCpu and maxCpu] deploy', async () => {
124-
inputs.dbMode = 'SERVERLESS';
125-
inputs.minCpu = 2;
126-
inputs.maxCpu = 4;
136+
test('[SERVERLESS] should disable public access', async () => {
137+
inputs.enablePublicAccess = false;
127138

128139
const res = await client.deploy(inputs);
129140
expect(res).toEqual({
@@ -132,21 +143,20 @@ describe('Cynosdb', () => {
132143
zone: inputs.zone,
133144
vpcConfig: inputs.vpcConfig,
134145
instanceCount: 1,
135-
adminPassword: expect.stringMatching(pwdReg),
146+
// adminPassword: expect.stringMatching(pwdReg),
136147
clusterId: expect.stringContaining('cynosdbmysql-'),
137-
minCpu: 2,
138-
maxCpu: 4,
148+
minCpu: 0.5,
149+
maxCpu: 2,
139150
connection: {
140151
ip: expect.any(String),
141152
port: 3306,
142153
readList: expect.any(Array),
143154
},
144155
});
145-
146-
({ clusterId } = res);
156+
inputs.clusterId = undefined;
147157
});
148158

149-
test('[SERVERLESS with minCpu and maxCpu] remove', async () => {
159+
test('[SERVERLESS] remove', async () => {
150160
await sleep(300);
151161
const res = await client.remove({ clusterId });
152162

@@ -155,7 +165,7 @@ describe('Cynosdb', () => {
155165
expect(detail.Status).toBe('isolated');
156166
});
157167

158-
test('[SERVERLESS with minCpu and maxCpu] offline', async () => {
168+
test('[SERVERLESS] offline', async () => {
159169
await sleep(300);
160170
const res = await offlineCluster(client.capi, clusterId);
161171
expect(res).toBeUndefined();

src/modules/cynosdb/apis.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const ACTIONS = [
1212
'ResetAccountPassword',
1313
'DescribeClusters',
1414
'DescribeServerlessInstanceSpecs',
15+
'OpenWan',
16+
'CloseWan',
17+
'DescribeClusterInstanceGrps',
1518
];
1619

1720
const APIS = ApiFactory({

src/modules/cynosdb/index.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const {
66
generatePwd,
77
formatConnectOutput,
88
resetPwd,
9+
openPublicAccess,
10+
closePublicAccess,
911
} = require('./utils');
1012
const { ApiError } = require('../../utils/error');
1113

@@ -46,6 +48,7 @@ class Cynosdb {
4648
maxCpu = 2,
4749
autoPause = 'yes',
4850
autoPauseDelay = 3600, // default 1h
51+
enablePublicAccess,
4952
} = inputs;
5053

5154
const outputs = {
@@ -57,6 +60,12 @@ class Cynosdb {
5760
dbMode,
5861
};
5962

63+
if (dbMode === 'SERVERLESS') {
64+
outputs.minCpu = minCpu;
65+
outputs.maxCpu = maxCpu;
66+
outputs.instanceCount = 1;
67+
}
68+
6069
let isExisted = false;
6170
let clusterDetail = null;
6271
if (clusterId) {
@@ -104,20 +113,29 @@ class Cynosdb {
104113
dbInputs.MaxCpu = maxCpu;
105114
dbInputs.AutoPause = autoPause;
106115
dbInputs.AutoPauseDelay = autoPauseDelay;
107-
108-
outputs.minCpu = minCpu;
109-
outputs.maxCpu = maxCpu;
110-
outputs.instanceCount = 1;
111116
}
112117

113118
clusterDetail = await createCluster(this.capi, dbInputs);
114119
outputs.clusterId = clusterDetail.ClusterId;
115120

116121
outputs.adminPassword = dbInputs.AdminPassword;
122+
} else {
123+
console.log(`Cynosdb cluster ${outputs.clusterId} already exist`);
117124
}
118125

119126
outputs.connection = formatConnectOutput(clusterDetail);
120127

128+
if (enablePublicAccess) {
129+
const wanInfo = await openPublicAccess(this.capi, outputs.clusterId);
130+
outputs.publicConnection = {
131+
domain: wanInfo.WanDomain,
132+
ip: wanInfo.WanIP,
133+
port: wanInfo.WanPort,
134+
};
135+
} else if (enablePublicAccess === false) {
136+
await closePublicAccess(this.capi, outputs.clusterId);
137+
}
138+
121139
return outputs;
122140
}
123141

src/modules/cynosdb/utils.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const {
88
DescribeServerlessInstanceSpecs,
99
OfflineCluster,
1010
DescribeInstances,
11+
OpenWan,
12+
CloseWan,
13+
DescribeClusterInstanceGrps,
1114
} = require('./apis');
1215
const { ApiError } = require('../../utils/error');
1316

@@ -238,6 +241,49 @@ async function resetPwd(capi, inputs) {
238241
return true;
239242
}
240243

244+
async function getClusterGrpsInfo(capi, clusterId) {
245+
const { InstanceGrpInfoList = [] } = await DescribeClusterInstanceGrps(capi, {
246+
ClusterId: clusterId,
247+
});
248+
return InstanceGrpInfoList[0];
249+
}
250+
251+
async function openPublicAccess(capi, clusterId) {
252+
const gprInfo = await getClusterGrpsInfo(capi, clusterId);
253+
254+
console.log(`Start opening public access to cluster ${clusterId}`);
255+
await OpenWan(capi, {
256+
InstanceGrpId: gprInfo.InstanceGrpId,
257+
});
258+
259+
const res = await waitResponse({
260+
callback: async () => getClusterGrpsInfo(capi, clusterId),
261+
targetProp: 'WanStatus',
262+
targetResponse: 'open',
263+
timeout: TIMEOUT,
264+
});
265+
console.log(`Open public access to cluster ${clusterId} success`);
266+
return res;
267+
}
268+
269+
async function closePublicAccess(capi, clusterId) {
270+
const gprInfo = await getClusterGrpsInfo(capi, clusterId);
271+
272+
console.log(`Start closing public access to cluster ${clusterId}`);
273+
await CloseWan(capi, {
274+
InstanceGrpId: gprInfo.InstanceGrpId,
275+
});
276+
277+
const res = await waitResponse({
278+
callback: async () => getClusterGrpsInfo(capi, clusterId),
279+
targetProp: 'WanStatus',
280+
targetResponse: 'closed',
281+
timeout: TIMEOUT,
282+
});
283+
console.log(`Close public access to cluster ${clusterId} success`);
284+
return res;
285+
}
286+
241287
module.exports = {
242288
TIMEOUT,
243289
PWD_CHARS,
@@ -252,4 +298,6 @@ module.exports = {
252298
offlineCluster,
253299
offlineInstance,
254300
getInstanceDetail,
301+
openPublicAccess,
302+
closePublicAccess,
255303
};

0 commit comments

Comments
 (0)