Last active
January 2, 2016 04:49
-
-
Save chmouel/8252918 to your computer and use it in GitHub Desktop.
Export RAX domain
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
#!/usr/bin/env python | |
"""Simple script to export rackspace cloud dns | |
Usage: python export-domain.py domain | |
You need to have the python-clouddns package install from github : | |
https://github.com/rackerlabs/python-clouddns | |
You need to have the env varibale defined with : | |
OS_USERNAME="rackspace_username" | |
OS_PASSWORD="rackpsace_key" | |
if you connect to the rax uk cloud export this variable : | |
RCLOUD_REGION="UK" | |
""" | |
import argparse | |
import json | |
import os | |
import sys | |
import time | |
import clouddns | |
import requests | |
#Need to setup to your region | |
REGION = os.environ.get("RCLOUD_REGION", "UK") | |
USER = os.environ.get("OS_USERNAME") | |
KEY = os.environ.get("OS_PASSWORD") | |
TTL = 300 | |
if not (KEY or not USER): | |
print("You need to have OS_USERNAME or OS_PASSWORD defined") | |
sys.exit(1) | |
class InvalidRegion(Exception): | |
def __init__(self, region): | |
self.region = region | |
def __str__(self): | |
return "InvalidRegion: %s" % repr(self.region) | |
def auth(): | |
authurl = clouddns.consts.default_authurl | |
if REGION and REGION.lower() == "uk": | |
authurl = clouddns.consts.uk_authurl | |
elif REGION: | |
raise InvalidRegion(REGION) | |
return clouddns.connection.Connection( | |
USER, KEY, authurl=authurl) | |
parser = argparse.ArgumentParser() | |
parser.add_argument('domain') | |
args = parser.parse_args() | |
cnx = auth() | |
all_domains = cnx.get_domains() | |
ourdomain = None | |
for x in all_domains: | |
if str(x) == args.domain: | |
ourdomain = x | |
if not ourdomain: | |
print("Cannot find our domain:") | |
sys.exit(1) | |
# print("curl -H 'x-auth-token: %s' " | |
# "https://dns.api.rackspacecloud.com/%s/domains/%s/export" % ( | |
# cnx.token, ourdomain.id, cnx.uri)) | |
export = json.loads(ourdomain.export()) | |
headers = {'x-auth-token': cnx.token} | |
r = None | |
json = None | |
for _ in xrange(100): | |
r = requests.get(export["callbackUrl"] + "?showDetails=True", | |
headers=headers) | |
json = r.json() | |
if json['status'] == 'COMPLETED': | |
break | |
time.sleep(2) | |
print(json['response']['contents']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment