Skip to content

Instantly share code, notes, and snippets.

@ipmb
Last active April 24, 2018 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipmb/3a4714452a6ebc067128c306970f207e to your computer and use it in GitHub Desktop.
Save ipmb/3a4714452a6ebc067128c306970f207e to your computer and use it in GitHub Desktop.
DNS zonefile -> terraform dyn
from blockstack_zones import parse_zone_file
with open('/path/to/your/zonefile') as f:
full = f.read()
ZONE = 'your-domain.com'
record_template = """
resource "dyn_record" "{slug}" {{
zone = "{zone}"
name = "{name}"
value = "{value}"
type = "{type_}"
ttl = "{ttl}"
}}
"""
def _print_record(record, type_):
if 'ttl' not in record:
record['ttl'] = record['name']
record['name'] = '@'
name = record['name'] if record['name'] != '@' else ''
if subdomain:
name += '.' + subdomain
name = name.strip('.')
slug = zone.replace('.', '_')
if name:
slug = '{}_{}'.format(name.replace('.', '_'), slug)
slug = slug.replace('\*', 'star')
if type_ == 'A':
value = record['ip']
elif type_ == 'CNAME':
value = record['alias']
elif type_ == 'TXT':
value = record['txt']
elif type_ == 'NS':
value = record['host']
elif type_ == 'MX':
value = ' '.join([record['preference'], record['host']])
print(record_template.format(slug=slug, zone=zone, name=name, value=value, type_=type_, ttl=record['ttl']))
for origin in full.split('\n\n'):
records = parse_zone_file(origin)
print('## {} ##\n'.format(records['$origin']))
subdomain = records['$origin'].replace(ZONE, '')
types = ['a', 'txt', 'cname', 'ns', 'mx']
for t in types:
for r in records[t]:
_print_record(r, t.upper())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment