|
| 1 | +const BaseTrigger = require('./base'); |
| 2 | +const Apis = require('./apis'); |
| 3 | + |
| 4 | +class ApigwTrigger extends BaseTrigger { |
| 5 | + async removeScfTrigger({ serviceId, apiId, functionName, namespace, qualifier }) { |
| 6 | + // 1. get all trigger list |
| 7 | + const allList = await this.getTriggerList({ |
| 8 | + functionName, |
| 9 | + namespace, |
| 10 | + qualifier, |
| 11 | + }); |
| 12 | + |
| 13 | + // 2. get apigw trigger list |
| 14 | + const apigwList = allList.filter((item) => item.Type === 'apigw'); |
| 15 | + |
| 16 | + const [curApiTrigger] = apigwList.filter(({ ResourceId }) => { |
| 17 | + return ResourceId.indexOf(`service/${serviceId}/API/${apiId}`) !== -1; |
| 18 | + }); |
| 19 | + |
| 20 | + // 3. remove current apigw trigger |
| 21 | + if (curApiTrigger) { |
| 22 | + try { |
| 23 | + await Apis.SCF.DeleteTrigger(this.capi, { |
| 24 | + Type: 'apigw', |
| 25 | + FunctionName: functionName, |
| 26 | + Namespace: namespace, |
| 27 | + Qualifier: qualifier, |
| 28 | + TriggerDesc: curApiTrigger.TriggerDesc, |
| 29 | + TriggerName: curApiTrigger.TriggerName, |
| 30 | + }); |
| 31 | + } catch (e) { |
| 32 | + console.log(e); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + async remove({ serviceId, apiId }) { |
| 37 | + // get api detail |
| 38 | + const apiDetail = await Apis.APIGW.DescribeApi(this.capi, { |
| 39 | + ServiceId: serviceId, |
| 40 | + ApiId: apiId, |
| 41 | + }); |
| 42 | + if (!apiDetail) { |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + // 1. scf type |
| 47 | + if (apiDetail.ServiceScfFunctionName) { |
| 48 | + await this.removeScfTrigger({ |
| 49 | + serviceId, |
| 50 | + apiId, |
| 51 | + functionName: apiDetail.ServiceScfFunctionName, |
| 52 | + namespace: apiDetail.ServiceScfFunctionNamespace, |
| 53 | + qualifier: apiDetail.ServiceScfFunctionQualifier, |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + // 2. ws type |
| 58 | + if (apiDetail.ServiceWebsocketRegisterFunctionName) { |
| 59 | + await this.removeScfTrigger({ |
| 60 | + serviceId, |
| 61 | + apiId, |
| 62 | + functionName: apiDetail.ServiceWebsocketRegisterFunctionName, |
| 63 | + namespace: apiDetail.ServiceWebsocketRegisterFunctionNamespace, |
| 64 | + qualifier: apiDetail.ServiceWebsocketRegisterFunctionQualifier, |
| 65 | + }); |
| 66 | + } |
| 67 | + if (apiDetail.ServiceWebsocketCleanupFunctionName) { |
| 68 | + await this.removeScfTrigger({ |
| 69 | + serviceId, |
| 70 | + apiId, |
| 71 | + functionName: apiDetail.ServiceWebsocketCleanupFunctionName, |
| 72 | + namespace: apiDetail.ServiceWebsocketCleanupFunctionNamespace, |
| 73 | + qualifier: apiDetail.ServiceWebsocketCleanupFunctionQualifier, |
| 74 | + }); |
| 75 | + } |
| 76 | + if (apiDetail.ServiceWebsocketTransportFunctionName) { |
| 77 | + await this.removeScfTrigger({ |
| 78 | + serviceId, |
| 79 | + apiId, |
| 80 | + functionName: apiDetail.ServiceWebsocketTransportFunctionName, |
| 81 | + namespace: apiDetail.ServiceWebsocketTransportFunctionNamespace, |
| 82 | + qualifier: apiDetail.ServiceWebsocketTransportFunctionQualifier, |
| 83 | + }); |
| 84 | + } |
| 85 | + return true; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +module.exports = ApigwTrigger; |
0 commit comments