Skip to content

Commit b5045cd

Browse files
author
dfounderliu
committed
增加 scf/cns/cos/domain/tag 组件
1 parent da70814 commit b5045cd

File tree

10 files changed

+1608
-0
lines changed

10 files changed

+1608
-0
lines changed

src/baas/cns/index.js

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
const { cns } = require('tencent-cloud-sdk')
2+
class CnsUtils {
3+
constructor(credentials = {}, region = 'ap-guangzhou') {
4+
this.region = region
5+
this.credentials = credentials
6+
this.cnsClient = new cns(this.credentials)
7+
}
8+
9+
haveRecord(newRecord, historyRcords) {
10+
for (let i = 0; i < historyRcords.length; i++) {
11+
if (newRecord.recordType == 'CNAME' && newRecord.value.slice(-1) != '.') {
12+
newRecord.value = `${newRecord.value}.`
13+
}
14+
if (
15+
newRecord.domain == historyRcords[i].domain &&
16+
newRecord.subDomain == historyRcords[i].subDomain &&
17+
newRecord.recordType == historyRcords[i].recordType &&
18+
newRecord.value == historyRcords[i].value &&
19+
newRecord.recordLine == historyRcords[i].recordLine
20+
) {
21+
return historyRcords[i]
22+
}
23+
}
24+
return false
25+
}
26+
27+
async deploy(inputs = {}) {
28+
// 准备一些临时参数
29+
const recordList = []
30+
const recordRelease = []
31+
let domainLength = 100
32+
let offset = 0
33+
const output = { records: [] }
34+
35+
// 获取线上的域名记录列表
36+
console.log(`Getting release domain records ... `)
37+
try {
38+
while (domainLength == 100) {
39+
const statusInputs = {
40+
Action: 'RecordList',
41+
Region: this.region,
42+
offset: offset,
43+
length: domainLength,
44+
domain: inputs.domain
45+
}
46+
let recordReleaseList = await this.cnsClient.request(statusInputs)
47+
if (recordReleaseList.code != 0) {
48+
// 如果没找到Domain,则尝试添加Domain
49+
try {
50+
console.log(`Get release domain error.`)
51+
console.log(`Adding domain ...`)
52+
await this.cnsClient.request({
53+
Action: 'DomainCreate',
54+
Region: this.region,
55+
domain: inputs.domain
56+
})
57+
console.log(`Added domain`)
58+
} catch (e) {
59+
// Domain添加错误,不影响主流程,继续尝试后面的工作
60+
console.log(`Add domain error`)
61+
console.log(`Trying to deploy ...`)
62+
}
63+
break
64+
}
65+
recordReleaseList = recordReleaseList['data']
66+
if (recordReleaseList['records']) {
67+
for (let i = 0; i < recordReleaseList['records'].length; i++) {
68+
recordRelease.push({
69+
domain: inputs.domain,
70+
subDomain: recordReleaseList['records'][i].name,
71+
recordType: recordReleaseList['records'][i].type,
72+
value: recordReleaseList['records'][i].value,
73+
recordId: recordReleaseList['records'][i].id,
74+
mx: recordReleaseList['records'][i].mx,
75+
ttl: recordReleaseList['records'][i].ttl,
76+
recordLine: recordReleaseList['records'][i].line
77+
})
78+
}
79+
domainLength = recordReleaseList['records'].length
80+
} else {
81+
domainLength = 0
82+
}
83+
offset = offset + 1
84+
}
85+
console.log(`Getted release domain.`)
86+
} catch (e) {}
87+
88+
const records = []
89+
for (let recordNum = 0; recordNum < inputs.records.length; recordNum++) {
90+
const tempSubDomain =
91+
typeof inputs.records[recordNum].subDomain == 'string'
92+
? [inputs.records[recordNum].subDomain]
93+
: inputs.records[recordNum].subDomain
94+
const tempRecordLine =
95+
typeof inputs.records[recordNum].recordLine == 'string'
96+
? [inputs.records[recordNum].recordLine]
97+
: inputs.records[recordNum].recordLine
98+
99+
for (let subDomainNum = 0; subDomainNum < tempSubDomain.length; subDomainNum++) {
100+
for (let recordLineNum = 0; recordLineNum < tempRecordLine.length; recordLineNum++) {
101+
const tempRecord = JSON.parse(JSON.stringify(inputs.records[recordNum]))
102+
tempRecord.subDomain = tempSubDomain[subDomainNum]
103+
tempRecord.recordLine = tempRecordLine[recordLineNum]
104+
records.push(tempRecord)
105+
}
106+
}
107+
}
108+
109+
// 增加/修改记录
110+
console.log(`Doing action about domain records ... `)
111+
for (let recordNum = 0; recordNum < records.length; recordNum++) {
112+
const tempInputs = JSON.parse(JSON.stringify(records[recordNum]))
113+
tempInputs.domain = inputs.domain
114+
tempInputs.Region = this.region
115+
if (!tempInputs.status) {
116+
tempInputs.status = 'enable' // 设置默认值
117+
}
118+
console.log(`Resolving ${tempInputs.subDomain} - ${tempInputs.value}`)
119+
const releseHistory = this.haveRecord(tempInputs, recordRelease)
120+
if (tempInputs.recordId || releseHistory) {
121+
// 修改
122+
if (!tempInputs.recordId) {
123+
tempInputs.recordId = releseHistory.recordId
124+
}
125+
tempInputs.recordId = Number(tempInputs.recordId)
126+
console.log(`Modifying (recordId is ${tempInputs.recordId})... `)
127+
tempInputs.Action = 'RecordModify'
128+
try {
129+
const modifyResult = await this.cnsClient.request(tempInputs)
130+
if (modifyResult.code != 0) {
131+
throw new Error(JSON.stringify(modifyResult))
132+
}
133+
} catch (e) {
134+
throw e
135+
}
136+
console.log(`Modified (recordId is ${tempInputs.recordId}) `)
137+
} else {
138+
// 新建
139+
console.log(`Creating ... `)
140+
tempInputs.Action = 'RecordCreate'
141+
try {
142+
let createOutputs = await this.cnsClient.request(tempInputs)
143+
if (createOutputs.code != 0) {
144+
throw new Error(JSON.stringify(createOutputs))
145+
}
146+
createOutputs = createOutputs['data']
147+
tempInputs.recordId = createOutputs.record.id
148+
} catch (e) {
149+
throw e
150+
}
151+
console.log(`Created (recordId is ${tempInputs.recordId}) `)
152+
}
153+
recordList.push(tempInputs)
154+
output.records.push({
155+
subDomain: tempInputs.subDomain,
156+
recordType: tempInputs.recordType,
157+
recordLine: tempInputs.recordLine,
158+
recordId: tempInputs.recordId,
159+
value: tempInputs.value,
160+
status: tempInputs.status,
161+
domain: inputs.domain
162+
})
163+
// 改状态
164+
console.log(`Modifying status to ${tempInputs.status} `)
165+
const statusInputs = {
166+
Action: 'RecordStatus',
167+
Region: this.region,
168+
domain: inputs.domain,
169+
recordId: tempInputs.recordId,
170+
status: tempInputs.status
171+
}
172+
try {
173+
const statusResult = await this.cnsClient.request(statusInputs)
174+
if (statusResult.code != 0) {
175+
throw new Error(JSON.stringify(statusResult))
176+
}
177+
} catch (e) {
178+
throw e
179+
}
180+
console.log(`Modified status to ${tempInputs.status} `)
181+
}
182+
return output
183+
}
184+
185+
async remove(inputs = {}) {
186+
187+
const deleteList = inputs.deleteList || []
188+
189+
if (deleteList.length > 0) {
190+
console.log(
191+
`Deleting records which deployed by this project, but not in this records list. `
192+
)
193+
for (let recordNum = 0; recordNum < deleteList.length; recordNum++) {
194+
console.log(
195+
`Deleting record ${deleteList[recordNum].subDomain} ${deleteList[recordNum].recordId} `
196+
)
197+
const deleteInputs = {
198+
Action: 'RecordDelete',
199+
Region: this.region,
200+
domain: deleteList[recordNum].domain,
201+
recordId: deleteList[recordNum].recordId
202+
}
203+
try {
204+
const deleteResult = await this.cnsClient.request(deleteInputs)
205+
if (deleteResult.code != 0) {
206+
throw new Error(JSON.stringify(deleteResult))
207+
}
208+
} catch (e) {
209+
throw e
210+
}
211+
console.log(
212+
`Deleted record ${deleteList[recordNum].subDomain} ${deleteList[recordNum].recordId} `
213+
)
214+
}
215+
}
216+
return true
217+
}
218+
}
219+
220+
module.exports = CnsUtils

src/baas/cns/index.test.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
const secret = require('../../../../secret')
2+
const CnsUtils = require('./index')
3+
4+
class ClientTest {
5+
async cnsTest() {
6+
const cns = new CnsUtils({
7+
SecretId: secret.SecretId,
8+
SecretKey: secret.SecretKey
9+
})
10+
const cnsDemo = {
11+
domain: 'anycodes.cn',
12+
records: [
13+
{
14+
subDomain: ['abc', 'cde'],
15+
recordType: 'CNAME',
16+
recordLine: ['移动', '电信'],
17+
value: 'cname1.dnspod.com',
18+
ttl: 600,
19+
mx: 10,
20+
status: 'enable'
21+
},
22+
{
23+
subDomain: 'xyz',
24+
recordType: 'CNAME',
25+
recordLine: '默认',
26+
value: 'cname2.dnspod.com',
27+
ttl: 600,
28+
mx: 10,
29+
status: 'enable'
30+
}
31+
]
32+
}
33+
const result = await cns.deploy(cnsDemo)
34+
console.log(JSON.stringify(result))
35+
console.log(result)
36+
console.log(await cns.remove({ deleteList: result.records }))
37+
}
38+
}
39+
40+
new ClientTest().cnsTest()
41+
42+
/* 测试结果:
43+
Getting release domain records ...
44+
Getted release domain.
45+
Doing action about domain records ...
46+
Resolving abc - cname1.dnspod.com
47+
Creating ...
48+
Created (recordId is 562263334)
49+
Modifying status to enable
50+
Modified status to enable
51+
Resolving abc - cname1.dnspod.com
52+
Creating ...
53+
Created (recordId is 562263340)
54+
Modifying status to enable
55+
Modified status to enable
56+
Resolving cde - cname1.dnspod.com
57+
Creating ...
58+
Created (recordId is 562263381)
59+
Modifying status to enable
60+
Modified status to enable
61+
Resolving cde - cname1.dnspod.com
62+
Creating ...
63+
Created (recordId is 562263465)
64+
Modifying status to enable
65+
Modified status to enable
66+
Resolving xyz - cname2.dnspod.com
67+
Creating ...
68+
Created (recordId is 562263473)
69+
Modifying status to enable
70+
Modified status to enable
71+
{"logs":{"records":[{"subDomain":"abc","recordType":"CNAME","recordLine":"移动","recordId":"562263334","value":"cname1.dnspod.com.","status":"enable","domain":"anycodes.cn"},{"subDomain":"abc","recordType":"CNAME","recordLine":"电信","recordId":"562263340","value":"cname1.dnspod.com.","status":"enable","domain":"anycodes.cn"},{"subDomain":"cde","recordType":"CNAME","recordLine":"移动","recordId":"562263381","value":"cname1.dnspod.com.","status":"enable","domain":"anycodes.cn"},{"subDomain":"cde","recordType":"CNAME","recordLine":"电信","recordId":"562263465","value":"cname1.dnspod.com.","status":"enable","domain":"anycodes.cn"},{"subDomain":"xyz","recordType":"CNAME","recordLine":"默认","recordId":"562263473","value":"cname2.dnspod.com.","status":"enable","domain":"anycodes.cn"}]},"error":[]}
72+
{
73+
logs: { records: [ [Object], [Object], [Object], [Object], [Object] ] },
74+
error: []
75+
}
76+
Deleting records which deployed by this project, but not in this records list.
77+
Deleting record abc 562263334
78+
Deleted record abc 562263334
79+
Deleting record abc 562263340
80+
Deleted record abc 562263340
81+
Deleting record cde 562263381
82+
Deleted record cde 562263381
83+
Deleting record cde 562263465
84+
Deleted record cde 562263465
85+
Deleting record xyz 562263473
86+
Deleted record xyz 562263473
87+
{ logs: {}, error: [] }
88+
*/

0 commit comments

Comments
 (0)