Skip to content

Commit 67a54fd

Browse files
author
Ryan Rossiter
committed
Update needed fixes for CLI and add doc
The CLI appears to fail out if it gets AttributeError as part of any of the manager calls, so in order to not make it look like a softlayer-pythyon but, we need to spit out something from SoftLayer.exceptions. The base exceptions.SoftLayerError was used. Presets also needed to be handle differently. When listing the presets, both the activePresets and accountRestrictedActivePresets need to be retrieved and merged together before being returned. Finally, extra doc was added to verify_order() and place_order() to make it easier to understand how to use it.
1 parent ab5ba93 commit 67a54fd

File tree

1 file changed

+60
-13
lines changed

1 file changed

+60
-13
lines changed

SoftLayer/managers/ordering.py

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
:license: MIT, see LICENSE for more details.
77
"""
88

9+
from SoftLayer import exceptions
10+
911

1012
class OrderingManager(object):
1113
"""Manager to help ordering via the SoftLayer API.
@@ -245,7 +247,7 @@ def list_categories(self, package_keyname, **kwargs):
245247

246248
package = self.get_package_by_key(package_keyname, mask='id')
247249
if not package:
248-
raise AttributeError("Package {} does not exist".format(package_keyname))
250+
raise exceptions.SoftLayerError("Package {} does not exist".format(package_keyname))
249251

250252
categories = self.package_svc.getConfiguration(id=package['id'], **get_kwargs)
251253
return categories
@@ -269,7 +271,7 @@ def list_items(self, package_keyname, **kwargs):
269271

270272
package = self.get_package_by_key(package_keyname, mask='id')
271273
if not package:
272-
raise AttributeError("Package {} does not exist".format(package_keyname))
274+
raise exceptions.SoftLayerError("Package {} does not exist".format(package_keyname))
273275

274276
items = self.package_svc.getItems(id=package['id'], **get_kwargs)
275277
return items
@@ -315,10 +317,12 @@ def list_presets(self, package_keyname, **kwargs):
315317

316318
package = self.get_package_by_key(package_keyname, mask='id')
317319
if not package:
318-
raise AttributeError("Package {} does not exist".format(package_keyname))
320+
raise exceptions.SoftLayerError("Package {} does not exist".format(package_keyname))
319321

320-
presets = self.package_svc.getActivePresets(id=package['id'], **get_kwargs)
321-
return presets
322+
acc_presets = self.package_svc.getAccountRestrictedActivePresets(
323+
id=package['id'], **get_kwargs)
324+
active_presets = self.package_svc.getActivePresets(id=package['id'], **get_kwargs)
325+
return acc_presets + active_presets
322326

323327
def get_preset_by_key(self, package_keyname, preset_keyname, mask=None):
324328
"""Gets a single preset with the given key."""
@@ -328,7 +332,7 @@ def get_preset_by_key(self, package_keyname, preset_keyname, mask=None):
328332
presets = self.list_presets(package_keyname, mask=mask, filter=_filter)
329333

330334
if len(presets) == 0:
331-
raise AttributeError(
335+
raise exceptions.SoftLayerError(
332336
"Preset {} does not exist in package {}".format(preset_keyname,
333337
package_keyname))
334338

@@ -349,7 +353,7 @@ def get_price_id_list(self, package_keyname, item_keynames):
349353
"""
350354
package = self.get_package_by_key(package_keyname, mask='id')
351355
if not package:
352-
raise AttributeError("Package {} does not exist".format(package_keyname))
356+
raise exceptions.SoftLayerError("Package {} does not exist".format(package_keyname))
353357

354358
mask = 'id, keyName, prices'
355359
items = self.list_items(package_keyname, mask=mask)
@@ -362,7 +366,7 @@ def get_price_id_list(self, package_keyname, item_keynames):
362366
matching_item = [i for i in items
363367
if i['keyName'] == item_keyname][0]
364368
except IndexError:
365-
raise AttributeError(
369+
raise exceptions.SoftLayerError(
366370
"Item {} does not exist for package {}".format(item_keyname,
367371
package_keyname))
368372

@@ -376,15 +380,36 @@ def get_price_id_list(self, package_keyname, item_keynames):
376380

377381
return prices
378382

379-
def verify_order(self, package_keyname, location, price_keynames,
383+
def verify_order(self, package_keyname, location, item_keynames,
380384
hourly=True, preset_keyname=None, extras=None, quantity=1):
381-
"""Verifies an order with the given package and prices."""
385+
"""Verifies an order with the given package and prices.
386+
387+
This function takes in parameters needed for an order and verifies the order
388+
to ensure the given items are compatible with the given package.
389+
390+
:param str package_keyname: The keyname for the package being ordered
391+
:param str location: The datacenter location string for ordering (Ex: DALLAS13)
392+
:param list item_keynames: The list of item keyname strings to order. To see list of
393+
possible keynames for a package, use list_items()
394+
(or `slcli order item-list`)
395+
:param bool hourly: If true, uses hourly billing, otherwise uses monthly billing
396+
:param string preset_keyname: If needed, specifies a preset to use for that package.
397+
To see a list of possible keynames for a package, use
398+
list_preset() (or `slcli order preset-list`)
399+
:param dict extras: The extra data for the order in dictionary format.
400+
Example: A VSI order requires hostname and domain to be set, so
401+
extras will look like the following:
402+
{'virtualGuests': [{'hostname': 'test',
403+
'domain': 'softlayer.com'}]}
404+
:param int quantity: The number of resources to order
405+
406+
"""
382407
order = {}
383408
extras = extras or {}
384409

385410
package = self.get_package_by_key(package_keyname, mask='id')
386411
if not package:
387-
raise AttributeError("Package {} does not exist".format(package_keyname))
412+
raise exceptions.SoftLayerError("Package {} does not exist".format(package_keyname))
388413

389414
# if there was extra data given for the order, add it to the order
390415
# example: VSIs require hostname and domain set on the order, so
@@ -405,9 +430,31 @@ def verify_order(self, package_keyname, location, price_keynames,
405430

406431
return self.order_svc.verifyOrder(order)
407432

408-
def place_order(self, package_keyname, location, price_keynames,
433+
def place_order(self, package_keyname, location, item_keynames,
409434
hourly=True, preset_keyname=None, extras=None, quantity=1):
410-
"""Places an order with the given package and prices."""
435+
"""Places an order with the given package and prices.
436+
437+
This function takes in parameters needed for an order and places the order.
438+
439+
:param str package_keyname: The keyname for the package being ordered
440+
:param str location: The datacenter location string for ordering (Ex: DALLAS13)
441+
:param list item_keynames: The list of item keyname strings to order. To see list of
442+
possible keynames for a package, use list_items()
443+
(or `slcli order item-list`)
444+
:param bool hourly: If true, uses hourly billing, otherwise uses monthly billing
445+
:param string preset_keyname: If needed, specifies a preset to use for that package.
446+
To see a list of possible keynames for a package, use
447+
list_preset() (or `slcli order preset-list`)
448+
:param dict extras: The extra data for the order in dictionary format.
449+
Example: A VSI order requires hostname and domain to be set, so
450+
extras will look like the following:
451+
{'virtualGuests': [{'hostname': 'test',
452+
'domain': 'softlayer.com'}]}
453+
:param int quantity: The number of resources to order
454+
455+
"""
456+
# verify the order, and if the order is valid, the proper prices will be filled
457+
# into the order template, so we can just send that to placeOrder to order it
411458
verified_order = self.verify_order(package_keyname, location, price_keynames,
412459
hourly=hourly,
413460
preset_keyname=preset_keyname,

0 commit comments

Comments
 (0)