@@ -2,7 +2,8 @@ const { Component } = require('@serverless/core')
2
2
const ensureObject = require ( 'type/object/ensure' )
3
3
const ensureIterable = require ( 'type/iterable/ensure' )
4
4
const ensureString = require ( 'type/string/ensure' )
5
- const { MultiApigw, Scf, Apigw, Cos, Cns } = require ( 'tencent-component-toolkit' )
5
+ const Cam = require ( 'tencent-cloud-sdk' ) . cam
6
+ const { MultiApigw, Scf, Apigw, Cos, Cns, Domain} = require ( 'tencent-component-toolkit' )
6
7
const { packageExpress, generateId } = require ( './utils' )
7
8
8
9
const DEFAULTS = {
@@ -22,6 +23,14 @@ class Express extends Component {
22
23
return 'http'
23
24
}
24
25
26
+ async getUserInfo ( credentials ) {
27
+ const cam = new Cam ( credentials )
28
+ return await cam . request ( {
29
+ Action : 'GetUserAppId' ,
30
+ Version : '2019-01-16'
31
+ } )
32
+ }
33
+
25
34
mergeJson ( sourceJson , targetJson ) {
26
35
for ( const eveKey in sourceJson ) {
27
36
if ( targetJson . hasOwnProperty ( eveKey ) ) {
@@ -47,6 +56,29 @@ class Express extends Component {
47
56
return targetJson
48
57
}
49
58
59
+ deleteRecord ( newRecords , historyRcords ) {
60
+ const deleteList = [ ]
61
+ for ( let i = 0 ; i < historyRcords . length ; i ++ ) {
62
+ let temp = false
63
+ for ( let j = 0 ; j < newRecords . length ; j ++ ) {
64
+ if (
65
+ newRecords [ j ] . domain == historyRcords [ i ] . domain &&
66
+ newRecords [ j ] . subDomain == historyRcords [ i ] . subDomain &&
67
+ newRecords [ j ] . recordType == historyRcords [ i ] . recordType &&
68
+ newRecords [ j ] . value == historyRcords [ i ] . value &&
69
+ newRecords [ j ] . recordLine == historyRcords [ i ] . recordLine
70
+ ) {
71
+ temp = true
72
+ break
73
+ }
74
+ }
75
+ if ( ! temp ) {
76
+ deleteList . push ( historyRcords [ i ] )
77
+ }
78
+ }
79
+ return deleteList
80
+ }
81
+
50
82
capitalString ( str ) {
51
83
if ( str . length < 2 ) {
52
84
return str . toUpperCase ( )
@@ -55,7 +87,7 @@ class Express extends Component {
55
87
return `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } `
56
88
}
57
89
58
- async prepareInputs ( inputs = { } ) {
90
+ async prepareInputs ( credentials , inputs = { } ) {
59
91
// 对function inputs进行标准化
60
92
const tempFunctionConf = inputs . functionConf ? inputs . functionConf : { }
61
93
const fromClientRemark = `tencent-express`
@@ -181,11 +213,9 @@ class Express extends Component {
181
213
const cnsConf = [ ]
182
214
// 对cns inputs进行检查和赋值
183
215
if ( apigatewayConf . customDomain && apigatewayConf . customDomain . length > 0 ) {
216
+ const domain = new Domain ( credentials )
184
217
for ( let domianNum = 0 ; domianNum < apigatewayConf . customDomain . length ; domianNum ++ ) {
185
- const tencentDomain = await this . load ( '@serverless/tencent-domain' )
186
- const domainData = await tencentDomain . check ( {
187
- domain : apigatewayConf . customDomain [ domianNum ] . domain
188
- } )
218
+ const domainData = await domain . check ( apigatewayConf . customDomain [ domianNum ] . domain )
189
219
const tempInputs = {
190
220
domain : domainData . domain ,
191
221
records : [ ]
@@ -218,11 +248,12 @@ class Express extends Component {
218
248
async uploadCodeToCos ( credentials , inputs , region , filePath ) {
219
249
// 创建cos对象
220
250
const cos = new Cos ( credentials , region )
251
+ const userInfo = await this . getUserInfo ( credentials )
221
252
// 创建存储桶 + 设置生命周期
222
253
if ( ! inputs . code . bucket ) {
223
254
inputs . code . bucket = `sls-cloudfunction-${ region } -code`
224
255
await cos . deploy ( {
225
- bucket : inputs . code . bucket ,
256
+ bucket : inputs . code . bucket + '-' + userInfo . Response . AppId ,
226
257
force : true ,
227
258
lifecycle : [
228
259
{
@@ -241,7 +272,7 @@ class Express extends Component {
241
272
const object = `${ inputs . name } -${ Math . floor ( Date . now ( ) / 1000 ) } .zip`
242
273
inputs . code . object = object
243
274
await cos . upload ( {
244
- bucket : inputs . code . bucket ,
275
+ bucket : inputs . code . bucket + '-' + userInfo . Response . AppId ,
245
276
file : filePath ,
246
277
key : inputs . code . object
247
278
} )
@@ -325,9 +356,48 @@ class Express extends Component {
325
356
return outputs
326
357
}
327
358
328
- async deployCns ( credentials , inputs , outputs = { } ) {
359
+ async deployCns ( credentials , inputs , regionList , apigwOutputs ) {
329
360
const cns = new Cns ( credentials )
330
- outputs [ 'cns' ] = await cns . deploy ( inputs )
361
+ const cnsRegion = { }
362
+ if ( regionList . length == 1 ) {
363
+ const [ curRegion ] = regionList
364
+ const curApigwOutput = apigwOutputs [ curRegion ]
365
+ cnsRegion [ curRegion ] = curApigwOutput . subDomain
366
+ } else {
367
+ for ( let i = 0 ; i < regionList . length ; i ++ ) {
368
+ const curRegion = regionList [ i ]
369
+ const curApigwOutput = apigwOutputs [ curRegion ]
370
+ cnsRegion [ curRegion ] = curApigwOutput . subDomain
371
+ }
372
+ }
373
+
374
+ const state = [ ]
375
+ const outputs = [ ]
376
+ for ( let i = 0 ; i < inputs . length ; i ++ ) {
377
+ const curCns = inputs [ i ]
378
+ for ( let j = 0 ; j < curCns . records . length ; j ++ ) {
379
+ curCns . records [ j ] . value =
380
+ cnsRegion [ curCns . records [ j ] . value . replace ( 'temp_value_about_' , '' ) ]
381
+ }
382
+ const tencentCnsOutputs = await cns . deploy ( curCns )
383
+ if ( tencentCnsOutputs . DNS ) {
384
+ outputs [ curCns . domain ] = tencentCnsOutputs . DNS
385
+ }
386
+ state . push ( tencentCnsOutputs )
387
+ }
388
+
389
+
390
+ // // 删除serverless创建的但是不在本次列表中
391
+ // for(const)
392
+ // const recordHistory = this.state.cns || []
393
+ // const delList = this.deleteRecord(state, recordHistory)
394
+ // for (let i = 0; i < recordHistory.length; i++) {
395
+ // await cns.remove( {deleteList: delList})
396
+ // }
397
+
398
+ this . state [ 'cns' ] = state
399
+ this . save ( )
400
+ return outputs
331
401
}
332
402
333
403
async deploy ( inputs ) {
@@ -337,7 +407,7 @@ class Express extends Component {
337
407
const credentials = this . credentials . tencent
338
408
339
409
// 对Inputs内容进行标准化
340
- const { regionList, functionConf, apigatewayConf, cnsConf } = await this . prepareInputs ( inputs )
410
+ const { regionList, functionConf, apigatewayConf, cnsConf } = await this . prepareInputs ( credentials , inputs )
341
411
342
412
// 部署函数 + API网关
343
413
const outputs = { }
@@ -350,12 +420,9 @@ class Express extends Component {
350
420
outputs [ 'scf' ] = functionOutputs
351
421
352
422
// 云解析遇到等API网关部署完成才可以继续部署
353
- // this.deployCns(credentials, cnsConf, outputs )
423
+ outputs [ 'cns' ] = await this . deployCns ( credentials , cnsConf , regionList , apigwOutputs )
354
424
355
- outputs . url = this . state . url
356
- if ( this . state . domain ) {
357
- outputs . domain = `https://${ this . state . domain } `
358
- }
425
+ console . log ( this . state )
359
426
360
427
return outputs
361
428
}
@@ -389,6 +456,14 @@ class Express extends Component {
389
456
}
390
457
391
458
await Promise . all ( removeHandlers )
459
+
460
+ if ( this . state . cns ) {
461
+ const cns = new Cns ( credentials )
462
+ for ( let i = 0 ; i < this . state . cns . length ; i ++ ) {
463
+ await cns . remove ( { deleteList : this . state . cns [ i ] . records } )
464
+ }
465
+ }
466
+
392
467
this . state = { }
393
468
}
394
469
}
0 commit comments