-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathremoveRecordsCloudflareScript.js
60 lines (55 loc) · 2.01 KB
/
removeRecordsCloudflareScript.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
/* eslint-disable camelcase */
/* eslint-disable no-restricted-syntax */
const axios = require('axios');
const qs = require('qs');
const config = require('config');
const cloudFlareAxiosConfig = {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${config.cloudflare.apiKey}`,
},
};
async function listDNSRecords(name, content, type = 'CNAME', page = 1, per_page = 100, records = []) {
// https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records
const query = {
name,
content,
type,
page,
per_page,
};
const queryString = qs.stringify(query);
const url = `${config.cloudflare.endpoint}zones/${config.cloudflare.zone}/dns_records?${queryString}`;
const response = await axios.get(url, cloudFlareAxiosConfig);
if (response.data.result_info.total_pages > page) {
const recs = records.concat(response.data.result);
return listDNSRecords(name, content, type, page + 1, per_page, recs);
}
const r = records.concat(response.data.result);
return r;
}
// throw error above
async function deleteDNSRecord(id) {
if (!id) {
throw new Error('No DNS ID record specified');
}
// https://api.cloudflare.com/#dns-records-for-a-zone-delete-dns-record
const url = `${config.cloudflare.endpoint}zones/${config.cloudflare.zone}/dns_records/${id}`;
const response = await axios.delete(url, cloudFlareAxiosConfig);
return response.data;
}
async function removeObsoleteRecords() {
const dnsRecords = await listDNSRecords();
// console.log(JSON.stringify(dnsRecords));
// console.log(dnsRecords);
// delete bad
for (const record of dnsRecords) { // async inside
// console.log(record);
if (record.name.endsWith('.app.runonflux.io') && record.name.length >= '13.app.runonflux.io'.length && record.content === 'fdm-lb-1.runonflux.io') {
// eslint-disable-next-line no-await-in-loop
await deleteDNSRecord(record.id); // may throw
console.info(`Record ${record.name} on ${record.content} deleted`);
}
}
}
removeObsoleteRecords();