-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
executable file
·127 lines (109 loc) · 4.26 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
from twisted.internet import defer
import utils
import pprint
class HybridClusterException(Exception):
"""
An exception raised by the Control Panel's XML API. The exception value is
the ElementTree of the XML response's Errors element.
"""
def __init__(self, xml, cmd, query):
self.xml = xml
self.cmd = cmd
self.query = query
def __str__(self):
return ("\n " + (str(self.xml) if isinstance(self.xml, list) else str(utils.tostring(self.xml))) +
"\nRunning: " + self.cmd +
"\nQuery: " + pprint.pformat(self.query))
class Client(object):
"""
Simple wrapper for the Control Panel XML API, returning responses using the
ElementTree API.
Smallest working example:
>>> api = Client("my.cluster.com", "resellername", "somepass")
>>> yield api.testSuccess()
>>> print "Got a successful response from the API"
Error case:
>>> try:
>>> yield api.testFail()
>>> except HybridClusterException, e:
>>> print "Got error:", e
Should print "Got Error: Failed test command."
Something more interesting, creating a website and fetching its website ID:
>>> websiteID = int((yield api.addExternalDomain(
>>> Domain='mydomain.com',
>>> Type='blank',
>>> )).find('WebsiteID').text)
"""
def __init__(self, cpDomain, username, password, apiVersion='1.1', decodeResponse=False):
self.endpoint = "http://%s/api/post" % (cpDomain)
self.username = username
self.password = password
self.apiVersion = apiVersion
self.decodeResponse = decodeResponse
def __getattr__(self, funcname):
command = funcname.upper()
def inner(**kw):
query = {
'Reseller': self.username,
'APIKey': self.password,
'Command': command,
'APIVersion': self.apiVersion,
}
query.update(kw)
if self.decodeResponse:
query['ResponseFormat'] = "json"
d = utils.httpRequest(self.endpoint,
query,
headers={'Content-Type': ['application/x-www-form-urlencoded']}
)
def parse(xml):
if self.decodeResponse:
from simplejson import loads
from simplejson.decoder import JSONDecodeError
try:
return loads(xml)
except JSONDecodeError:
raise Exception("Could not decode JSON '%s'" % xml)
try:
result = utils.fromstring(xml)
except Exception:
print "Trying to parse", xml
raise
return result
d.addCallback(parse)
def checkErrors(xml):
if self.decodeResponse:
if int(xml['ErrorCount'] > 0):
raise HybridClusterException(xml['Errors'], command, query)
else:
if int(xml.find('ErrorCount').text) > 0:
raise HybridClusterException(xml.find('Errors'), command, query)
return xml
d.addCallback(checkErrors)
return d
return inner
@defer.inlineCallbacks
def pollStepAction(api, stepActionID):
"""
Poll the given stepActionID until all the consituent steps are
complete, then fire the returned deferred.
"""
numStepActions = 0
completedStepActions = None
while completedStepActions < numStepActions:
response = yield api.pollStepAction(StepActionID=stepActionID)
steps = response.find("Steps").getchildren()
numStepActions = 0
completedStepActions = 0
print "\n"
for step in steps:
status = step.find("Description").text.ljust(40, '.')
numStepActions += 1
if step.find("CompleteDate") is not None:
completedStepActions += 1
status += "FINISHED"
else:
status += "PENDING"
print status
yield utils.sleep(1)