Skip to content

Commit 0bee4df

Browse files
authored
fix(apigw): add remove by trigger for only remove api (#198)
* fix(apigw): get relative id * fix(apigw): add remove by trigger for only remove api * test(metrics): update test function name
1 parent 08c1ab4 commit 0bee4df

File tree

6 files changed

+34
-44
lines changed

6 files changed

+34
-44
lines changed

__tests__/metrics.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('Metrics', () => {
2525
SecretKey: process.env.TENCENT_SECRET_KEY,
2626
};
2727
const metrics = new Metrics(credentials, {
28-
funcName: 'serverless-test',
28+
funcName: 'serverless-unit-test',
2929
});
3030

3131
const rangeStart = '2020-09-09 10:00:00';

src/modules/apigw/entities/api.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,6 @@ export default class ApiEntity {
361361
if (exist) {
362362
apiConfig.apiId = exist.apiId;
363363
apiConfig.created = exist.created;
364-
365-
if (isOauthApi) {
366-
apiConfig.authRelationApiId = exist.authRelationApiId;
367-
}
368364
}
369365
if (isOauthApi && !apiConfig.authRelationApiId) {
370366
// find reletive oauth api

src/modules/apigw/index.ts

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,9 @@ export default class Apigw {
129129
apiList,
130130
customDomains,
131131
usagePlan,
132-
isInputServiceId = false,
132+
isRemoveTrigger = false,
133133
} = inputs;
134134

135-
// 如果用户 yaml 的 inputs 配置了 serviceId,走特殊流程
136-
if (isInputServiceId) {
137-
await this.removeWithInputServiceId(inputs);
138-
return;
139-
}
140-
141135
// check service exist
142136
const detail = await this.request({
143137
Action: 'DescribeService',
@@ -148,6 +142,20 @@ export default class Apigw {
148142
return;
149143
}
150144

145+
// 1. remove all apis
146+
await this.api.bulkRemove({
147+
apiList,
148+
serviceId,
149+
environment,
150+
});
151+
152+
// 定制化需求:如果用户在yaml中配置了 serviceId,则只执行删除 api 逻辑
153+
// 删除后需要重新发布
154+
if (isRemoveTrigger) {
155+
await this.service.release({ serviceId, environment });
156+
return;
157+
}
158+
151159
// remove usage plan
152160
if (usagePlan) {
153161
await this.usagePlan.remove({
@@ -157,13 +165,6 @@ export default class Apigw {
157165
});
158166
}
159167

160-
// 1. remove all apis
161-
await this.api.bulkRemove({
162-
apiList,
163-
serviceId,
164-
environment,
165-
});
166-
167168
// 2. unbind all custom domains
168169
if (customDomains) {
169170
for (let i = 0; i < customDomains.length; i++) {
@@ -231,29 +232,6 @@ export default class Apigw {
231232

232233
return outputs;
233234
}
234-
235-
// 定制化需求:如果用户在yaml中配置了 serviceId,则只执行删除 api 逻辑
236-
async removeWithInputServiceId(inputs: ApigwRemoveInputs) {
237-
const { environment, serviceId, apiList } = inputs;
238-
239-
// check service exist
240-
const detail = await this.request({
241-
Action: 'DescribeService',
242-
ServiceId: serviceId,
243-
});
244-
245-
if (!detail) {
246-
console.log(`Service ${serviceId} not exist`);
247-
return;
248-
}
249-
250-
// 1. remove all apis
251-
await this.api.bulkRemove({
252-
apiList,
253-
serviceId,
254-
environment,
255-
});
256-
}
257235
}
258236

259237
module.exports = Apigw;

src/modules/apigw/interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ export interface ApigwDeployInputs extends ApigwCreateServiceInputs, ApigwBindCu
158158

159159
endpoints?: ApiEndpoint[];
160160
isInputServiceId?: boolean;
161+
isRemoveTrigger?: boolean;
161162
}
162163

163164
export type ApigwDeployWithServiceIdInputs = ApigwDeployInputs & { serviceId: string };
@@ -233,4 +234,5 @@ export interface ApigwRemoveInputs {
233234
customDomains?: ApigwBindCustomDomainOutputs[];
234235
usagePlan?: ApigwSetupUsagePlanInputs;
235236
isInputServiceId?: boolean;
237+
isRemoveTrigger?: boolean;
236238
}

src/modules/scf/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ export default class Scf {
762762
try {
763763
// delete apigw trigger
764764
const curTrigger = inputs.Triggers[i];
765+
curTrigger.isRemoveTrigger = true;
765766
await this.apigwClient.remove(curTrigger);
766767
} catch (e) {
767768
console.log(e);

src/modules/triggers/apigw.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,15 @@ export default class ApigwTrigger extends BaseTrigger<ApigwTriggerInputsParams>
145145
inputs: TriggerInputs<ApigwTriggerInputsParams>;
146146
}) {
147147
const { parameters } = inputs;
148-
const { oldState, protocols, environment, serviceId, serviceName, serviceDesc } = parameters!;
148+
const {
149+
oldState,
150+
protocols,
151+
environment,
152+
serviceId,
153+
serviceName,
154+
serviceDesc,
155+
isInputServiceId = false,
156+
} = parameters!;
149157
const endpoints = parameters?.endpoints ?? [{ path: '/', method: 'ANY' }];
150158
const triggerInputs: ApigwTriggerInputsParams = {
151159
oldState: oldState ?? {},
@@ -155,7 +163,12 @@ export default class ApigwTrigger extends BaseTrigger<ApigwTriggerInputsParams>
155163
serviceId,
156164
serviceName,
157165
serviceDesc,
158-
isInputServiceId: !!serviceId,
166+
167+
// 定制化需求:是否在 yaml 文件中配置了 apigw 触发器的 serviceId
168+
isInputServiceId,
169+
170+
// 定制化需求:是否是删除云函数的api网关触发器,跟api网关组件区分开
171+
isRemoveTrigger: true,
159172
endpoints: endpoints.map((ep: any) => {
160173
ep.function = ep.function || {};
161174
ep.function.functionName = inputs.functionName;

0 commit comments

Comments
 (0)