@@ -8,7 +8,7 @@ const Cam = require('../cam/index');
88const { formatFunctionInputs } = require ( './utils' ) ;
99const CONFIGS = require ( './config' ) ;
1010const Apis = require ( './apis' ) ;
11- const Triggers = require ( './triggers' ) ;
11+ const TRIGGERS = require ( './triggers' ) ;
1212
1313class 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 ,
0 commit comments