diff --git a/sample.py b/sample.py index 84e6655..cb7a501 100644 --- a/sample.py +++ b/sample.py @@ -96,5 +96,8 @@ #delete task status print 'delete task status: %s ' % c.clear_task(result['task_id']) +#export zonefile in bind format +print('export zone: %s ' % c.export_zone('sample.client.me.')) + #delete the zone print 'delete zone: %s ' % c.delete_zone('sample.client.me.') diff --git a/setup.py b/setup.py index d0ec0ba..3c8e0e7 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='ultra_rest_client', - version='0.2.1', + version='0.2.2', description='A sample Python client for communicating with the UltraDNS REST API', url='https://github.com/ultradns/python_rest_api_client', author='Jon Bodner', diff --git a/test.py b/test.py index 69c81c2..d49e69b 100644 --- a/test.py +++ b/test.py @@ -80,5 +80,8 @@ #delete task status print('delete task status: %s ' % c.clear_task(result['task_id'])) +#export zonefile in bind format +print('export zone: %s ' % c.export_zone('sample.client.me.')) + #delete the zone print('delete zone: %s ' % c.delete_zone('sample.client.me.')) diff --git a/ultra_rest_client/connection.py b/ultra_rest_client/connection.py index 2523f7e..4685e24 100644 --- a/ultra_rest_client/connection.py +++ b/ultra_rest_client/connection.py @@ -105,6 +105,9 @@ def _do_call(self, uri, method, params=None, body=None, retry=True, files=None, # body = {"errorCode":60001,"errorMessage":"invalid_grant:token not found, expired or invalid"} if r1.status_code == requests.codes.NO_CONTENT: return {} + # if the content-type is text/plain just return the text + if r1.headers['Content-Type'] == 'text/plain': + return r1.text json_body = r1.json() # if this is a background task, add the task id to the body if r1.status_code == requests.codes.ACCEPTED: diff --git a/ultra_rest_client/ultra_rest_client.py b/ultra_rest_client/ultra_rest_client.py index ad7c6e5..c20a27f 100644 --- a/ultra_rest_client/ultra_rest_client.py +++ b/ultra_rest_client/ultra_rest_client.py @@ -6,7 +6,7 @@ __author__ = 'Jon Bodner' from .connection import RestApiConnection import json - +import time class RestApiClient: def __init__(self, username, password, use_http=False, host="restapi.ultradns.com"): @@ -735,6 +735,26 @@ def edit_tc_pool(self, zone_name, owner_name, ttl, pool_info, rdata_info, backup rrset = self._build_tc_rrset(backup_record, pool_info, rdata_info, ttl) return self.rest_api_connection.put("/v1/zones/" + zone_name + "/rrsets/A/" + owner_name, json.dumps(rrset)) + # export zone in bind format + def export_zone(self, zone_name): + """Returns a zone file in bind format + + Arguments: + zone_name -- The name of the zone being returned. A single zone as a string. + + """ + zonelist = [zone_name] + zonejson=json.dumps({'zoneNames': zonelist}) + status = self.rest_api_connection.post("/v3/zones/export", zonejson) + taskId = status.get('task_id') + while True: + task_status = self.rest_api_connection.get("/v1/tasks/"+taskId) + if task_status['code'] != 'IN_PROCESS': + break + time.sleep(1) + result=self.rest_api_connection.get("/v1/tasks/"+taskId+"/result") + self.clear_task(taskId) + return result def build_params(q, args): params = {}