1
- const { apigw } = require ( 'tencent-cloud-sdk' )
2
- const { uniqueArray } = require ( '../../utils/index' )
1
+ const { apigw} = require ( 'tencent-cloud-sdk' )
2
+ const { uniqueArray} = require ( '../../utils/index' )
3
3
4
4
class Apigw {
5
5
constructor ( credentials = { } , region ) {
@@ -55,7 +55,8 @@ class Apigw {
55
55
protocol : protocols
56
56
} )
57
57
}
58
- } catch ( e ) { }
58
+ } catch ( e ) {
59
+ }
59
60
}
60
61
if ( ! exist ) {
61
62
const createData = await this . request ( {
@@ -76,7 +77,7 @@ class Apigw {
76
77
}
77
78
}
78
79
79
- async createOrUpdateApi ( { serviceId, endpoint } ) {
80
+ async createOrUpdateApi ( { serviceId, endpoint} ) {
80
81
const output = {
81
82
path : endpoint . path ,
82
83
method : endpoint . method ,
@@ -106,8 +107,8 @@ class Apigw {
106
107
const funcQualifier = endpoint . function . functionQualifier
107
108
? endpoint . function . functionQualifier
108
109
: '$LATEST'
109
- ? endpoint . function . functionQualifier
110
- : '$LATEST'
110
+ ? endpoint . function . functionQualifier
111
+ : '$LATEST'
111
112
112
113
if ( endpoint . protocol === 'WEBSOCKET' ) {
113
114
if ( ! endpoint . function . transportFunctionName ) {
@@ -141,6 +142,23 @@ class Apigw {
141
142
}
142
143
143
144
let exist = false
145
+
146
+ // 没有apiId,还需要根据path来确定
147
+ if ( ! endpoint . apiId ) {
148
+ const pathAPIList = await this . request ( {
149
+ Action : 'DescribeApisStatus' ,
150
+ serviceId : serviceId ,
151
+ searchName : endpoint . path
152
+ } )
153
+ if ( pathAPIList . apiIdStatusSet ) {
154
+ for ( let i = 0 ; i < pathAPIList . apiIdStatusSet . length ; i ++ ) {
155
+ if ( pathAPIList . apiIdStatusSet [ i ] . method == endpoint . method && pathAPIList . apiIdStatusSet [ i ] . path == endpoint . path ) {
156
+ endpoint . apiId = pathAPIList . apiIdStatusSet [ i ] . apiId
157
+ }
158
+ }
159
+ }
160
+ }
161
+
144
162
if ( endpoint . apiId ) {
145
163
try {
146
164
const detail = await this . request ( {
@@ -159,10 +177,13 @@ class Apigw {
159
177
output . apiId = endpoint . apiId
160
178
console . log ( `Service with id ${ output . apiId } updated.` )
161
179
}
162
- } catch ( e ) { }
180
+ } catch ( e ) {
181
+ }
163
182
}
183
+
184
+
164
185
if ( ! exist ) {
165
- const { apiId } = await this . request ( {
186
+ const { apiId} = await this . request ( {
166
187
Action : 'CreateApi' ,
167
188
...apiInputs
168
189
} )
@@ -171,7 +192,7 @@ class Apigw {
171
192
console . log ( `API with id ${ output . apiId } created.` )
172
193
}
173
194
174
- const { internalDomain } = await this . request ( {
195
+ const { internalDomain} = await this . request ( {
175
196
Action : 'DescribeApi' ,
176
197
serviceId : serviceId ,
177
198
apiId : output . apiId
@@ -181,15 +202,15 @@ class Apigw {
181
202
return output
182
203
}
183
204
184
- async setupUsagePlanSecret ( { secretName, secretIds } ) {
205
+ async setupUsagePlanSecret ( { secretName, secretIds} ) {
185
206
const secretIdsOutput = {
186
207
created : false ,
187
208
secretIds
188
209
}
189
210
190
211
if ( secretIds . length === 0 ) {
191
212
console . log ( `Creating a new Secret key.` )
192
- const { secretId, secretKey } = await this . request ( {
213
+ const { secretId, secretKey} = await this . request ( {
193
214
Action : 'CreateApiKey' ,
194
215
secretName : secretName ,
195
216
type : 'auto'
@@ -201,7 +222,7 @@ class Apigw {
201
222
const uniqSecretIds = uniqueArray ( secretIds )
202
223
203
224
// get all secretId, check local secretId exists
204
- const { apiKeyStatusSet } = await this . request ( {
225
+ const { apiKeyStatusSet} = await this . request ( {
205
226
Action : 'DescribeApiKeysStatus' ,
206
227
secretIds : uniqSecretIds ,
207
228
limit : uniqSecretIds . length
@@ -238,7 +259,7 @@ class Apigw {
238
259
return secretIdsOutput
239
260
}
240
261
241
- async setupApiUsagePlan ( { usagePlan } ) {
262
+ async setupApiUsagePlan ( { usagePlan} ) {
242
263
const usageInputs = {
243
264
usagePlanName : usagePlan . usagePlanName || '' ,
244
265
usagePlanDesc : usagePlan . usagePlanDesc || '' ,
@@ -274,9 +295,9 @@ class Apigw {
274
295
/**
275
296
* get all unbound secretids
276
297
*/
277
- async getUnboundSecretIds ( { usagePlanId, secretIds } ) {
278
- const getAllBoundSecrets = async ( res = [ ] , { limit, offset = 0 } ) => {
279
- const { secretIdList } = await this . request ( {
298
+ async getUnboundSecretIds ( { usagePlanId, secretIds} ) {
299
+ const getAllBoundSecrets = async ( res = [ ] , { limit, offset = 0 } ) => {
300
+ const { secretIdList} = await this . request ( {
280
301
Action : 'DescribeUsagePlanSecretIds' ,
281
302
usagePlanId,
282
303
limit,
@@ -285,9 +306,9 @@ class Apigw {
285
306
if ( secretIdList . length < limit ) {
286
307
return res
287
308
}
288
- return getAllBoundSecrets ( res , { limit, offset : offset + secretIdList . length } )
309
+ return getAllBoundSecrets ( res , { limit, offset : offset + secretIdList . length } )
289
310
}
290
- const allBoundSecretObjs = await getAllBoundSecrets ( [ ] , { limit : 100 } )
311
+ const allBoundSecretObjs = await getAllBoundSecrets ( [ ] , { limit : 100 } )
291
312
const allBoundSecretIds = allBoundSecretObjs . map ( ( item ) => item . secretId )
292
313
const unboundSecretIds = secretIds . filter ( ( item ) => {
293
314
if ( allBoundSecretIds . indexOf ( item ) === - 1 ) {
@@ -300,8 +321,8 @@ class Apigw {
300
321
}
301
322
302
323
// bind custom domains
303
- async bindCustomDomain ( { serviceId, subDomain, inputs } ) {
304
- const { customDomains, oldState = { } } = inputs
324
+ async bindCustomDomain ( { serviceId, subDomain, inputs} ) {
325
+ const { customDomains, oldState = { } } = inputs
305
326
// 1. unbind all custom domain
306
327
const customDomainDetail = await this . request ( {
307
328
Action : 'DescribeServiceSubDomains' ,
@@ -312,7 +333,7 @@ class Apigw {
312
333
customDomainDetail . domainSet &&
313
334
customDomainDetail . domainSet . length > 0
314
335
) {
315
- const { domainSet = [ ] } = customDomainDetail
336
+ const { domainSet = [ ] } = customDomainDetail
316
337
// unbind all created domain
317
338
const stateDomains = oldState . customDomains || [ ]
318
339
for ( let i = 0 ; i < domainSet . length ; i ++ ) {
@@ -368,14 +389,14 @@ class Apigw {
368
389
369
390
// bind environment fo usage plan
370
391
async bindUsagePlanEnvironment ( {
371
- environment,
372
- bindType = 'API' ,
373
- serviceId,
374
- apiId,
375
- endpoint,
376
- usagePlan
377
- } ) {
378
- const { usagePlanList } = await this . request ( {
392
+ environment,
393
+ bindType = 'API' ,
394
+ serviceId,
395
+ apiId,
396
+ endpoint,
397
+ usagePlan
398
+ } ) {
399
+ const { usagePlanList} = await this . request ( {
379
400
Action : 'DescribeApiUsagePlan' ,
380
401
serviceId,
381
402
apiIds : [ apiId ]
@@ -403,17 +424,17 @@ class Apigw {
403
424
}
404
425
405
426
async deploy ( inputs ) {
406
- const { environment, oldState = { } } = inputs
427
+ const { environment, oldState = { } } = inputs
407
428
inputs . protocols = this . getProtocolString ( inputs . protocols )
408
429
409
- const { serviceId, serviceName, subDomain, serviceCreated } = await this . createOrUpdateService (
430
+ const { serviceId, serviceName, subDomain, serviceCreated} = await this . createOrUpdateService (
410
431
inputs
411
432
)
412
433
413
434
const apiList = [ ]
414
435
const stateApiList = oldState . apiList || [ ]
415
436
416
- const { endpoints } = inputs
437
+ const { endpoints} = inputs
417
438
for ( let i = 0 , len = endpoints . length ; i < len ; i ++ ) {
418
439
const endpoint = endpoints [ i ]
419
440
// if exist in state list, set created to be true
@@ -445,7 +466,7 @@ class Apigw {
445
466
// store in api list
446
467
curApi . usagePlan = usagePlan
447
468
448
- const { secretIds = [ ] } = endpoint . auth
469
+ const { secretIds = [ ] } = endpoint . auth
449
470
const secrets = await this . setupUsagePlanSecret ( {
450
471
secretName : endpoint . auth . secretName ,
451
472
secretIds
@@ -517,15 +538,15 @@ class Apigw {
517
538
}
518
539
519
540
async remove ( inputs ) {
520
- const { created, environment, serviceId, apiList, customDomains } = inputs
541
+ const { created, environment, serviceId, apiList, customDomains} = inputs
521
542
// 1. remove all apis
522
543
for ( let i = 0 ; i < apiList . length ; i ++ ) {
523
544
const curApi = apiList [ i ]
524
545
525
546
// 1. remove usage plan
526
547
if ( curApi . usagePlan ) {
527
548
// 1.1 unbind secrete ids
528
- const { secrets } = curApi . usagePlan
549
+ const { secrets} = curApi . usagePlan
529
550
if ( secrets && secrets . secretIds ) {
530
551
await this . request ( {
531
552
Action : 'UnBindSecretIds' ,
0 commit comments