Skip to content

Commit 5f2dd82

Browse files
authored
fix(postgresql): use instance id for uniqure handle id (#189)
* fix(postgresql): use instance id for uniqure handle id * test: update scf * test(cns): remove unuse log
1 parent 548db43 commit 5f2dd82

File tree

6 files changed

+89
-33
lines changed

6 files changed

+89
-33
lines changed

__tests__/pg.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe('Postgresql', () => {
4141
dbname: expect.stringContaining('tencentdb_'),
4242
},
4343
});
44+
inputs.dBInstanceId = res.dBInstanceId;
4445
});
4546
test('should enable public access for postgresql success', async () => {
4647
inputs.extranetAccess = true;

__tests__/scf.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ describe('Scf', () => {
7575
};
7676

7777
const inputs: ScfDeployInputs = {
78-
// name: `serverless-test-${Date.now()}`,
79-
name: `serverless-test-fixed`,
78+
name: `serverless-test-${Date.now()}`,
8079
code: {
8180
bucket: process.env.BUCKET,
8281
object: 'express_code.zip',
@@ -174,7 +173,7 @@ describe('Scf', () => {
174173
},
175174
Handler: inputs.handler,
176175
AsyncRunEnable: 'FALSE',
177-
LogType: 'normal',
176+
LogType: expect.any(String),
178177
TraceEnable: 'FALSE',
179178
UseGpu: 'FALSE',
180179
Role: inputs.role,
@@ -211,8 +210,8 @@ describe('Scf', () => {
211210
EipConfig: { EipStatus: 'ENABLE', EipAddress: expect.any(Array) },
212211
},
213212
Triggers: expect.any(Array),
214-
ClsLogsetId: '',
215-
ClsTopicId: '',
213+
ClsLogsetId: expect.any(String),
214+
ClsTopicId: expect.any(String),
216215
CodeInfo: '',
217216
CodeResult: 'success',
218217
CodeError: '',

src/modules/cns/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ export default class Cns {
148148
recordId: tempInputs.recordId,
149149
status: tempInputs.status,
150150
};
151-
console.log(statusInputs);
152151
await this.request(statusInputs);
153152
console.log(`Modified status to ${tempInputs.status}`);
154153
}

src/modules/postgresql/index.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export default class Postgresql {
3838
region,
3939
zone,
4040
projectId,
41+
dBInstanceId,
4142
dBInstanceName,
4243
dBVersion,
4344
dBCharset,
@@ -52,21 +53,30 @@ export default class Postgresql {
5253
dBInstanceName: dBInstanceName,
5354
};
5455

55-
let dbDetail = await getDbInstanceDetail(this.capi, dBInstanceName!);
56+
let dbDetail;
57+
if (dBInstanceId) {
58+
dbDetail = await getDbInstanceDetail(this.capi, dBInstanceId!);
59+
}
5660

57-
if (dbDetail && dbDetail.DBInstanceName && dbDetail.Zone === zone) {
61+
if (dbDetail && dbDetail.DBInstanceId && dbDetail.Zone === zone) {
5862
const publicAccess = getDbExtranetAccess(dbDetail.DBInstanceNetInfo);
5963
// exist and public access config different, update db instance
6064
if (publicAccess !== extranetAccess) {
61-
console.log(`DB instance ${dBInstanceName} existed, updating`);
65+
console.log(`DB instance id ${dbDetail.DBInstanceId} existed, updating`);
6266
// do not throw error when open public access
6367
try {
64-
dbDetail = await toggleDbInstanceAccess(this.capi, dBInstanceName!, extranetAccess!);
68+
dbDetail = await toggleDbInstanceAccess(
69+
this.capi,
70+
dbDetail.DBInstanceId!,
71+
extranetAccess!,
72+
);
6573
} catch (e) {
66-
console.log(`Toggle DB Instane access failed, ${e.message}, ${e.reqId}`);
74+
console.log(
75+
`Toggle db instane ${dbDetail.DBInstanceId} access failed, ${e.message}, ${e.reqId}`,
76+
);
6777
}
6878
} else {
69-
console.log(`DB instance ${dBInstanceName} existed.`);
79+
console.log(`DB instance id ${dbDetail.DBInstanceId} existed.`);
7080
}
7181
} else {
7282
// not exist, create
@@ -82,7 +92,7 @@ export default class Postgresql {
8292

8393
dbDetail = await createDbInstance(this.capi, postgresInputs);
8494
if (extranetAccess) {
85-
dbDetail = await toggleDbInstanceAccess(this.capi, dBInstanceName!, extranetAccess);
95+
dbDetail = await toggleDbInstanceAccess(this.capi, dbDetail.DBInstanceId!, extranetAccess);
8696
}
8797
}
8898
outputs.dBInstanceId = dbDetail.DBInstanceId;
@@ -117,12 +127,12 @@ export default class Postgresql {
117127

118128
/** 移除 postgresql 实例 */
119129
async remove(inputs: PostgresqlRemoveInputs = {}) {
120-
const { dBInstanceName } = inputs;
130+
const { dBInstanceId } = inputs;
121131

122-
const dbDetail = await getDbInstanceDetail(this.capi, dBInstanceName!);
132+
const dbDetail = await getDbInstanceDetail(this.capi, dBInstanceId!);
123133
if (dbDetail && dbDetail.DBInstanceName) {
124134
// need circle for deleting, after host status is 6, then we can delete it
125-
await deleteDbInstance(this.capi, dBInstanceName!);
135+
await deleteDbInstance(this.capi, dBInstanceId!);
126136
}
127137
return {};
128138
}

src/modules/postgresql/interface.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface PostgresqlDeployInputs {
55
zone?: string;
66
projectId?: number;
77
dBInstanceName?: string;
8+
dBInstanceId?: string;
89
dBVersion?: string;
910
dBCharset?: string;
1011
extranetAccess?: boolean;
@@ -32,4 +33,35 @@ export interface PostgresqlDeployOutputs {
3233

3334
export interface PostgresqlRemoveInputs {
3435
dBInstanceName?: string;
36+
dBInstanceId?: string;
37+
}
38+
39+
export interface PostgresqlInstanceNetInfo {
40+
Address: string;
41+
Ip: string;
42+
NetType: string;
43+
Port: number;
44+
Status: string;
45+
}
46+
47+
export interface PostgresqlInstanceDetail {
48+
CreateTime: string;
49+
DBAccountSet: {
50+
DBConnLimit: number;
51+
DBPassword: string;
52+
DBUser: string;
53+
}[];
54+
DBCharset: string;
55+
DBDatabaseList: string[];
56+
DBInstanceId: string;
57+
DBInstanceName: string;
58+
DBInstanceNetInfo: PostgresqlInstanceNetInfo[];
59+
DBInstanceStatus: string;
60+
DBVersion: string;
61+
ProjectId: number;
62+
Region: string;
63+
SubnetId: string;
64+
TagList: any[];
65+
VpcId: string;
66+
Zone: string;
3567
}

src/modules/postgresql/utils.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Capi } from '@tencent-sdk/capi';
22
import { waitResponse } from '@ygkit/request';
33
import APIS from './apis';
4+
import { PostgresqlInstanceDetail, PostgresqlInstanceNetInfo } from './interface';
45

56
// timeout 5 minutes
67
const TIMEOUT = 5 * 60 * 1000;
@@ -10,14 +11,17 @@ const TIMEOUT = 5 * 60 * 1000;
1011
* @param {object} capi capi instance
1112
* @param {*} dBInstanceName
1213
*/
13-
export async function getDbInstanceDetail(capi: Capi, dBInstanceName: string) {
14+
export async function getDbInstanceDetail(
15+
capi: Capi,
16+
dBInstanceId: string,
17+
): Promise<PostgresqlInstanceDetail | undefined> {
1418
// get instance detail
1519
try {
1620
const res = await APIS.DescribeServerlessDBInstances(capi, {
1721
Filter: [
1822
{
19-
Name: 'db-instance-name',
20-
Values: [dBInstanceName],
23+
Name: 'db-instance-id',
24+
Values: [dBInstanceId],
2125
},
2226
],
2327
});
@@ -47,6 +51,17 @@ export function getDbExtranetAccess(netInfos: { NetType: string; Status: string
4751
return result;
4852
}
4953

54+
export function isEnablePublicAccess(detail: PostgresqlInstanceDetail) {
55+
let enable = false;
56+
const { DBInstanceNetInfo } = detail;
57+
DBInstanceNetInfo.forEach((item: PostgresqlInstanceNetInfo) => {
58+
if (item.NetType === 'public' && item.Status === 'opened') {
59+
enable = true;
60+
}
61+
});
62+
return enable;
63+
}
64+
5065
/**
5166
INSTANCE_STATUS_APPLYING: "applying", 申请中
5267
INSTANCE_STATUS_INIT: "init", 待初始化
@@ -72,16 +87,16 @@ export function getDbExtranetAccess(netInfos: { NetType: string; Status: string
7287
*/
7388
export async function toggleDbInstanceAccess(
7489
capi: Capi,
75-
dBInstanceName: string,
90+
DBInstanceId: string,
7691
extranetAccess: boolean,
77-
) {
92+
): Promise<PostgresqlInstanceDetail> {
7893
if (extranetAccess) {
7994
console.log(`Start open db extranet access...`);
8095
await APIS.OpenServerlessDBExtranetAccess(capi, {
81-
DBInstanceName: dBInstanceName,
96+
DBInstanceId: DBInstanceId,
8297
});
8398
const detail = await waitResponse({
84-
callback: async () => getDbInstanceDetail(capi, dBInstanceName),
99+
callback: async () => getDbInstanceDetail(capi, DBInstanceId),
85100
targetResponse: 'running',
86101
targetProp: 'DBInstanceStatus',
87102
timeout: TIMEOUT,
@@ -91,10 +106,10 @@ export async function toggleDbInstanceAccess(
91106
}
92107
console.log(`Start close db extranet access`);
93108
await APIS.CloseServerlessDBExtranetAccess(capi, {
94-
DBInstanceName: dBInstanceName,
109+
DBInstanceId: DBInstanceId,
95110
});
96111
const detail = await waitResponse({
97-
callback: async () => getDbInstanceDetail(capi, dBInstanceName),
112+
callback: async () => getDbInstanceDetail(capi, DBInstanceId),
98113
targetResponse: 'running',
99114
targetProp: 'DBInstanceStatus',
100115
timeout: TIMEOUT,
@@ -122,15 +137,15 @@ export async function createDbInstance(
122137
) {
123138
console.log(`Start create DB instance ${postgresInputs.DBInstanceName}`);
124139
const { DBInstanceId } = await APIS.CreateServerlessDBInstance(capi, postgresInputs);
125-
console.log(`Creating DB instance ID: ${DBInstanceId}`);
140+
console.log(`Creating db instance id: ${DBInstanceId}`);
126141

127142
const detail = await waitResponse({
128-
callback: async () => getDbInstanceDetail(capi, postgresInputs.DBInstanceName),
143+
callback: async () => getDbInstanceDetail(capi, DBInstanceId),
129144
targetResponse: 'running',
130145
targetProp: 'DBInstanceStatus',
131146
timeout: TIMEOUT,
132147
});
133-
console.log(`Created DB instance name ${postgresInputs.DBInstanceName} successfully`);
148+
console.log(`Created db instance id ${DBInstanceId} success`);
134149
return detail;
135150
}
136151

@@ -139,17 +154,17 @@ export async function createDbInstance(
139154
* @param {object} capi capi client
140155
* @param {string} db instance name
141156
*/
142-
export async function deleteDbInstance(capi: Capi, dBInstanceName: string) {
143-
console.log(`Start removing postgres instance ${dBInstanceName}`);
157+
export async function deleteDbInstance(capi: Capi, DBInstanceId: string) {
158+
console.log(`Start removing postgres instance id ${DBInstanceId}`);
144159
await APIS.DeleteServerlessDBInstance(capi, {
145-
DBInstanceName: dBInstanceName,
160+
DBInstanceId,
146161
});
147162
const detail = await waitResponse({
148-
callback: async () => getDbInstanceDetail(capi, dBInstanceName),
163+
callback: async () => getDbInstanceDetail(capi, DBInstanceId),
149164
targetResponse: undefined,
150165
timeout: TIMEOUT,
151166
});
152-
console.log(`Removed postgres instance ${dBInstanceName} successfully`);
167+
console.log(`Removed postgres instance id ${DBInstanceId} successfully`);
153168
return detail;
154169
}
155170

0 commit comments

Comments
 (0)