Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save labeveryday/c5df8ffcc00b832ecb2fa9020fdd64dd to your computer and use it in GitHub Desktop.
Save labeveryday/c5df8ffcc00b832ecb2fa9020fdd64dd to your computer and use it in GitHub Desktop.
update-go daddy-nameservers.py
"""
This script takes Terraform name server output and updates the nameservers for a domain in Godaddy to match the nameservers from AWS.
"""
import os
import json
import subprocess
import requests
import datetime
from dotenv import load_dotenv
from pprint import pprint
load_dotenv()
DOMAIN = os.getenv('DOMAIN')
GODADDY_API_KEY = os.getenv('GODADDY_API_KEY')
GODADDY_API_SECRET = os.getenv('GODADDY_API_SECRET')
def main():
# AWS Nameservers from terraform output
terraform_aws_ns = subprocess.run(['terraform', 'output', '-json', 'web_app_1_ns'], stdout=subprocess.PIPE)
# Parse the output of terraform
aws_nameservers = json.loads(terraform_aws_ns.stdout)
# Get list of Godaddy nameservers
godaddy_ns = get_godaddy_dns()["nameServers"]
# Compare the nameservers
compare_nameservers(aws_nameservers, godaddy_ns)
# Validate the nameservers
if validate_nameservers(aws_nameservers):
print('Nameservers have been updated successfully')
else:
raise Exception('Nameservers were not updated successfully')
# Create a get request to the godaddy API
def get_godaddy_dns(domain: str = DOMAIN):
"""
Get the DNS records for a domain from Godaddy
Args:
domain (str): Domain name
Returns:
dict: DNS records for the domain
"""
url = f'https://api.godaddy.com/v1/domains/{domain}'
headers = {
'Authorization' : f'sso-key {GODADDY_API_KEY}:{GODADDY_API_SECRET}'
}
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception(f'Failed to get DNS records: {response.status_code}')
return response.json()
# Function that compares the nameservers from AWS and Godaddy
def compare_nameservers(aws_nameservers: list, godaddy_ns: list):
"""
Compare the nameservers from AWS and Godaddy.
If they are the same, print a message and return.
If they are different, update the nameservers in Godaddy.
Args:
aws_nameservers (list): Nameservers from AWS
godaddy_ns (list): Nameservers from Godaddy
"""
if aws_nameservers == godaddy_ns:
print('Nameservers are the same. Update not required.')
else:
print('Nameservers are different. Updating...')
update_godaddy_nameservers(aws_nameservers)
# Function that updates the nameservers in Godaddy
def update_godaddy_nameservers(nameservers: list, domain: str = DOMAIN):
"""
Update the nameservers for a domain in Godaddy
Args:
nameservers (list): List of nameservers
domain (str): Domain name
Returns:
dict: Response from Godaddy API
"""
public_ip = get_public_ip()
url = f'https://api.godaddy.com/api/v1/domains/{domain}'
headers = {
'Authorization ' : f'sso-key {GODADDY_API_KEY}:{GODADDY_API_SECRET}',
'Content-type' : 'application/json',
}
payload = {
"nameServers" : nameservers,
"consent": {
"agreedAt": datetime.datetime.now().isoformat(),
"agreedBy": public_ip,
"agreementKeys": []
}
}
response = requests.patch(url, headers=headers, json=payload)
if response.status_code != 200:
raise Exception(f'Failed to update DNS records: {response.status_code}')
return response.json()
# Function that validates the nameservers are the same
def validate_nameservers(nameservers: list, domain: str = DOMAIN):
"""
Validate that the nameservers for a domain are as expected
Args:
nameservers (list): List of nameservers
domain (str): Domain name
Returns:
bool: True if the nameservers are as expected, False otherwise
"""
current_nameservers = get_godaddy_dns(domain)['nameServers']
return current_nameservers == nameservers
def get_public_ip():
"""
Get the public IP address of the machine
Returns:
str: Public IP address
"""
response = requests.get('https://api.ipify.org')
return response.text
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment