|
10 | 10 | import os
|
11 | 11 | import socket
|
12 | 12 | import consts
|
| 13 | +import time |
13 | 14 | from Queue import Queue, Empty, Full
|
14 | 15 | from errors import ResponseError
|
15 | 16 | from httplib import HTTPSConnection, HTTPConnection, HTTPException
|
16 | 17 | from sys import version_info
|
17 |
| -from time import time |
18 | 18 | from urllib import quote
|
19 | 19 |
|
20 | 20 | from utils import unicode_quote, parse_url, \
|
21 | 21 | THTTPConnection, THTTPSConnection
|
22 |
| -from domain import DomainResults |
| 22 | +from domain import DomainResults, Domain |
23 | 23 | from authentication import Authentication
|
24 | 24 | from fjson import json_loads
|
25 | 25 | # Because HTTPResponse objects *have* to have read() called on them
|
@@ -134,25 +134,70 @@ def retry_request():
|
134 | 134 | response = retry_request()
|
135 | 135 | return response
|
136 | 136 |
|
137 |
| - def get_all_domains(self, limit=None, marker=None, **parms): |
138 |
| - if limit: |
139 |
| - parms['limit'] = limit |
140 |
| - if marker: |
141 |
| - parms['marker'] = marker |
142 |
| - return DomainResults(self, self.list_domains_info(**parms)) |
143 |
| - |
144 |
| - def list_domains_info(self, limit=None, marker=None, **parms): |
145 |
| - if limit: |
146 |
| - parms['limit'] = limit |
147 |
| - if marker: |
148 |
| - parms['marker'] = marker |
149 |
| - parms['format'] = 'json' |
150 |
| - response = self.make_request('GET', ['domains'], parms=parms) |
| 137 | + def get_domains(self): |
| 138 | + return DomainResults(self, self.list_domains_info()) |
| 139 | + |
| 140 | + def list_domains_info(self): |
| 141 | + response = self.make_request('GET', ['domains']) |
151 | 142 | if (response.status < 200) or (response.status > 299):
|
152 | 143 | response.read()
|
153 | 144 | raise ResponseError(response.status, response.reason)
|
154 | 145 | return json_loads(response.read())['domains']['domain']
|
155 | 146 |
|
| 147 | + #TODO |
| 148 | + def get_domain(self, domain_id): |
| 149 | + pass |
| 150 | + |
| 151 | + # Take a reponse parse it if there is asyncResponse and wait for |
| 152 | + # it |
| 153 | + def wait_for_async_request(self, response): |
| 154 | + if (response.status < 200) or (response.status > 299): |
| 155 | + response.read() |
| 156 | + raise ResponseError(response.status, response.reason) |
| 157 | + output = json_loads(response.read()) |
| 158 | + |
| 159 | + while True: |
| 160 | + if 'asyncResponse' in output: |
| 161 | + jobId = output['asyncResponse']['jobId'] |
| 162 | + response = self.make_request('GET', ['status', jobId]) |
| 163 | + if (response.status < 200) or (response.status > 299): |
| 164 | + response.read() |
| 165 | + raise ResponseError(response.status, response.reason) |
| 166 | + _output = response.read().strip() |
| 167 | + if not _output: |
| 168 | + return True |
| 169 | + output = json_loads(_output) |
| 170 | + time.sleep(1) |
| 171 | + continue |
| 172 | + elif 'DnsFault' in output: |
| 173 | + raise ResponseError(output['DnsFault']['code'], |
| 174 | + output['DnsFault']['message']) |
| 175 | + else: |
| 176 | + return output |
| 177 | + |
| 178 | + #TODO: We make it syncronous here, we should offer async as well |
| 179 | + # unlike API does (choice). |
| 180 | + def create_domain(self, name, ttl, emailAddress): |
| 181 | + if not ttl >= 300: |
| 182 | + raise Exception("Ttl is a minimun of 300 seconds") |
| 183 | + xml = """<domains xmlns="http://docs.rackspacecloud.com/dns/api/v1.0"> |
| 184 | + <domain name="%(name)s" ttl="%(ttl)s" emailAddress="%(emailAddress)s"> |
| 185 | + </domain> |
| 186 | +</domains> |
| 187 | +""" % locals() |
| 188 | + response = self.make_request('POST', ['domains'], data=xml) |
| 189 | + output = self.wait_for_async_request(response) |
| 190 | + |
| 191 | + if 'domains' in output: |
| 192 | + domain = output["domains"]["domain"] |
| 193 | + return Domain(**domain[0]) |
| 194 | + else: |
| 195 | + raise Exception("This should not happen") |
| 196 | + |
| 197 | + def delete_domain(self, domain_id, deleteSubdomains=False): |
| 198 | + response = self.make_request('DELETE', ['domains', domain_id]) |
| 199 | + return self.wait_for_async_request(response) |
| 200 | + |
156 | 201 |
|
157 | 202 | class ConnectionPool(Queue):
|
158 | 203 | """
|
@@ -192,7 +237,7 @@ def put(self, connobj):
|
192 | 237 | @type connobj: L{Connection}
|
193 | 238 | """
|
194 | 239 | try:
|
195 |
| - Queue.put(self, (time(), connobj), block=0) |
| 240 | + Queue.put(self, (time.time(), connobj), block=0) |
196 | 241 | except Full:
|
197 | 242 | del connobj
|
198 | 243 | # vim:set ai sw=4 ts=4 tw=0 expandtab:
|
0 commit comments