forked from softlayer/softlayer-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.py
More file actions
433 lines (328 loc) · 14.1 KB
/
API.py
File metadata and controls
433 lines (328 loc) · 14.1 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
"""
SoftLayer.API
~~~~~~~~~~~~~
SoftLayer API bindings
:copyright: (c) 2013, SoftLayer Technologies, Inc. All rights reserved.
:license: BSD, see LICENSE for more details.
"""
from SoftLayer.consts import API_PUBLIC_ENDPOINT, API_PRIVATE_ENDPOINT, \
USER_AGENT
from SoftLayer.transport import make_api_call
from SoftLayer.exceptions import SoftLayerError
import os
API_USERNAME = None
API_KEY = None
API_BASE_URL = API_PUBLIC_ENDPOINT
class Client(object):
""" A SoftLayer API client.
:param service_name: the name of the SoftLayer API service to query
:param integer id: an optional object ID if you're instantiating a
particular SoftLayer_API object. Setting an ID defines this client's
initialization parameter.
:param username: an optional API username if you wish to bypass the
package's built-in username
:param api_key: an optional API key if you wish to bypass the package's
built in API key
:param endpoint_url: the API endpoint base URL you wish to connect to.
Set this to API_PRIVATE_ENDPOINT to connect via SoftLayer's private
network.
:param integer timeout: timeout for API requests
:param boolean verbose: prints details about every HTTP request if true
Usage:
>>> import SoftLayer
>>> client = SoftLayer.Client(username="username", api_key="api_key")
>>> resp = client['Account'].getObject()
>>> resp['companyName']
'Your Company'
"""
_prefix = "SoftLayer_"
def __init__(self, service_name=None, id=None, username=None, api_key=None,
endpoint_url=None, timeout=None, verbose=False):
self._service_name = service_name
self.verbose = verbose
self._headers = {}
self._raw_headers = {}
self.username = username or API_USERNAME or \
os.environ.get('SL_USERNAME') or ''
self.api_key = api_key or API_KEY or os.environ.get('SL_API_KEY') or ''
self.set_authentication(self.username, self.api_key)
if id is not None:
self.set_init_parameter(int(id))
self._endpoint_url = (endpoint_url or API_BASE_URL or
API_PUBLIC_ENDPOINT).rstrip('/')
self.timeout = timeout
def add_raw_header(self, name, value):
""" Set HTTP headers for API calls.
:param name: the header name
:param value: the header value
.. deprecated:: 2.0.0
"""
self._raw_headers[name] = value
def add_header(self, name, value):
""" Set a SoftLayer API call header.
:param name: the header name
:param value: the header value
.. deprecated:: 2.0.0
"""
name = name.strip()
if name is None or name == '':
raise SoftLayerError('Please specify a header name.')
self._headers[name] = value
def remove_header(self, name):
""" Remove a SoftLayer API call header.
:param name: the header name
.. deprecated:: 2.0.0
"""
if name in self._headers:
del self._headers[name.strip()]
def set_authentication(self, username, api_key):
""" Set user and key to authenticate a SoftLayer API call.
Use this method if you wish to bypass the API_USER and API_KEY class
constants and set custom authentication per API call.
See https://manage.softlayer.com/Administrative/apiKeychain for more
information.
:param username: the username to authenticate with
:param api_key: the user's API key
.. deprecated:: 2.0.0
"""
self.add_header('authenticate', {
'username': username.strip(),
'apiKey': api_key.strip(),
})
def set_init_parameter(self, id):
""" Set an initialization parameter header.
Initialization parameters instantiate a SoftLayer API service object to
act upon during your API method call. For instance, if your account has
a server with ID number 1234, then setting an initialization parameter
of 1234 in the SoftLayer_Hardware_Server Service instructs the API to
act on server record 1234 in your method calls.
See http://sldn.softlayer.com/article/Using-Initialization-Parameters-SoftLayer-API # NOQA
for more information.
:param id: the ID of the SoftLayer API object to instantiate
.. deprecated:: 2.0.0
"""
self.add_header(self._service_name + 'InitParameters', {
'id': int(id)
})
def set_object_mask(self, mask):
""" Set an object mask to a SoftLayer API call.
Use an object mask to retrieve data related your API call's result.
Object masks are skeleton objects, or strings that define nested
relational properties to retrieve along with an object's local
properties. See
http://sldn.softlayer.com/article/Using-Object-Masks-SoftLayer-API
for more information.
:param mask: the object mask you wish to define
.. deprecated:: 2.0.0
"""
header = 'SoftLayer_ObjectMask'
if isinstance(mask, dict):
header = '%sObjectMask' % self._service_name
self.add_header(header, {'mask': mask})
def set_result_limit(self, limit, offset=0):
""" Set a result limit on a SoftLayer API call.
Many SoftLayer API methods return a group of results. These methods
support a way to limit the number of results retrieved from the
SoftLayer API in a way akin to an SQL LIMIT statement.
:param limit: the number of results to limit a SoftLayer API call to
:param offset: An optional offset at which to begin a SoftLayer API
call's returned result
.. deprecated:: 2.0.0
"""
self.add_header('resultLimit', {
'limit': int(limit),
'offset': int(offset)
})
def __getitem__(self, name):
""" Get a SoftLayer Service.
:param name: The name of the service. E.G. Account
Usage:
>>> client['Account']
<Service: Account>
"""
return Service(self, name)
def call(self, service, method, *args, **kwargs):
""" Make a SoftLayer API call
:param service: the name of the SoftLayer API service
:param method: the method to call on the service
:param \*args: (optional) arguments for the remote call
:param id: (optional) id for the resource
:param mask: (optional) object mask
:param dict filter: (optional) filter dict
:param dict headers: (optional) optional XML-RPC headers
:param dict raw_headers: (optional) HTTP transport headers
:param int limit: (optional) return at most this many results
:param int offset: (optional) offset results by this many
:param boolean iter: (optional) if True, returns a generator with the
results
Usage:
>>> client['Account'].getVirtualGuests(mask="id", limit=10)
[...]
"""
if kwargs.get('iter'):
return self.iter_call(service, method, *args, **kwargs)
if not service.startswith(self._prefix):
service = self._prefix + service
objectid = kwargs.get('id')
objectmask = kwargs.get('mask')
objectfilter = kwargs.get('filter')
headers = kwargs.get('headers')
raw_headers = kwargs.get('raw_headers')
limit = kwargs.get('limit')
offset = kwargs.get('offset', 0)
if headers is None:
headers = {
'authenticate': {
'username': self.username,
'apiKey': self.api_key,
}}
http_headers = {
'User-Agent': USER_AGENT,
'Content-Type': 'application/xml',
}
if self._raw_headers:
for name, value in self._raw_headers.items():
http_headers[name] = value
if raw_headers:
for name, value in raw_headers.items():
http_headers[name] = value
if objectid is not None:
headers[service + 'InitParameters'] = {'id': int(objectid)}
if objectmask is not None:
headers.update(self.__format_object_mask(objectmask, service))
if objectfilter is not None:
headers['%sObjectFilter' % service] = objectfilter
if limit:
headers['resultLimit'] = {
'limit': int(limit),
'offset': int(offset)
}
uri = '/'.join([self._endpoint_url, service])
return make_api_call(uri, method, args, headers=headers,
http_headers=http_headers, timeout=self.timeout,
verbose=self.verbose)
__call__ = call
def iter_call(self, service, method,
chunk=100, limit=None, offset=0, *args, **kwargs):
""" A generator that deals with paginating through results.
:param service: the name of the SoftLayer API service
:param method: the method to call on the service
:param integer chunk: result size for each API call
:param \*args: same optional arguments that ``Client.call`` takes
:param \*\*kwargs: same optional keyword arguments that ``Client.call``
takes
"""
if chunk <= 0:
raise AttributeError("Chunk size should be greater than zero.")
if limit:
chunk = min(chunk, limit)
result_count = 0
kwargs['iter'] = False
while True:
if limit:
# We've reached the end of the results
if result_count >= limit:
break
# Don't over-fetch past the given limit
if chunk + result_count > limit:
chunk = limit - result_count
results = self.call(service, method,
offset=offset, limit=chunk, *args, **kwargs)
# It looks like we ran out results
if not results:
break
# Apparently this method doesn't return a list.
# Why are you even iterating over this?
if not isinstance(results, list):
yield results
break
for item in results:
yield item
result_count += 1
offset += chunk
if len(results) < chunk:
break
def __format_object_mask(self, objectmask, service):
""" Format new and old style object masks into proper headers.
:param objectmask: a string- or dict-based object mask
:param service: a SoftLayer API service name
"""
if isinstance(objectmask, dict):
mheader = '%sObjectMask' % service
else:
mheader = self._prefix + 'ObjectMask'
objectmask = objectmask.strip()
if objectmask.startswith('mask'):
objectmask = objectmask[4:]
if objectmask[0] == '.':
objectmask = objectmask[1:]
elif objectmask[0] == '[' and objectmask[-1] == ']':
objectmask = objectmask[1:-1]
else:
raise SoftLayerError('Malformed Mask: %s' % objectmask)
objectmask = "mask[%s]" % objectmask
return {mheader: {'mask': objectmask}}
def __getattr__(self, name):
""" Attempt a SoftLayer API call.
Use this as a catch-all so users can call SoftLayer API methods
directly against their client object. If the property or method
relating to their client object doesn't exist then assume the user is
attempting a SoftLayer API call and return a simple function that makes
an XML-RPC call.
:param name: method name
.. deprecated:: 2.0.0
"""
if name in ["__name__", "__bases__"]:
raise AttributeError("'Obj' object has no attribute '%s'" % name)
def call_handler(*args, **kwargs):
if self._service_name is None:
raise SoftLayerError(
"Service is not set on Client instance.")
kwargs['headers'] = self._headers
return self.call(self._service_name, name, *args, **kwargs)
return call_handler
def __repr__(self):
return "<Client: endpoint=%s, user=%s>" \
% (self._endpoint_url, self.username)
__str__ = __repr__
class Service(object):
def __init__(self, client, name):
self.client = client
self.name = name
def call(self, name, *args, **kwargs):
""" Make a SoftLayer API call
:param method: the method to call on the service
:param \*args: same optional arguments that ``Client.call`` takes
:param \*\*kwargs: same optional keyword arguments that ``Client.call``
takes
Usage:
>>> client['Account'].getVirtualGuests(mask="id", limit=10)
[...]
"""
return self.client.call(self.name, name, *args, **kwargs)
__call__ = call
def iter_call(self, name, *args, **kwargs):
""" A generator that deals with paginating through results.
:param method: the method to call on the service
:param integer chunk: result size for each API call
:param \*args: same optional arguments that ``Client.call`` takes
:param \*\*kwargs: same optional keyword arguments that ``Client.call``
takes
Usage:
>>> gen = client['Account'].getVirtualGuests(iter=True)
>>> for virtual_guest in gen:
... virtual_guest['id']
...
1234
4321
"""
return self.client.iter_call(self.name, name, *args, **kwargs)
def __getattr__(self, name):
if name in ["__name__", "__bases__"]:
raise AttributeError("'Obj' object has no attribute '%s'" % name)
def call_handler(*args, **kwargs):
return self(name, *args, **kwargs)
return call_handler
def __repr__(self):
return "<Service: %s>" % (self.name,)
__str__ = __repr__