-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
63 lines (53 loc) · 2.09 KB
/
main.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
const {
validateConfiguration,
getPublicIp,
getToken,
listDnsZone,
listARecords,
createUpdateRecord,
} = require('./helpers');
async function main() {
const config = await validateConfiguration({
tenantId: process.env.AZURERM_TENANT_ID || process.env.ARM_TENANT_ID || process.env.TENANT_ID || undefined,
clientId: process.env.AZURERM_CLIENT_ID || process.env.ARM_CLIENT_ID || process.env.CLIENT_ID || undefined,
clientSecret: process.env.AZURERM_CLIENT_SECRET || process.env.ARM_CLIENT_SECRET || process.env.CLIENT_SECRET || undefined,
subscriptionId: process.env.SUBSCRIPTION_ID,
resourceGroupName: process.env.RESOURCE_GROUP_NAME,
dnsZone: process.env.DNS_ZONE,
record: process.env.RECORD // @ or abc
});
const token = await getToken(config.tenantId, config.clientId, config.clientSecret);
const zones = await listDnsZone(token, config.subscriptionId, config.resourceGroupName);
if (!zones.includes(config.dnsZone)) {
console.error(`Error - DNS zone [${config.dnsZone}] not found in resource group [${config.resourceGroupName}]`);
process.exit(1);
}
const publicIp = await getPublicIp();
const records = await listARecords(
token,
config.subscriptionId,
config.resourceGroupName,
config.dnsZone
)
const [ record ] = records.value.filter(({ name }) => name === config.record);
if (record && record.properties.ARecords.some(record => record.ipv4Address === publicIp)) {
console.log(`No Change - Domain: ${config.record === '@' ? config.dnsZone :
`${config.record}.${config.dnsZone}`} IP: ${publicIp}`);
process.exit(0);
}
const zone = await createUpdateRecord(
token,
config.subscriptionId,
config.resourceGroupName,
config.dnsZone,
config.record,
publicIp
);
if (!zone.properties.provisioningState === 'Succeeded') {
console.error(`Error - DNS zone [${config.dnsZone}] could not be updated with [${publicIp}]`);
process.exit(1);
}
console.log(`Updated - Domain: ${config.record === '@' ? config.dnsZone :
`${config.record}.${config.dnsZone}`} IP: ${publicIp}`);
}
module.exports = main;