Created
February 1, 2019 20:32
-
-
Save bzcorn/f05f3dafd01a507bd6b8082d4d6dc39c to your computer and use it in GitHub Desktop.
Simple script to extract data from ultradns to route53
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 ultra_rest_client | |
import boto3 | |
from botocore.exceptions import ClientError | |
def create_record(rname, rtype, ttl, resource_record_list): | |
record = { | |
'Action': 'UPSERT', | |
'ResourceRecordSet': { | |
'Name': rname, | |
'Type': rtype, | |
'TTL': ttl, | |
'ResourceRecords': resource_record_list | |
} | |
} | |
return record | |
def resource_records(rrtype, rlist): | |
response = [] | |
records = rlist | |
for item in records: | |
# In case a record is longer than 255 characters split it into | |
# smaller pieces. | |
n = 255 | |
if len(item) > n: | |
replace = [item[i:i + n] for i in range(0, len(item), n)] | |
records.remove(item) | |
records += replace | |
rlist = records | |
for item in rlist: | |
item = item.replace('"', '') | |
if rrtype == "TXT": | |
response.append({'Value': f'"{item}"'}) | |
else: | |
response.append({'Value': item}) | |
return response | |
# Config settings for UltraDNS | |
username = '' | |
password = '' | |
use_http = 'False' | |
domain = 'restapi.ultradns.com' | |
get_domain = '' # The domain to pull records from | |
# Get data from Ultradns | |
c = ultra_rest_client.RestApiClient( | |
username, | |
password, | |
'True' == use_http, | |
domain) | |
account_details = c.get_account_details() | |
account_name = account_details[u'accounts'][0][u'accountName'] | |
rrsets = c.get_rrsets(get_domain, limit=500) | |
# Push data to Route 53 | |
hosted_zone_id = '' | |
client = boto3.client('route53') | |
crrs = [] | |
# Massage the data imported from Ultradns to be consumable by Route 53 | |
for record in rrsets['rrSets']: | |
rrtype = record['rrtype'].split(' ')[0] | |
crrs.append( | |
create_record( | |
record['ownerName'], | |
rrtype, | |
record['ttl'], | |
resource_records(rrtype, record['rdata']) | |
) | |
) | |
# Upload data to Route 53 | |
for i in range(0, len(crrs), 100): | |
try: | |
response = client.change_resource_record_sets( | |
HostedZoneId=hosted_zone_id, | |
ChangeBatch={ | |
'Changes': crrs[i:i + 100] | |
} | |
) | |
except ClientError as e: | |
print f'Error! boto3 says... {e}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment