-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliyun_client.js
76 lines (67 loc) · 1.85 KB
/
aliyun_client.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
const Core = require('@alicloud/pop-core');
class BreakSignal {
}
class AliYunClient {
constructor(options) {
this.options = options;
this.client = new Core({
accessKeyId: options.accessKeyId,
accessKeySecret: options.accessKeySecret,
endpoint: 'https://alidns.aliyuncs.com',
apiVersion: '2015-01-09'
});
this.domainParams = {
DomainName: options.DomainName,
RR: options.RR,
Type: 'A',
Value: undefined
};
this.requestOption = {
method: 'POST'
};
}
req(action, p) {
return this.client.request(action, { ...this.domainParams, ...p }, this.requestOption);
}
addDomainRecord(ip) {
return this.req('AddDomainRecord', { Value: ip });
}
describeSubDomainRecords() {
return this.req('DescribeSubDomainRecords', { SubDomain: `${this.domainParams.RR}.${this.domainParams.DomainName}` });
}
deleteDomainRecord(RecordId) {
return this.req('DeleteDomainRecord', { RecordId });
}
addOrUpdateDomainRecord(ip) {
return this.describeSubDomainRecords()
.then((result) => {
let sameRecordExist = false;
result.DomainRecords.Record.forEach((record) => {
if (record.Value === ip) {
console.info(`IP existing on current DNS configuration`);
sameRecordExist = true;
} else {
// delete all the unmatched records
console.info(`delete record: `, record);
return this.deleteDomainRecord(record.RecordId);
}
});
if (sameRecordExist) {
throw new BreakSignal();
}
})
.then(() => {
return this.addDomainRecord(ip);
})
.catch((err) => {
if (err instanceof BreakSignal) {
// normal finish the operation
} else {
throw err;
}
});
}
}
module.exports = {
AliYunClient
}