This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.js
128 lines (124 loc) · 3.91 KB
/
cli.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
function getResourceName (recordDetails) {
return `${recordDetails.name.replace(new RegExp('\\.', 'g'), '_')}_${recordDetails.type}_${recordDetails.id}`
}
function formatAsTf (records) {
const output = {
'resource': {
'cloudflare_record': {
}
}
}
for (let recordDetails of records) {
const tfDetails = {
'domain': recordDetails.zone_name,
'value': recordDetails.content,
'type': recordDetails.type,
'proxied': recordDetails.proxied
}
if (recordDetails.name === recordDetails.zone_name) {
tfDetails['name'] = '@'
} else {
tfDetails['name'] = recordDetails.name.replace(new RegExp(`.${recordDetails.zone_name}$$`), '')
}
if (recordDetails.hasOwnProperty('priority')) {
tfDetails['priority'] = recordDetails.priority
}
if (recordDetails.ttl !== 1) { // Cloudflare API returns ttl of 1 when set to automatic ttl
tfDetails['ttl'] = recordDetails.ttl
}
output['resource']['cloudflare_record'][getResourceName(recordDetails)] = tfDetails
}
return output
}
function formatAsTfstate (records) {
const output = {
'version': 1,
'serial': 1,
'modules': [
{
'path': [
'root'
],
'outputs': {},
'resources': {},
'depends_on': []
}
]
}
for (let recordDetails of records) {
const tfstateDetails = {
'type': 'cloudflare_record',
'depends_on': [],
'primary': {
'id': recordDetails.id,
'attributes': {
'domain': recordDetails.zone_name,
'id': recordDetails.id,
'hostname': recordDetails.name,
'priority': '0',
'proxied': recordDetails.proxied.toString(),
'ttl': recordDetails.ttl.toString(),
'type': recordDetails.type,
'value': recordDetails.content,
'zone_id': recordDetails.zone_id
},
'meta': {
'schema_version': '1'
},
'tainted': false
},
'deposed': [],
'provider': ''
}
if (recordDetails.hasOwnProperty('priority')) {
tfstateDetails['primary']['attributes']['priority'] = recordDetails.priority.toString()
}
if (recordDetails.name === recordDetails.zone_name) {
tfstateDetails['primary']['attributes']['name'] = '@'
} else {
tfstateDetails['primary']['attributes']['name'] = recordDetails.name.replace(new RegExp(`.${recordDetails.zone_name}$$`), '')
}
output['modules'][0]['resources'][`cloudflare_record.${getResourceName(recordDetails)}`] = tfstateDetails
}
return output
}
require('yargs') // eslint-disable-line
.command('dns', 'import dns records', (yargs) => {
yargs.option('zone', {
describe: 'limit to a particular zone by name',
default: ''
})
}, (argv) => {
if (!(process.env.CLOUDFLARE_EMAIL && process.env.CLOUDFLARE_TOKEN)) {
console.log('You must define both CLOUDFLARE_EMAIL and CLOUDFLARE_TOKEN as env vars')
return
}
const cf = require('cloudflare')({
email: process.env.CLOUDFLARE_EMAIL,
key: process.env.CLOUDFLARE_TOKEN
})
cf.zones.browse().then(function (zones) {
for (let zoneDetails of zones.result) {
if (!argv.zone || zoneDetails.name === argv.zone) {
cf.dnsRecords.browse(zoneDetails.id).then(async function (firstPage) {
let records = firstPage.result
for (let i = 2; i <= firstPage.result_info.total_pages; i++) {
const page = await cf.dnsRecords.browse(zoneDetails.id, {'page': i})
records = records.concat(page.result)
}
if (argv.tfstate) {
console.log(JSON.stringify(formatAsTfstate(records), null, 4))
} else {
console.log(JSON.stringify(formatAsTf(records), null, 4))
}
})
}
}
})
})
.help('help')
.option('tfstate', {
describe: 'output tfstate',
default: false
})
.argv