Skip to content

Commit 8aae828

Browse files
committed
fix(scf): get trigger list method
1 parent 2ce3f66 commit 8aae828

File tree

5 files changed

+40
-26
lines changed

5 files changed

+40
-26
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ jobs:
4444
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
4545
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
4646
GIT_AUTHOR_NAME: slsplus
47-
GIT_AUTHOR_EMAIL: yuga.sun.bj@gmail.com
47+
GIT_AUTHOR_EMAIL: slsplus.sz@gmail.com
4848
GIT_COMMITTER_NAME: slsplus
49-
GIT_COMMITTER_EMAIL: yuga.sun.bj@gmail.com
49+
GIT_COMMITTER_EMAIL: slsplus.sz@gmail.com

__tests__/scf.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('Scf', () => {
1515
};
1616

1717
const inputs = {
18-
name: 'serverless-test',
18+
name: `serverless-test-${Date.now()}`,
1919
code: {
2020
bucket: process.env.BUCKET,
2121
object: 'express_code.zip',
@@ -226,7 +226,7 @@ describe('Scf', () => {
226226
CodeResult: 'success',
227227
CodeError: '',
228228
ErrNo: 0,
229-
Tags: [],
229+
Tags: expect.any(Array),
230230
AccessInfo: { Host: '', Vip: '' },
231231
Type: 'Event',
232232
CfsConfig: {

src/modules/scf/apis.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const ACTIONS = [
1515
'DeleteAlias',
1616
'GetAlias',
1717
'Invoke',
18+
'ListTriggers',
1819
];
1920

2021
const APIS = ApiFactory({

src/modules/scf/index.js

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const Cam = require('../cam/index');
88
const { formatFunctionInputs } = require('./utils');
99
const CONFIGS = require('./config');
1010
const Apis = require('./apis');
11-
const Triggers = require('./triggers');
11+
const TRIGGERS = require('./triggers');
1212

1313
class Scf {
1414
constructor(credentials = {}, region) {
@@ -167,45 +167,57 @@ class Scf {
167167
return true;
168168
}
169169

170+
async getTriggerList(functionName, namespace = 'default') {
171+
const { Triggers = [], TotalCount } = await this.request({
172+
Action: 'ListTriggers',
173+
FunctionName: functionName,
174+
Namespace: namespace,
175+
Limit: 100,
176+
});
177+
if (TotalCount > 100) {
178+
const res = await this.getTriggerList(functionName, namespace);
179+
return Triggers.concat(res);
180+
}
181+
182+
return Triggers;
183+
}
184+
170185
// deploy SCF triggers
171186
async deployTrigger(funcInfo, inputs) {
172187
console.log(`Deploying ${inputs.name}'s triggers in ${this.region}.`);
173188

174189
// should check function status is active, then continue
175190
await this.isOperationalStatus(inputs.namespace, inputs.name);
176191

192+
// get all triggers
193+
const triggerList = await this.getTriggerList(funcInfo.FunctionName, funcInfo.Namespace);
194+
177195
// remove all old triggers
178-
const oldTriggers = funcInfo.Triggers || [];
179-
for (let tIdx = 0, len = oldTriggers.length; tIdx < len; tIdx++) {
180-
const curTrigger = oldTriggers[tIdx];
196+
for (let i = 0, len = triggerList.length; i < len; i++) {
197+
const curTrigger = triggerList[i];
181198
const { Type } = curTrigger;
182-
const triggerClass = Triggers[Type];
183-
184-
if (Type === 'apigw') {
185-
// TODO: now apigw can not sync in SCF trigger list
186-
// await this.apigwClient.remove(curTrigger);
187-
} else {
188-
console.log(`Removing ${curTrigger.Type} triggers: ${curTrigger.TriggerName}.`);
189-
await triggerClass.delete(this, funcInfo, curTrigger);
199+
const triggerClass = TRIGGERS[Type];
200+
if (triggerClass) {
201+
if (Type === 'apigw') {
202+
// TODO: now apigw can not sync in SCF trigger list
203+
// await this.apigwClient.remove(curTrigger);
204+
} else {
205+
await triggerClass.delete(this, funcInfo, curTrigger);
206+
}
190207
}
191208
}
192209

193210
// create all new triggers
194211
const triggerResult = [];
195212
for (let i = 0; i < inputs.events.length; i++) {
196213
const event = inputs.events[i];
197-
const eventType = Object.keys(event)[0];
198-
const triggerClass = Triggers[eventType];
214+
const Type = Object.keys(event)[0];
215+
const triggerClass = TRIGGERS[Type];
199216
if (!triggerClass) {
200-
throw TypeError('PARAMETER_SCF', `Unknow trigger type ${eventType}`);
217+
throw TypeError('PARAMETER_SCF', `Unknow trigger type ${Type}`);
201218
}
202219
try {
203-
const triggerOutput = await triggerClass.create(
204-
this,
205-
this.region,
206-
funcInfo,
207-
event[eventType],
208-
);
220+
const triggerOutput = await triggerClass.create(this, this.region, funcInfo, event[Type]);
209221

210222
triggerResult.push(triggerOutput);
211223
} catch (e) {
@@ -245,7 +257,7 @@ class Scf {
245257
* @param {object} inputs publish version parameter
246258
*/
247259
async publishVersion(inputs) {
248-
console.log(`Publish function ${inputs.functionName} version`);
260+
console.log(`Publishing function ${inputs.functionName} version`);
249261
const publishInputs = {
250262
Action: 'PublishVersion',
251263
FunctionName: inputs.functionName,

src/modules/scf/triggers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const BaseTrigger = {
1515
Type: inputs.Type,
1616
TriggerDesc: inputs.TriggerDesc,
1717
TriggerName: inputs.TriggerName,
18+
Qualifier: inputs.Qualifier,
1819
});
1920
return true;
2021
} catch (e) {

0 commit comments

Comments
 (0)