-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroute53.js
86 lines (81 loc) · 2.36 KB
/
route53.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var Route53 = require('nice-route53')
var r53 = new Route53({
accessKeyId: process.env.aws_access_key_id,
secretAccessKey: process.env.aws_secret_access_key
})
function getZoneName(zoneid) {
return new Promise((resolve, reject) => {
r53.zoneInfo(zoneid, function (err, zoneInfo) {
if (err) {
resolve(err)
} else {
console.log(zoneInfo.name)
resolve(zoneInfo.name)
}
})
})
}
module.exports.getRecords = function getRecords(zoneid) {
return new Promise((resolve, reject) => {
r53.records(zoneid, function (err, records) {
if (err) {
resolve(err)
} else {
var recordList = []
for (record of records) {
var addme = {}
if (record.name.match(/zt/)) {
addme.name = record.name.split('.')[0]
addme.ip = record.values[0]
recordList.push(addme)
}
}
resolve(recordList)
}
})
})
}
module.exports.addRecord = function addRecord(memberName, ip) {
return new Promise(async (resolve, reject) => {
var domainName = await getZoneName(process.env.hosted_zone)
var args = {
zoneId: process.env.hosted_zone,
name: `${memberName}.zt.${domainName}`,
type: 'A',
ttl: 300,
values: [
ip,
]
}
// console.log(args)
r53.upsertRecord(args, function (err, res) {
if (err) {
resolve(err)
} else {
resolve(res)
}
})
})
}
module.exports.delRecord = function delRecord(memberName, ip) {
return new Promise(async (resolve, reject) => {
var domainName = await getZoneName(process.env.hosted_zone)
var args = {
zoneId: process.env.hosted_zone,
name: `${memberName}.zt.${domainName}`,
type: 'A',
ttl: 300,
values: [
ip,
]
}
// console.log(args)
r53.delRecord(args, function (err, res) {
if (err) {
resolve(err)
} else {
resolve(res)
}
})
})
}