Skip to content

Commit 338d665

Browse files
authored
fix(scf): trigger key compare (#202)
* fix(scf): trigger key compare * test(scf): remove compared property
1 parent b7d5344 commit 338d665

File tree

9 files changed

+98
-380
lines changed

9 files changed

+98
-380
lines changed

src/index.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ export { default as Cdn } from './modules/cdn';
33
export { default as Cns } from './modules/cns';
44
export { default as Cos } from './modules/cos';
55
export { default as Domain } from './modules/domain';
6-
export { default as MultiApigw } from './modules/multi-apigw';
7-
export { default as MultiScf } from './modules/multi-scf';
86
export { default as Scf } from './modules/scf';
97
export { default as Tag } from './modules/tag';
108
export { default as Postgresql } from './modules/postgresql';
@@ -15,23 +13,3 @@ export { default as Layer } from './modules/layer';
1513
export { default as Cfs } from './modules/cfs';
1614
export { default as Cynosdb } from './modules/cynosdb';
1715
export { default as Cls } from './modules/cls';
18-
19-
// module.exports = {
20-
// // Apigw: require('./modules/apigw'),
21-
// // Cdn: require('./modules/cdn'),
22-
// // Cns: require('./modules/cns'),
23-
// // Cos: require('./modules/cos'),
24-
// // Domain: require('./modules/domain'),
25-
// // MultiApigw: require('./modules/multi-apigw'),
26-
// // MultiScf: require('./modules/multi-scf'),
27-
// // Scf: require('./modules/scf'),
28-
// // Tag: require('./modules/tag'),
29-
// // Postgresql: require('./modules/postgresql'),
30-
// // Vpc: require('./modules/vpc'),
31-
// // Cam: require('./modules/cam'),
32-
// // Metrics: require('./modules/metrics'),
33-
// // Layer: require('./modules/layer'),
34-
// // Cfs: require('./modules/cfs'),
35-
// // Cynosdb: require('./modules/cynosdb'),
36-
// // Cls: require('./modules/cls'),
37-
// };

src/modules/apigw/entities/api.ts

Lines changed: 70 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { Capi } from '@tencent-sdk/capi';
22
import {
3+
UpdateApiInputs,
34
ApiDeployInputs,
45
ApiDeployOutputs,
56
CreateOrUpdateApiInputs,
67
ApiRemoveInputs,
78
ApiBulkRemoveInputs,
89
ApiBulkDeployInputs,
10+
ApiDetail,
911
} from '../interface';
1012
import { pascalCaseProps } from '../../../utils';
1113
import { ApiTypeError } from '../../../utils/error';
@@ -39,7 +41,7 @@ export default class ApiEntity {
3941
return true;
4042
}
4143

42-
async create({ serviceId, endpoint, environment, created }: CreateOrUpdateApiInputs) {
44+
async create({ serviceId, endpoint, environment }: CreateOrUpdateApiInputs) {
4345
// compatibility for secret auth config depends on auth & usagePlan
4446
const authType = endpoint?.auth ? 'SECRET' : endpoint?.authType ?? 'NONE';
4547
const businessType = endpoint?.businessType ?? 'NORMAL';
@@ -90,70 +92,31 @@ export default class ApiEntity {
9092

9193
this.formatInput(endpoint, apiInputs);
9294

93-
let apiDetail: {
94-
ApiId?: string;
95-
InternalDomain?: string;
96-
};
95+
const res = await this.request({
96+
Action: 'CreateApi',
97+
...apiInputs,
98+
});
99+
const { ApiId } = res;
100+
output.apiId = ApiId;
97101

98-
if (endpoint?.apiId) {
99-
apiDetail = await this.getById({ serviceId: serviceId!, apiId: endpoint.apiId });
100-
}
102+
console.log(`API ${ApiId} created`);
103+
const apiDetail: ApiDetail = await this.request({
104+
Action: 'DescribeApi',
105+
serviceId: serviceId,
106+
apiId: output.apiId,
107+
});
108+
output.internalDomain = apiDetail.InternalDomain || '';
101109

102-
if (!apiDetail!) {
103-
apiDetail = await this.getByPathAndMethod({
104-
serviceId: serviceId!,
105-
path: endpoint?.path!,
106-
method: endpoint?.method!,
107-
});
110+
if (endpoint?.isBase64Encoded && endpoint.isBase64Trigger) {
111+
apiInputs.isBase64Trigger = endpoint.isBase64Trigger;
112+
apiInputs.base64EncodedTriggerRules = endpoint.base64EncodedTriggerRules;
108113
}
109114

110-
if (apiDetail && endpoint) {
111-
console.log(`Api method ${endpoint?.method}, path ${endpoint?.path} already exist`);
112-
endpoint.apiId = apiDetail.ApiId;
113-
114-
if (endpoint.isBase64Encoded && endpoint.isBase64Trigger) {
115-
apiInputs.isBase64Trigger = endpoint.isBase64Trigger;
116-
apiInputs.base64EncodedTriggerRules = endpoint.base64EncodedTriggerRules;
117-
}
118-
119-
await this.request({
120-
Action: 'ModifyApi',
121-
apiId: endpoint.apiId,
122-
...apiInputs,
123-
});
124-
125-
output.apiId = endpoint.apiId;
126-
output.created = !!created;
127-
output.internalDomain = apiDetail.InternalDomain || '';
128-
console.log(`Api ${output.apiId} updated`);
129-
} else {
130-
const res = await this.request({
131-
Action: 'CreateApi',
132-
...apiInputs,
133-
});
134-
const { ApiId } = res;
135-
output.apiId = ApiId;
136-
output.created = true;
137-
138-
console.log(`API ${ApiId} created`);
139-
apiDetail = await this.request({
140-
Action: 'DescribeApi',
141-
serviceId: serviceId,
142-
apiId: output.apiId,
143-
});
144-
output.internalDomain = apiDetail.InternalDomain || '';
145-
146-
if (endpoint?.isBase64Encoded && endpoint.isBase64Trigger) {
147-
apiInputs.isBase64Trigger = endpoint.isBase64Trigger;
148-
apiInputs.base64EncodedTriggerRules = endpoint.base64EncodedTriggerRules;
149-
}
150-
151-
await this.request({
152-
Action: 'ModifyApi',
153-
apiId: ApiId,
154-
...apiInputs,
155-
});
156-
}
115+
await this.request({
116+
Action: 'ModifyApi',
117+
apiId: ApiId,
118+
...apiInputs,
119+
});
157120

158121
output.apiName = apiInputs.apiName;
159122

@@ -172,15 +135,18 @@ export default class ApiEntity {
172135
return output;
173136
}
174137

175-
async update({ serviceId, endpoint, environment, created }: CreateOrUpdateApiInputs) {
138+
async update(
139+
{ serviceId, endpoint, environment, created }: UpdateApiInputs,
140+
apiDetail: ApiDetail,
141+
) {
176142
// compatibility for secret auth config depends on auth & usagePlan
177143
const authType = endpoint?.auth ? 'SECRET' : endpoint?.authType ?? 'NONE';
178144
const businessType = endpoint?.businessType ?? 'NORMAL';
179145
const output: ApiDeployOutputs = {
180146
path: endpoint?.path,
181147
method: endpoint?.method,
182148
apiName: endpoint?.apiName || 'index',
183-
created: true,
149+
created: false,
184150
authType: authType,
185151
businessType: businessType,
186152
isBase64Encoded: endpoint?.isBase64Encoded === true,
@@ -223,70 +189,24 @@ export default class ApiEntity {
223189

224190
this.formatInput(endpoint, apiInputs);
225191

226-
let apiDetail: {
227-
ApiId?: string;
228-
InternalDomain?: string;
229-
};
230-
231-
if (endpoint?.apiId) {
232-
apiDetail = await this.getById({ serviceId: serviceId!, apiId: endpoint.apiId });
233-
}
192+
console.log(`Api method ${endpoint?.method}, path ${endpoint?.path} already exist`);
193+
endpoint.apiId = apiDetail.ApiId;
234194

235-
if (!apiDetail!) {
236-
apiDetail = await this.getByPathAndMethod({
237-
serviceId: serviceId!,
238-
path: endpoint?.path!,
239-
method: endpoint?.method!,
240-
});
195+
if (endpoint.isBase64Encoded && endpoint.isBase64Trigger) {
196+
apiInputs.isBase64Trigger = endpoint.isBase64Trigger;
197+
apiInputs.base64EncodedTriggerRules = endpoint.base64EncodedTriggerRules;
241198
}
242199

243-
if (apiDetail && endpoint) {
244-
console.log(`Api method ${endpoint?.method}, path ${endpoint?.path} already exist`);
245-
endpoint.apiId = apiDetail.ApiId;
246-
247-
if (endpoint.isBase64Encoded && endpoint.isBase64Trigger) {
248-
apiInputs.isBase64Trigger = endpoint.isBase64Trigger;
249-
apiInputs.base64EncodedTriggerRules = endpoint.base64EncodedTriggerRules;
250-
}
251-
252-
await this.request({
253-
Action: 'ModifyApi',
254-
apiId: endpoint.apiId,
255-
...apiInputs,
256-
});
257-
258-
output.apiId = endpoint.apiId;
259-
output.created = !!created;
260-
output.internalDomain = apiDetail.InternalDomain || '';
261-
console.log(`Api ${output.apiId} updated`);
262-
} else {
263-
const res = await this.request({
264-
Action: 'CreateApi',
265-
...apiInputs,
266-
});
267-
const { ApiId } = res;
268-
output.apiId = ApiId;
269-
output.created = true;
270-
271-
console.log(`API ${ApiId} created`);
272-
apiDetail = await this.request({
273-
Action: 'DescribeApi',
274-
serviceId: serviceId,
275-
apiId: output.apiId,
276-
});
277-
output.internalDomain = apiDetail.InternalDomain || '';
278-
279-
if (endpoint?.isBase64Encoded && endpoint.isBase64Trigger) {
280-
apiInputs.isBase64Trigger = endpoint.isBase64Trigger;
281-
apiInputs.base64EncodedTriggerRules = endpoint.base64EncodedTriggerRules;
282-
}
200+
await this.request({
201+
Action: 'ModifyApi',
202+
apiId: endpoint.apiId,
203+
...apiInputs,
204+
});
283205

284-
await this.request({
285-
Action: 'ModifyApi',
286-
apiId: ApiId,
287-
...apiInputs,
288-
});
289-
}
206+
output.apiId = endpoint.apiId;
207+
output.created = !!created;
208+
output.internalDomain = apiDetail.InternalDomain || '';
209+
console.log(`Api ${output.apiId} updated`);
290210

291211
output.apiName = apiInputs.apiName;
292212

@@ -387,13 +307,35 @@ export default class ApiEntity {
387307
}
388308

389309
let curApi;
310+
let apiDetail: ApiDetail | null = null;
311+
let apiExist = false;
390312
if (apiConfig.apiId) {
391-
curApi = await this.update({
392-
serviceId,
393-
environment,
394-
endpoint: apiConfig,
395-
created: exist && exist.created,
313+
apiDetail = await this.getById({ serviceId: serviceId!, apiId: apiConfig.apiId });
314+
}
315+
316+
if (!apiDetail) {
317+
apiDetail = await this.getByPathAndMethod({
318+
serviceId: serviceId!,
319+
path: apiConfig?.path!,
320+
method: apiConfig?.method!,
396321
});
322+
}
323+
324+
if (apiDetail) {
325+
apiExist = true;
326+
}
327+
328+
// api 存在就更新,不存在就创建
329+
if (apiExist) {
330+
curApi = await this.update(
331+
{
332+
serviceId,
333+
environment,
334+
endpoint: apiConfig,
335+
created: exist && exist.created,
336+
},
337+
apiDetail,
338+
);
397339
} else {
398340
curApi = await this.create({
399341
serviceId,

src/modules/apigw/interface.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ export interface CreateOrUpdateApiInputs {
142142
created?: boolean;
143143
}
144144

145+
export interface UpdateApiInputs {
146+
serviceId: string;
147+
endpoint: ApiEndpoint;
148+
environment: EnviromentType;
149+
created?: boolean;
150+
}
151+
145152
export interface ApiDeployInputs {
146153
serviceId: string;
147154
environment: EnviromentType;
@@ -236,3 +243,8 @@ export interface ApigwRemoveInputs {
236243
isInputServiceId?: boolean;
237244
isRemoveTrigger?: boolean;
238245
}
246+
247+
export interface ApiDetail {
248+
ApiId: string;
249+
InternalDomain: string;
250+
}

0 commit comments

Comments
 (0)