Skip to content

Commit 2a536a9

Browse files
committed
fix(apigw): optimize remove apigw trigger flow
1 parent 15dca35 commit 2a536a9

File tree

4 files changed

+88
-27
lines changed

4 files changed

+88
-27
lines changed

src/modules/apigw/entities/service.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import {
66
ApigwCreateOrUpdateServiceOutputs,
77
ApigwSetupUsagePlanInputs,
88
} from '../interface';
9+
import { ApiServiceType } from '../../interface';
910
import { pascalCaseProps, deepClone } from '../../../utils';
1011
import APIS, { ActionType } from '../apis';
1112
import UsagePlanEntity from './usage-plan';
13+
import Tag from '../../tag';
1214

1315
interface Detail {
1416
InnerSubDomain: string;
@@ -27,11 +29,22 @@ interface Detail {
2729
export default class ServiceEntity {
2830
capi: Capi;
2931
usagePlan: UsagePlanEntity;
32+
tag: Tag;
3033

3134
constructor(capi: Capi) {
3235
this.capi = capi;
3336

3437
this.usagePlan = new UsagePlanEntity(capi);
38+
39+
const { options } = capi;
40+
this.tag = new Tag(
41+
{
42+
SecretId: options.SecretId,
43+
SecretKey: options.SecretKey,
44+
Token: options.Token,
45+
},
46+
options.Region,
47+
);
3548
}
3649

3750
async request({ Action, ...data }: { Action: ActionType; [key: string]: any }) {
@@ -303,4 +316,42 @@ export default class ServiceEntity {
303316
releaseDesc: 'Released by Serverless',
304317
});
305318
}
319+
320+
async remove({ serviceId, environment }: { serviceId: string; environment: string }) {
321+
const detail = await this.getById(serviceId);
322+
if (!detail) {
323+
console.log(`API service ${serviceId} not exist`);
324+
return true;
325+
}
326+
327+
try {
328+
console.log(`Unreleasing service: ${serviceId}, environment ${environment}`);
329+
await this.request({
330+
Action: 'UnReleaseService',
331+
serviceId,
332+
environmentName: environment,
333+
});
334+
console.log(`Unrelease service ${serviceId}, environment ${environment} success`);
335+
336+
// 在删除之前,如果关联了标签,需要先删除标签关联
337+
if (detail.Tags && detail.Tags.length > 0) {
338+
await this.tag.deployResourceTags({
339+
tags: [],
340+
resourceId: serviceId,
341+
serviceType: ApiServiceType.apigw,
342+
resourcePrefix: 'service',
343+
});
344+
}
345+
346+
// delete service
347+
console.log(`Removing service ${serviceId}`);
348+
await this.request({
349+
Action: 'DeleteService',
350+
serviceId,
351+
});
352+
console.log(`Remove service ${serviceId} success`);
353+
} catch (e) {
354+
console.log(e.message);
355+
}
356+
}
306357
}

src/modules/apigw/index.ts

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export default class Apigw {
193193
return;
194194
}
195195

196-
// remove usage plan
196+
// 删除使用计划
197197
if (usagePlan) {
198198
await this.usagePlan.remove({
199199
serviceId,
@@ -202,7 +202,7 @@ export default class Apigw {
202202
});
203203
}
204204

205-
// 2. unbind all custom domains
205+
// 解绑自定义域名
206206
if (customDomains) {
207207
for (let i = 0; i < customDomains.length; i++) {
208208
const curDomain = customDomains[i];
@@ -217,33 +217,11 @@ export default class Apigw {
217217
}
218218
}
219219

220-
if (created === true) {
221-
// unrelease service
222-
console.log(`Unreleasing service: ${serviceId}, environment ${environment}`);
223-
await this.removeRequest({
224-
Action: 'UnReleaseService',
225-
serviceId,
226-
environmentName: environment,
227-
});
228-
console.log(`Unrelease service ${serviceId}, environment ${environment} success`);
229-
230-
// 在删除之前,如果关联了标签,需要先删除标签关联
231-
if (detail.Tags && detail.Tags.length > 0) {
232-
await this.tagClient.deployResourceTags({
233-
tags: [],
234-
resourceId: serviceId,
235-
serviceType: ApiServiceType.apigw,
236-
resourcePrefix: 'service',
237-
});
238-
}
239-
240-
// delete service
241-
console.log(`Removing service ${serviceId}`);
242-
await this.removeRequest({
243-
Action: 'DeleteService',
220+
if (created && isAutoRelease) {
221+
await this.service.remove({
244222
serviceId,
223+
environment,
245224
});
246-
console.log(`Remove service ${serviceId} success`);
247225
}
248226
}
249227

src/modules/triggers/interface/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,14 @@ export interface NewTriggerInputs {
162162
export * from './clb';
163163

164164
export interface SimpleApigwDetail {
165+
// 是否是通过 CLI 创建的
166+
created?: boolean;
167+
// 当前触发器关联函数名称
165168
functionName: string;
169+
// 服务 ID
166170
serviceId: string;
171+
// 服务名称
167172
serviceName: string;
173+
// 发布的环境
168174
environment: string;
169175
}

src/modules/triggers/manager.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ export class TriggerManager {
273273
// 筛选出 API 网关触发器,可以单独的进行发布
274274
if (triggerOutput.serviceId) {
275275
apigwServiceList.push({
276+
created: triggerOutput.created,
276277
functionName: name,
277278
serviceId: triggerOutput.serviceId,
278279
serviceName: triggerOutput.serviceName,
@@ -403,6 +404,31 @@ export class TriggerManager {
403404
});
404405
}
405406

407+
/**
408+
* 批量删除 API 网关,防止重复删除同一个网关
409+
* @param list API 网关列表
410+
*/
411+
async bulkRemoveApigw(list: SimpleApigwDetail[]) {
412+
// 筛选非重复的网关服务
413+
const uniqueList: SimpleApigwDetail[] = [];
414+
const map: { [key: string]: number } = {};
415+
list.forEach((item) => {
416+
if (!map[item.serviceId]) {
417+
map[item.serviceId] = 1;
418+
uniqueList.push(item);
419+
}
420+
});
421+
422+
const releaseTask: Promise<any>[] = [];
423+
for (let i = 0; i < uniqueList.length; i++) {
424+
const temp = uniqueList[i];
425+
const exist = await this.apigwClient.service.getById(temp.serviceId);
426+
if (exist) {
427+
releaseTask.push(this.apigwClient.service.release(temp));
428+
}
429+
}
430+
await Promise.all(releaseTask);
431+
}
406432
/**
407433
* 批量发布 API 网关,防止重复发布同一个网关
408434
* @param list API 网关列表

0 commit comments

Comments
 (0)