Created
November 15, 2017 13:35
-
-
Save maxpeterson/d5a6fff3d944d5b5057c6c6ed5f30e10 to your computer and use it in GitHub Desktop.
Export DNS bind 9 zone data for all domains in a Rackspace account.
This file contains hidden or 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 os | |
import time | |
import requests | |
def get_tokens(username, apikey): | |
# Get the auth token | |
auth_data = { | |
"auth": { | |
"RAX-KSKEY:apiKeyCredentials": { | |
"username": username, | |
"apiKey": apikey | |
} | |
} | |
} | |
r = requests.post('https://identity.api.rackspacecloud.com/v2.0/tokens', json=auth_data) | |
return r.json()['access'] | |
def get_auth_headers(username, apikey): | |
tokens = get_tokens(username, apikey) | |
auth_token = tokens['token']['id'] | |
return {'x-auth-token': auth_token} | |
def get_domain(account_id, domain_id, headers): | |
url = 'https://dns.api.rackspacecloud.com/v1.0/{}/domains/{}/export'.format(account_id, domain_id) | |
r = requests.get(url, headers=headers) | |
jobId = r.json()['jobId'] | |
print('Waiting for task {} to complete'.format(jobId)) | |
status = None | |
while True: | |
r = requests.get(r.json()['callbackUrl'] + '?showDetails=True', headers=headers) | |
status = r.json()['status'] | |
if status in ['COMPLETED', 'FAILED']: | |
break | |
print('{0}\r'.format(status)) | |
time.sleep(1) | |
return r.json()['response']['contents'] | |
def get_dns(account_id, username, apikey, directory='./zones'): | |
""" | |
Export DNS bind 9 zone data for all domains in an account. | |
get_dns(account_id, username, apikey) | |
""" | |
url = 'https://dns.api.rackspacecloud.com/v1.0/{}/domains'.format(account_id) | |
headers = get_auth_headers(username, apikey) | |
domains = requests.get(url, headers=headers) | |
directory = os.path.expanduser(directory) | |
if not os.path.exists(directory): | |
os.makedirs(directory) | |
for domain in domains.json()['domains']: | |
domain_id = domain['id'] | |
domain_name = domain['name'] | |
print('Get zone info for {} ({})'.format(domain_name, domain_id)) | |
contents = get_domain(account_id, domain_id, headers) | |
print('Zone info for {} ({})'.format(domain_name, domain_id)) | |
print(contents) | |
filename = os.path.join(directory, "%s.bind9" % domain_name) | |
print('Writing info to {}'.format(filename)) | |
with open(filename, 'w') as fd: | |
fd.write(contents) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment