Created
April 2, 2021 19:04
-
-
Save chancez/51d3a8bc9235576a9566d7c6cb0e17e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from os import getenv, path | |
from string import Template | |
from subprocess import run | |
from sys import exit | |
ZONE_ID = getenv("ZONE_ID") | |
ZONE_FILE = getenv("ZONE_FILE") | |
TERRAFORM_DIR = getenv("TERRAFORM_DIR") | |
OUTPUT_FILE = getenv("OUTPUT_FILE", "dummy.tf") | |
DRY_RUN = getenv("DRY_RUN", 'true').lower() == 'true' | |
# Returns the variable key if not present in ENV. | |
def check_env_vars(): | |
if not ZONE_FILE: | |
return "$ZONE_FILE" | |
if not ZONE_ID: | |
return "$ZONE_ID" | |
if not TERRAFORM_DIR: | |
return "$TERRAFORM_DIR" | |
if not OUTPUT_FILE: | |
return "$OUTPUT_FILE" | |
return "" | |
# Loads the zone records in a dict | |
def load_records(zone_file=ZONE_FILE): | |
with open(zone_file) as record_file: | |
data = json.load(record_file) | |
return data | |
# Writes the Terraform template which is required | |
# before `terraform import` runs. | |
def template_resource(resource_name, resource_type, record_name, resource_records, ttl): | |
records = [] | |
for rr in resource_records: | |
value = rr["Value"] | |
if resource_type == "TXT": | |
# remove double quotes from the ends | |
if value[0] == '"': | |
value = value[1:] | |
if value[-1] == '"': | |
value = value[:-1] | |
# escape double quotes from the inside | |
value = value.replace('"', '\\"') | |
records.append(' "'+value+'"') | |
records_str = ",\n".join(records) + "," | |
resource_template = Template( | |
""" | |
resource "aws_route53_record" "$resource_name" { | |
zone_id = "$zone_id" | |
type = "$resource_type" | |
name = "$record_name" | |
ttl = "$ttl" | |
records = [ | |
$records | |
] | |
} | |
""" | |
) | |
return resource_template.substitute( | |
zone_id=ZONE_ID, | |
resource_name=resource_name, | |
resource_type=resource_type, | |
record_name=record_name, | |
records=records_str, | |
ttl=ttl, | |
) | |
# Shells out `terraform import` command in the host OS. | |
def terraform_import_cmd(resource_name, resource_type, record_name): | |
return f'terraform import aws_route53_record.{resource_name} {ZONE_ID}_{record_name}_{resource_type}' | |
if __name__ == "__main__": | |
missing = check_env_vars() | |
if missing: | |
exit(f"Required env variable {missing} is missing.") | |
records = load_records() | |
resources = [] | |
import_cmds = [] | |
for i in records.get("ResourceRecordSets"): | |
resource_name = i.get("Name") | |
resource_type = i.get("Type") | |
resource_records = i.get("ResourceRecords") | |
ttl = i.get("TTL") | |
# replace \100 with @ | |
resource_name = resource_name.replace("\\100", "@") | |
record_name = resource_name | |
# remove trailing dot | |
if resource_name[-1] == '.': | |
resource_name = resource_name[:-1] | |
# replace dots with underscores | |
resource_name = resource_name.replace(".", "_") | |
# replace @ with at | |
resource_name = resource_name.replace("@", "at") | |
# append resource type to resource name since we can have multiple | |
# types of records for each record name | |
resource_name += "_" + resource_type.lower() | |
if not resource_name[0].isalpha(): | |
resource_name = "verification_" + resource_name | |
# Replace __ with _ | |
resource_name = resource_name.replace("__", "_") | |
resources.append(template_resource(resource_name, resource_type, record_name, resource_records, ttl)) | |
import_cmds.append(terraform_import_cmd(resource_name, resource_type, record_name)) | |
dummy_file_content = "".join(resources) | |
if DRY_RUN: | |
print(dummy_file_content) | |
# commands = "\n".join(import_cmds) | |
# print(commands) | |
else: | |
dummy_file_path = path.join(TERRAFORM_DIR, OUTPUT_FILE) | |
with open(dummy_file_path, "a") as f: | |
f.write(dummy_file_content) | |
for command in import_cmds: | |
print(command) | |
run(command, shell=True, check=True) | |
print(f"Imported {resource_name}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment