Skip to content

Commit b452285

Browse files
dfounderliuyugasun
authored andcommitted
fix: 增加cns
1 parent 0fcc0b8 commit b452285

File tree

1 file changed

+91
-16
lines changed

1 file changed

+91
-16
lines changed

src/serverless.js

Lines changed: 91 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const { Component } = require('@serverless/core')
22
const ensureObject = require('type/object/ensure')
33
const ensureIterable = require('type/iterable/ensure')
44
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')
67
const { packageExpress, generateId } = require('./utils')
78

89
const DEFAULTS = {
@@ -22,6 +23,14 @@ class Express extends Component {
2223
return 'http'
2324
}
2425

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+
2534
mergeJson(sourceJson, targetJson) {
2635
for (const eveKey in sourceJson) {
2736
if (targetJson.hasOwnProperty(eveKey)) {
@@ -47,6 +56,29 @@ class Express extends Component {
4756
return targetJson
4857
}
4958

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+
5082
capitalString(str) {
5183
if (str.length < 2) {
5284
return str.toUpperCase()
@@ -55,7 +87,7 @@ class Express extends Component {
5587
return `${str[0].toUpperCase()}${str.slice(1)}`
5688
}
5789

58-
async prepareInputs(inputs = {}) {
90+
async prepareInputs(credentials, inputs = {}) {
5991
// 对function inputs进行标准化
6092
const tempFunctionConf = inputs.functionConf ? inputs.functionConf : {}
6193
const fromClientRemark = `tencent-express`
@@ -181,11 +213,9 @@ class Express extends Component {
181213
const cnsConf = []
182214
// 对cns inputs进行检查和赋值
183215
if (apigatewayConf.customDomain && apigatewayConf.customDomain.length > 0) {
216+
const domain = new Domain(credentials)
184217
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)
189219
const tempInputs = {
190220
domain: domainData.domain,
191221
records: []
@@ -218,11 +248,12 @@ class Express extends Component {
218248
async uploadCodeToCos(credentials, inputs, region, filePath) {
219249
// 创建cos对象
220250
const cos = new Cos(credentials, region)
251+
const userInfo = await this.getUserInfo(credentials)
221252
// 创建存储桶 + 设置生命周期
222253
if (!inputs.code.bucket) {
223254
inputs.code.bucket = `sls-cloudfunction-${region}-code`
224255
await cos.deploy({
225-
bucket: inputs.code.bucket,
256+
bucket: inputs.code.bucket + '-' + userInfo.Response.AppId,
226257
force: true,
227258
lifecycle: [
228259
{
@@ -241,7 +272,7 @@ class Express extends Component {
241272
const object = `${inputs.name}-${Math.floor(Date.now() / 1000)}.zip`
242273
inputs.code.object = object
243274
await cos.upload({
244-
bucket: inputs.code.bucket,
275+
bucket: inputs.code.bucket + '-' + userInfo.Response.AppId,
245276
file: filePath,
246277
key: inputs.code.object
247278
})
@@ -325,9 +356,48 @@ class Express extends Component {
325356
return outputs
326357
}
327358

328-
async deployCns(credentials, inputs, outputs = {}) {
359+
async deployCns(credentials, inputs, regionList, apigwOutputs) {
329360
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
331401
}
332402

333403
async deploy(inputs) {
@@ -337,7 +407,7 @@ class Express extends Component {
337407
const credentials = this.credentials.tencent
338408

339409
// 对Inputs内容进行标准化
340-
const { regionList, functionConf, apigatewayConf, cnsConf } = await this.prepareInputs(inputs)
410+
const { regionList, functionConf, apigatewayConf, cnsConf } = await this.prepareInputs(credentials, inputs)
341411

342412
// 部署函数 + API网关
343413
const outputs = {}
@@ -350,12 +420,9 @@ class Express extends Component {
350420
outputs['scf'] = functionOutputs
351421

352422
// 云解析遇到等API网关部署完成才可以继续部署
353-
// this.deployCns(credentials, cnsConf, outputs)
423+
outputs['cns'] = await this.deployCns(credentials, cnsConf, regionList, apigwOutputs)
354424

355-
outputs.url = this.state.url
356-
if (this.state.domain) {
357-
outputs.domain = `https://${this.state.domain}`
358-
}
425+
console.log(this.state)
359426

360427
return outputs
361428
}
@@ -389,6 +456,14 @@ class Express extends Component {
389456
}
390457

391458
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+
392467
this.state = {}
393468
}
394469
}

0 commit comments

Comments
 (0)