Commit 010ded9

Dan Krause <dan.krause@rackspace.com>
2013-05-16 09:08:48
added exporting functionality to hoverimport.py and renamed it to bulkhover.py
1 parent 6617ded
hoverimport.py → bulkhover.py
@@ -6,9 +6,9 @@ import optparse
 import sys
 
 
-# This is a command-line script to import DNS records for a single domain
-# into a hover account. Run it like so:
-# ./dynhover.py -u USERNAME -p PASSWORD DNS_FILE DOMAIN
+# This is a command-line script to import and export DNS records for a
+# single domain into or out of a hover account. Run it like so:
+# ./bulkhover.py -u USERNAME -p PASSWORD (import|export) DOMAIN DNS_FILE
 # or create a config file like this:
 #
 # [hover]
@@ -16,7 +16,7 @@ import sys
 # password=PASSWORD
 #
 # and run it like this:
-# ./dynhover.py -c PATH_TO_CONF DNS_FILE DOMAIN
+# ./bulkhover.py -c PATH_TO_CONF (import|export) DOMAIN DNS_FILE
 #
 # The DNS file should have one record per line, in the format:
 # {name} {type} {content}
@@ -25,6 +25,9 @@ import sys
 #
 # www A 127.0.0.1
 # @ MX 10 example.com
+# 
+# You can even copy the entire contents of one domain to another, like so:
+# ./bulkhover.py -c CONF export example.com - | ./bulkhover.py -c CONF -f import other.com -
 
 
 class HoverException(Exception):
@@ -63,6 +66,7 @@ def import_dns(username, password, domain, filename, flush=False):
     
     domain_id = client.call("get", "domains/{0}".format(domain))["domain"]["id"]
     
+    if filename == "-": filename = "/dev/stdin"
     with open(filename, "r") as f:
         for line in f:
             parts = line.strip().split(" ", 2)
@@ -70,6 +74,18 @@ def import_dns(username, password, domain, filename, flush=False):
             client.call("post", "domains/{0}/dns".format(domain), record)
             print "Created {name} {type} {content}".format(**record)
 
+def export_dns(username, password, domain, filename):
+    try:
+        client = HoverAPI(username, password)
+    except HoverException as e:
+        raise HoverException("Authentication failed")
+    records = client.call("get", "domains/{0}/dns".format(domain))["domains"][0]["entries"]
+    
+    if filename == "-": filename = "/dev/stdout"
+    with open(filename, "w") as f:
+        for record in records:
+            f.write("{name} {type} {content}\n".format(**record))
+    
 
 def main():
     usage = "usage: %prog (-c CONF|-u USERNAME -p PASSWORD) DOMAIN FILE"
@@ -81,10 +97,13 @@ def main():
     parser.add_option("-f", "--flush", default=False, action="store_true", help="Flush all DNS records associated with the domain before importing")
     (options, args) = parser.parse_args()
     
-    if len(args) < 2:
-        parser.error("You must specify both a domain, and a file to import")
+    if len(args) < 3:
+        parser.error("You must specify an operation, a domain, and a file")
+    
+    operation, domain, filename = args
     
-    domain, filename = args
+    if operation not in ("import", "export"):
+        parser.error("Invalid operation: {0} - Valid operations are import and export".format(operation))
     
     def get_conf(filename):
         config = ConfigParser.ConfigParser()
@@ -100,7 +119,10 @@ def main():
     else:
         username, password = get_conf(options.conf)
 
-    import_dns(username, password, domain, filename, options.flush)
+    if operation == "import":
+        import_dns(username, password, domain, filename, options.flush)
+    elif operation == "export":
+        export_dns(username, password, domain, filename)
 
 
 if __name__ == "__main__":
dynhover.py