@@ -8,7 +8,7 @@ const Cam = require('../cam/index');
8
8
const { formatFunctionInputs } = require ( './utils' ) ;
9
9
const CONFIGS = require ( './config' ) ;
10
10
const Apis = require ( './apis' ) ;
11
- const Triggers = require ( './triggers' ) ;
11
+ const TRIGGERS = require ( './triggers' ) ;
12
12
13
13
class Scf {
14
14
constructor ( credentials = { } , region ) {
@@ -167,45 +167,57 @@ class Scf {
167
167
return true ;
168
168
}
169
169
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
+
170
185
// deploy SCF triggers
171
186
async deployTrigger ( funcInfo , inputs ) {
172
187
console . log ( `Deploying ${ inputs . name } 's triggers in ${ this . region } .` ) ;
173
188
174
189
// should check function status is active, then continue
175
190
await this . isOperationalStatus ( inputs . namespace , inputs . name ) ;
176
191
192
+ // get all triggers
193
+ const triggerList = await this . getTriggerList ( funcInfo . FunctionName , funcInfo . Namespace ) ;
194
+
177
195
// 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 ] ;
181
198
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
+ }
190
207
}
191
208
}
192
209
193
210
// create all new triggers
194
211
const triggerResult = [ ] ;
195
212
for ( let i = 0 ; i < inputs . events . length ; i ++ ) {
196
213
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 ] ;
199
216
if ( ! triggerClass ) {
200
- throw TypeError ( 'PARAMETER_SCF' , `Unknow trigger type ${ eventType } ` ) ;
217
+ throw TypeError ( 'PARAMETER_SCF' , `Unknow trigger type ${ Type } ` ) ;
201
218
}
202
219
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 ] ) ;
209
221
210
222
triggerResult . push ( triggerOutput ) ;
211
223
} catch ( e ) {
@@ -245,7 +257,7 @@ class Scf {
245
257
* @param {object } inputs publish version parameter
246
258
*/
247
259
async publishVersion ( inputs ) {
248
- console . log ( `Publish function ${ inputs . functionName } version` ) ;
260
+ console . log ( `Publishing function ${ inputs . functionName } version` ) ;
249
261
const publishInputs = {
250
262
Action : 'PublishVersion' ,
251
263
FunctionName : inputs . functionName ,
0 commit comments