Skip to content

Commit 860f4c2

Browse files
author
Chmouel Boudjnah
committed
more changes.
1 parent 5bd177d commit 860f4c2

File tree

5 files changed

+85
-67
lines changed

5 files changed

+85
-67
lines changed

clouddns/authentication.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ def authenticate(self):
6565
two-tuple containing the storage system URL and session token.
6666
"""
6767
conn = self.conn_class(self.host, self.port, timeout=self.timeout)
68-
#conn = self.conn_class(self.host, self.port)
6968
conn.request('GET', '/' + self.uri, headers=self.headers)
7069
response = conn.getresponse()
7170
response.read()

clouddns/connection.py

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
import os
1111
import socket
1212
import consts
13+
import time
1314
from Queue import Queue, Empty, Full
1415
from errors import ResponseError
1516
from httplib import HTTPSConnection, HTTPConnection, HTTPException
1617
from sys import version_info
17-
from time import time
1818
from urllib import quote
1919

2020
from utils import unicode_quote, parse_url, \
2121
THTTPConnection, THTTPSConnection
22-
from domain import DomainResults
22+
from domain import DomainResults, Domain
2323
from authentication import Authentication
2424
from fjson import json_loads
2525
# Because HTTPResponse objects *have* to have read() called on them
@@ -134,25 +134,70 @@ def retry_request():
134134
response = retry_request()
135135
return response
136136

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'])
151142
if (response.status < 200) or (response.status > 299):
152143
response.read()
153144
raise ResponseError(response.status, response.reason)
154145
return json_loads(response.read())['domains']['domain']
155146

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+
156201

157202
class ConnectionPool(Queue):
158203
"""
@@ -192,7 +237,7 @@ def put(self, connobj):
192237
@type connobj: L{Connection}
193238
"""
194239
try:
195-
Queue.put(self, (time(), connobj), block=0)
240+
Queue.put(self, (time.time(), connobj), block=0)
196241
except Full:
197242
del connobj
198243
# vim:set ai sw=4 ts=4 tw=0 expandtab:

clouddns/domain.py

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ def __set_name(self, name):
2929
name = property(fget=lambda self: self._name, fset=__set_name,
3030
doc="the name of the domain (read-only)")
3131

32-
def __init__(self, connection=None, name=None, id=None, accountId=None):
32+
def __init__(self, connection=None,
33+
name=None,
34+
id=None,
35+
accountId=None,
36+
ttl=None,
37+
emailAddress=None,
38+
):
3339
"""
3440
Domains will rarely if ever need to be instantiated directly by the
3541
user.
@@ -39,29 +45,29 @@ def __init__(self, connection=None, name=None, id=None, accountId=None):
3945
L{list_domains<Connection.list_domains>} and
4046
other methods on a valid Connection object.
4147
"""
42-
self.name = name
4348
self.conn = connection
49+
self.name = name
4450
self.id = id
4551
self.accountId = accountId
52+
self.ttl = ttl
53+
self.emailAddress = emailAddress
4654

47-
def create_record(self, record_name):
48-
pass
55+
def get_record(self, record_id):
56+
records = self.list_records_info()
57+
for rec in records:
58+
if rec['id'] == record_id:
59+
return Record(self, record=rec)
60+
61+
#TODO: Exceptions
62+
raise Exception("Not found")
4963

5064
def get_records(self):
5165
return RecordResults(self, self.list_records_info())
5266

53-
def get_record(self, record_id):
54-
return Record(self, record_id=record_id)
55-
56-
#TODO: filtering
5767
def list_records_info(self):
5868
resp = self._list_records_raw()
5969
return json_loads(resp)['records']['record']
6070

61-
def list_records(self):
62-
records = self._list_records_raw()
63-
print records
64-
6571
def _list_records_raw(self):
6672
"""
6773
Returns a chunk list of records
@@ -79,6 +85,9 @@ def __getitem__(self, key):
7985
def __str__(self):
8086
return self.name
8187

88+
def create_record(self, record_name):
89+
pass
90+
8291
def delete_record(self, record_name):
8392
pass
8493

@@ -101,8 +110,8 @@ def __getitem__(self, key):
101110
self._domains[key]['accountId'])
102111

103112
def __getslice__(self, i, j):
104-
return [Domain(self.conn, k['name'], k['count'], \
105-
k['size']) for k in self._domains[i:j]]
113+
return [Domain(self.conn, k['name'], k['id'], \
114+
k['accountId']) for k in self._domains[i:j]]
106115

107116
def __contains__(self, item):
108117
return item in self._names
@@ -113,17 +122,3 @@ def __repr__(self):
113122

114123
def __len__(self):
115124
return len(self._domains)
116-
117-
def index(self, value, *args):
118-
"""
119-
returns an integer for the first index of value
120-
"""
121-
return self._names.index(value, *args)
122-
123-
def count(self, value):
124-
"""
125-
returns the number of occurrences of value
126-
"""
127-
return self._names.count(value)
128-
129-
# vim:set ai sw=4 ts=4 tw=0 expandtab:

tests/auth.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/connection.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)