#!/usr/bin/env perl # bind2tfcf.pl # Converts DNS info from BIND zone file format to Terraform Cloudflare provider configuration # by Jon Jensen # 2018-10-24 # # This is free and unencumbered software released into the public domain. # For more information see https://unlicense.org/ use strict; use warnings; my $domain = $ARGV[0] or die "Supply domain name as argument\n"; my %label_counts; while () { next unless /\S/; chomp; my ($name, $type, $value) = split /\s+/, $_, 3; unless ($type =~ /^(?:a|aaaa|caa|cname|ns)$/i) { warn "Can't yet handle this type of record: $_\n"; next; } my $label = $name; $label = '__bare' if !length($label // ''); $label =~ s/\*/__star/g; $label =~ s/[^\w-]/_/g; $label .= '_' . lc($type); $label .= '_' . $label_counts{$label} if ++$label_counts{$label} > 1; $name =~ s/\.$//; if (!length($name // '') or $name eq $domain) { $name = '${var.domain}'; } elsif ($name !~ /\./) { $name .= '.${var.domain}'; } my $data; if ($type =~ /^(?:cname|ns)$/i) { $value =~ s/\.$//; if (!length($value // '') or $value eq $domain) { $value = '${var.domain}'; } elsif ($value !~ /\./) { $value .= '.${var.domain}'; } } elsif ($type =~ /^caa$/i) { my ($caa_flag, $caa_type, $caa_value) = split /\s+/, $value, 3; $caa_value =~ s/^"//; $caa_value =~ s/"$//; $data = <