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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
|
"""
SoftLayer.ordering
~~~~~~~~~~~~~~~~~~
Ordering Manager
:license: MIT, see LICENSE for more details.
"""
from re import match
from SoftLayer import exceptions
from SoftLayer import utils
CATEGORY_MASK = '''id, isRequired, itemCategory[id, name, categoryCode]'''
ITEM_MASK = '''id, keyName, description, itemCategory, categories, prices'''
PACKAGE_MASK = '''id, name, keyName, isActive, type'''
PRESET_MASK = '''id, name, keyName, description, prices[id, hourlyRecurringFee, recurringFee], locations'''
# pylint: disable=R0904
class OrderingManager(object):
"""Manager to help ordering via the SoftLayer API.
:param SoftLayer.API.BaseClient client: the client instance
"""
def __init__(self, client):
self.client = client
self.package_svc = client['Product_Package']
self.order_svc = client['Product_Order']
self.billing_svc = client['Billing_Order']
self.package_preset = client['Product_Package_Preset']
self.package_mask = 'id, description, capacity, itemCategory, keyName, prices[categories], ' \
'softwareDescription[id,referenceCode,longDescription]'
def get_packages_of_type(self, package_types, mask=None):
"""Get packages that match a certain type.
Each ordering package has a type, so return all packages that match
the types we are looking for
:param list package_types: List of strings representing the package
type keynames we are interested in.
:param string mask: Mask to specify the properties we want to retrieve
"""
_filter = {
'type': {
'keyName': {
'operation': 'in',
'options': [
{'name': 'data',
'value': package_types}
],
},
},
}
packages = self.package_svc.getAllObjects(mask=mask, filter=_filter)
packages = self.filter_outlet_packages(packages)
return packages
def get_order_detail(self, order_id, mask=None):
"""Get order details.
:param int order_id: to specify the order that we want to retrieve.
:param string mask: Mask to specify the properties we want to retrieve.
"""
_default_mask = (
'mask[orderTotalAmount,orderApprovalDate,'
'initialInvoice[id,amount,invoiceTotalAmount,'
'invoiceTopLevelItems[id, description, hostName, domainName, oneTimeAfterTaxAmount,'
'recurringAfterTaxAmount, createDate,'
'categoryCode,'
'category[name],'
'location[name],'
'children[id, category[name], description, oneTimeAfterTaxAmount,recurringAfterTaxAmount]]],'
'items[description],userRecord[displayName,userStatus]]')
mask = _default_mask if mask is None else mask
order = self.billing_svc.getObject(mask=mask, id=order_id)
return order
@staticmethod
def filter_outlet_packages(packages):
"""Remove packages designated as OUTLET.
Those type of packages must be handled in a different way,
and they are not supported at the moment.
:param packages: Dictionary of packages. Name and description keys
must be present in each of them.
"""
non_outlet_packages = []
for package in packages:
if all(['OUTLET' not in package.get('description', '').upper(),
'OUTLET' not in package.get('name', '').upper()]):
non_outlet_packages.append(package)
return non_outlet_packages
@staticmethod
def get_only_active_packages(packages):
"""Return only active packages.
If a package is active, it is eligible for ordering
This will inspect the 'isActive' property on the provided packages
:param packages: Dictionary of packages, isActive key must be present
"""
active_packages = []
for package in packages:
if package['isActive']:
active_packages.append(package)
return active_packages
def get_package_by_type(self, package_type, mask=None):
"""Get a single package of a given type.
Syntactic sugar to retrieve a single package of a given type.
If multiple packages share the given type, this will return the first
one returned by the API.
If no packages are found, returns None
:param string package_type: representing the package type key name we are interested in
"""
packages = self.get_packages_of_type([package_type], mask)
if len(packages) == 0:
return None
else:
return packages.pop()
def get_package_id_by_type(self, package_type):
"""Return the package ID of a Product Package with a given type.
:param string package_type: representing the package type key name we are interested in
:raises ValueError: when no package of the given type is found
"""
mask = "mask[id, name, description, isActive, type[keyName]]"
package = self.get_package_by_type(package_type, mask)
if package:
return package['id']
else:
raise ValueError("No package found for type: " + package_type)
def get_quotes(self):
"""Retrieve a list of active quotes.
:returns: a list of SoftLayer_Billing_Order_Quote
"""
mask = "mask[order[id,items[id,package[id,keyName]]]]"
quotes = self.client['Account'].getActiveQuotes(mask=mask)
return quotes
def get_quote_details(self, quote_id):
"""Retrieve quote details.
:param quote_id: ID number of target quote
"""
mask = "mask[order[id,items[package[id,keyName]]]]"
quote = self.client['Billing_Order_Quote'].getObject(id=quote_id, mask=mask)
return quote
def save_quote(self, quote_id):
"""Save a quote.
:param quote_id: ID number of target quote
"""
return self.client['Billing_Order_Quote'].saveQuote(id=quote_id)
def get_order_container(self, quote_id):
"""Generate an order container from a quote object.
:param quote_id: ID number of target quote
"""
quote = self.client['Billing_Order_Quote']
container = quote.getRecalculatedOrderContainer(id=quote_id)
return container
def generate_order_template(self, quote_id, extra, quantity=1):
"""Generate a complete order template.
:param int quote_id: ID of target quote
:param dictionary extra: Overrides for the defaults of SoftLayer_Container_Product_Order
:param int quantity: Number of items to order.
"""
if not isinstance(extra, dict):
raise ValueError("extra is not formatted properly")
container = self.get_order_container(quote_id)
container['quantity'] = quantity
for key in extra.keys():
container[key] = extra[key]
return container
def verify_quote(self, quote_id, extra):
"""Verifies that a quote order is valid.
::
extras = {
'hardware': {'hostname': 'test', 'domain': 'testing.com'},
'quantity': 2
}
manager = ordering.OrderingManager(env.client)
result = manager.verify_quote(12345, extras)
:param int quote_id: ID for the target quote
:param dictionary extra: Overrides for the defaults of SoftLayer_Container_Product_Order
:param int quantity: Quantity to override default
"""
container = self.generate_order_template(quote_id, extra)
clean_container = {}
# There are a few fields that wil cause exceptions in the XML endpoint if you send in '',
# or None in Rest endpoint (e.g. reservedCapacityId, hostId). But we clean all just to be safe.
# This for some reason is only a problem on verify_quote.
for key in container.keys():
if container.get(key):
clean_container[key] = container[key]
return self.client.call('SoftLayer_Billing_Order_Quote', 'verifyOrder', clean_container, id=quote_id)
def order_quote(self, quote_id, extra):
"""Places an order using a quote
::
extras = {
'hardware': {'hostname': 'test', 'domain': 'testing.com'},
'quantity': 2
}
manager = ordering.OrderingManager(env.client)
result = manager.order_quote(12345, extras)
:param int quote_id: ID for the target quote
:param dictionary extra: Overrides for the defaults of SoftLayer_Container_Product_Order
:param int quantity: Quantity to override default
"""
container = self.generate_order_template(quote_id, extra)
return self.client.call('SoftLayer_Billing_Order_Quote', 'placeOrder', container, id=quote_id)
def get_package_by_key(self, package_keyname, mask=None):
"""Get a single package with a given key.
If no packages are found, returns None
:param package_keyname: string representing the package key name we are interested in.
:param string mask: Mask to specify the properties we want to retrieve
"""
_filter = {'keyName': {'operation': package_keyname}}
packages = self.client.call('SoftLayer_Product_Package', 'getAllObjects', mask=mask, filter=_filter)
if len(packages) == 0:
raise exceptions.SoftLayerError(f"Package {package_keyname} does not exist")
return packages.pop()
def list_categories(self, package_keyname, **kwargs):
"""List the categories for the given package.
:param str package_keyname: The package for which to get the categories.
:returns: List of categories associated with the package
"""
kwargs['mask'] = kwargs.get('mask', CATEGORY_MASK)
if 'filter' in kwargs:
kwargs['filter'] = kwargs['filter']
package = self.get_package_by_key(package_keyname, mask='id')
categories = self.package_svc.getConfiguration(id=package['id'], **kwargs)
return categories
def list_items(self, package_keyname, **kwargs):
"""List the items for the given package.
:param str package_keyname: The package for which to get the items.
:returns: List of items in the package
"""
if 'mask' not in kwargs:
kwargs['mask'] = ITEM_MASK
package = self.get_package_by_key(package_keyname, mask='id')
items = self.package_svc.getItems(id=package['id'], **kwargs)
return items
def list_packages(self, **kwargs):
"""List active packages.
:returns: List of active packages.
"""
kwargs['mask'] = kwargs.get('mask', PACKAGE_MASK)
if 'filter' in kwargs:
kwargs['filter'] = kwargs['filter']
packages = self.package_svc.getAllObjects(**kwargs)
return [package for package in packages if package['isActive']]
def list_presets(self, package_keyname, **kwargs):
"""Gets active presets for the given package.
:param str package_keyname: The package for which to get presets
:returns: A list of package presets that can be used for ordering
"""
kwargs['mask'] = kwargs.get('mask', PRESET_MASK)
if 'filter' in kwargs:
kwargs['filter'] = kwargs['filter']
package = self.get_package_by_key(package_keyname, mask='id')
acc_presets = self.package_svc.getAccountRestrictedActivePresets(id=package['id'], **kwargs)
active_presets = self.package_svc.getActivePresets(id=package['id'], **kwargs)
return active_presets + acc_presets
def get_preset_by_key(self, package_keyname, preset_keyname, mask=None):
"""Gets a single preset with the given key."""
preset_operation = '_= %s' % preset_keyname
_filter = {
'activePresets': {
'keyName': {
'operation': preset_operation
}
},
'accountRestrictedActivePresets': {
'keyName': {
'operation': preset_operation
}
}
}
presets = self.list_presets(package_keyname, mask=mask, filter=_filter)
if len(presets) == 0:
raise exceptions.SoftLayerError(
f"Preset {preset_keyname} does not exist in package {package_keyname}")
return presets[0]
def get_price_id_list(self, package_keyname, item_keynames, core=None):
"""Converts a list of item keynames to a list of price IDs.
This function is used to convert a list of item keynames into
a list of price IDs that are used in the Product_Order verifyOrder()
and placeOrder() functions.
:param str package_keyname: The package associated with the prices
:param list item_keynames: A list of item keyname strings
:param str core: preset guest core capacity.
:returns: A list of price IDs associated with the given item
keynames in the given package
"""
mask = 'id, description, capacity, itemCategory, keyName, prices[categories], ' \
'softwareDescription[id,referenceCode,longDescription]'
items = self.list_items(package_keyname, mask=mask)
item_capacity = self.get_item_capacity(items, item_keynames)
prices = []
category_dict = {"gpu0": -1, "pcie_slot0": -1}
for item_keyname in item_keynames:
matching_item = []
# Need to find the item in the package that has a matching
# keyName with the current item we are searching for
for i in items:
reference_code = utils.lookup(i, 'softwareDescription', 'referenceCode')
if i['keyName'] == item_keyname or reference_code == item_keyname:
matching_item.append(i)
if len(matching_item) == 0:
message = f"Item {item_keyname} does not exist for package {package_keyname}"
raise exceptions.SoftLayerError(message)
matching_item = matching_item[0]
# we want to get the price ID that has no location attached to it,
# because that is the most generic price. verifyOrder/placeOrder
# can take that ID and create the proper price for us in the location
# in which the order is made
item_category = matching_item['itemCategory']['categoryCode']
if item_category not in category_dict:
if core is None:
price_id = self.get_item_price_id(item_capacity, matching_item['prices'])
else:
price_id = self.get_item_price_id(core, matching_item['prices'])
else:
# GPU and PCIe items has two generic prices and they are added to the list
# according to the number of items in the order.
category_dict[item_category] += 1
category_code = item_category[:-1] + str(category_dict[item_category])
price_id = [p['id'] for p in matching_item['prices']
if not p['locationGroupId']
and p['categories'][0]['categoryCode'] == category_code][0]
prices.append(price_id)
return prices
@staticmethod
def get_item_price_id(core, prices, term=0):
"""get item price id
core: None or a number to match against capacityRestrictionType
prices: list of SoftLayer_Product_Item_Price
term: int to match against SoftLayer_Product_Item_Price.termLength
"""
price_id = None
for price in prices:
if not price['locationGroupId'] and price.get('termLength', 0) in {term, '', None}:
restriction = price.get('capacityRestrictionType', False)
# There is a price restriction. Make sure the price is within the restriction
if restriction and core is not None:
capacity_min = int(price.get('capacityRestrictionMinimum', -1))
capacity_max = int(price.get('capacityRestrictionMaximum', -1))
if "STORAGE" in restriction:
if capacity_min <= int(core) <= capacity_max:
price_id = price['id']
if "CORE" in restriction:
if capacity_min <= int(core) <= capacity_max:
price_id = price['id']
if "PROCESSOR" in restriction:
price_id = price['id']
# No price restrictions
else:
price_id = price['id']
return price_id
def get_item_capacity(self, items, item_keynames):
"""Get item capacity."""
item_capacity = None
for item_keyname in item_keynames:
for item in items:
if item['keyName'] == item_keyname:
if "CORE" in item["keyName"]:
item_capacity = item['capacity']
break
if "TIER" in item["keyName"]:
item_capacity = item['capacity']
break
if "INTEL" in item["keyName"]:
item_split = item['description'].split("(")
item_core = item_split[1].split(" ")
item_capacity = item_core[0]
break
return item_capacity
def get_preset_prices(self, preset):
"""Get preset item prices.
Retrieve a SoftLayer_Product_Package_Preset record.
:param int preset: preset identifier.
:returns: A list of price IDs associated with the given preset_id.
"""
mask = 'mask[prices[item]]'
prices = self.package_preset.getObject(id=preset, mask=mask)
return prices
def get_item_prices(self, package_id):
"""Get item prices.
Retrieve a SoftLayer_Product_Package item prices record.
:param int package_id: package identifier.
:returns: A list of price IDs associated with the given package.
"""
mask = 'mask[pricingLocationGroup[locations]]'
prices = self.package_svc.getItemPrices(id=package_id, mask=mask)
return prices
def verify_order(self, package_keyname, location, item_keynames, complex_type=None,
hourly=True, preset_keyname=None, extras=None, quantity=1):
"""Verifies an order with the given package and prices.
This function takes in parameters needed for an order and verifies the order
to ensure the given items are compatible with the given package.
:param str package_keyname: The keyname for the package being ordered
:param str location: The datacenter location string for ordering (Ex: DALLAS13)
:param list item_keynames: The list of item keyname strings to order. To see list of
possible keynames for a package, use list_items()
(or `slcli order item-list`)
:param str complex_type: The complex type to send with the order. Typically begins
with `SoftLayer_Container_Product_Order_`.
:param bool hourly: If true, uses hourly billing, otherwise uses monthly billing
:param string preset_keyname: If needed, specifies a preset to use for that package.
To see a list of possible keynames for a package, use
list_preset() (or `slcli order preset-list`)
:param dict extras: The extra data for the order in dictionary format.
Example: A VSI order requires hostname and domain to be set, so
extras will look like the following:
'virtualGuests': [{'hostname': 'test', 'domain': 'softlayer.com'}]}
:param int quantity: The number of resources to order
"""
order = self.generate_order(package_keyname, location, item_keynames,
complex_type=complex_type, hourly=hourly,
preset_keyname=preset_keyname,
extras=extras, quantity=quantity)
return self.order_svc.verifyOrder(order)
def place_order(self, package_keyname, location, item_keynames, complex_type=None,
hourly=True, preset_keyname=None, extras=None, quantity=1):
"""Places an order with the given package and prices.
This function takes in parameters needed for an order and places the order.
:param str package_keyname: The keyname for the package being ordered
:param str location: The datacenter location string for ordering (Ex: DALLAS13)
:param list item_keynames: The list of item keyname strings to order. To see list of
possible keynames for a package, use list_items()
(or `slcli order item-list`)
:param str complex_type: The complex type to send with the order. Typically begins
with `SoftLayer_Container_Product_Order_`.
:param bool hourly: If true, uses hourly billing, otherwise uses monthly billing
:param string preset_keyname: If needed, specifies a preset to use for that package.
To see a list of possible keynames for a package, use
list_preset() (or `slcli order preset-list`)
:param dict extras: The extra data for the order in dictionary format.
Example: A VSI order requires hostname and domain to be set, so
extras will look like the following:
{'virtualGuests': [{'hostname': 'test', domain': 'softlayer.com'}]}
:param int quantity: The number of resources to order
"""
order = self.generate_order(package_keyname, location, item_keynames,
complex_type=complex_type, hourly=hourly,
preset_keyname=preset_keyname,
extras=extras, quantity=quantity)
return self.order_svc.placeOrder(order)
def place_quote(self, package_keyname, location, item_keynames, complex_type=None,
preset_keyname=None, extras=None, quantity=1, quote_name=None, send_email=False):
"""Place a quote with the given package and prices.
This function takes in parameters needed for an order and places the quote.
:param str package_keyname: The keyname for the package being ordered
:param str location: The datacenter location string for ordering (Ex: DALLAS13)
:param list item_keynames: The list of item keyname strings to order. To see list of
possible keynames for a package, use list_items()
(or `slcli order item-list`)
:param str complex_type: The complex type to send with the order. Typically begins
with `SoftLayer_Container_Product_Order_`.
:param string preset_keyname: If needed, specifies a preset to use for that package.
To see a list of possible keynames for a package, use
list_preset() (or `slcli order preset-list`)
:param dict extras: The extra data for the order in dictionary format.
Example: A VSI order requires hostname and domain to be set, so
extras will look like the following:
{'virtualGuests': [{'hostname': 'test', domain': 'softlayer.com'}]}
:param int quantity: The number of resources to order
:param string quote_name: A custom name to be assigned to the quote (optional).
:param bool send_email: This flag indicates that the quote should be sent to the email
address associated with the account or order.
"""
order = self.generate_order(package_keyname, location, item_keynames, complex_type=complex_type,
hourly=False, preset_keyname=preset_keyname, extras=extras, quantity=quantity)
order['quoteName'] = quote_name
order['sendQuoteEmailFlag'] = send_email
return self.order_svc.placeQuote(order)
def generate_order(self, package_keyname, location, item_keynames, complex_type=None,
hourly=True, preset_keyname=None, extras=None, quantity=1):
"""Generates an order with the given package and prices.
This function takes in parameters needed for an order and generates an order
dictionary. This dictionary can then be used in either verify or placeOrder().
:param str package_keyname: The keyname for the package being ordered
:param str location: The datacenter location string for ordering (Ex: DALLAS13)
:param list item_keynames: The list of item keyname strings to order. To see list of
possible keynames for a package, use list_items()
(or `slcli order item-list`)
:param str complex_type: The complex type to send with the order. Typically begins
with `SoftLayer_Container_Product_Order_`.
:param bool hourly: If true, uses hourly billing, otherwise uses monthly billing
:param string preset_keyname: If needed, specifies a preset to use for that package.
To see a list of possible keynames for a package, use
list_preset() (or `slcli order preset-list`)
:param dict extras: The extra data for the order in dictionary format.
Example: A VSI order requires hostname and domain to be set, so
extras will look like the following:
{'virtualGuests': [{'hostname': 'test', 'domain': 'softlayer.com'}]}
:param int quantity: The number of resources to order
"""
container = {}
order = {}
extras = extras or {}
package = self.get_package_by_key(package_keyname, mask='id')
# if there was extra data given for the order, add it to the order
# example: VSIs require hostname and domain set on the order, so
# extras will be {'virtualGuests': [{'hostname': 'test',
# 'domain': 'softlayer.com'}]}
order.update(extras)
order['packageId'] = package['id']
order['quantity'] = quantity
order['location'] = self.get_location_id(location)
order['useHourlyPricing'] = hourly
preset_core = None
if preset_keyname:
preset_id = self.get_preset_by_key(package_keyname, preset_keyname)['id']
preset_items = self.get_preset_prices(preset_id)
for item in preset_items['prices']:
if item['item']['itemCategory']['categoryCode'] == "guest_core":
preset_core = item['item']['capacity']
order['presetId'] = preset_id
if not complex_type:
raise exceptions.SoftLayerError("A complex type must be specified with the order")
order['complexType'] = complex_type
price_ids = self.get_price_id_list(package_keyname, item_keynames, preset_core)
order['prices'] = [{'id': price_id} for price_id in price_ids]
container['orderContainers'] = [order]
return container
def package_locations(self, package_keyname):
"""List datacenter locations for a package keyname
:param str package_keyname: The package for which to get the items.
:returns: List of locations a package is orderable in
"""
mask = "mask[description, keyname, locations]"
package = self.get_package_by_key(package_keyname, mask='id')
regions = self.package_svc.getRegions(id=package['id'], mask=mask)
return regions
def get_location_id(self, location):
"""Finds the location ID of a given datacenter
This is mostly used so either a dc name, or regions keyname can be used when ordering
:param str location: Region Keyname (DALLAS13) or datacenter name (dal13)
:returns: integer id of the datacenter
"""
if isinstance(location, int):
return location
# Some orders dont require a location, just use 0
if location.upper() == "NONE":
return 0
mask = "mask[id,name,regions[keyname]]"
if match(r'[a-zA-Z]{3}[0-9]{2}', location) is not None:
search = {'name': {'operation': location}}
else:
search = {'regions': {'keyname': {'operation': location}}}
datacenter = self.client.call('SoftLayer_Location', 'getDatacenters', mask=mask, filter=search)
if len(datacenter) != 1:
raise exceptions.SoftLayerError("Unable to find location: %s" % location)
return datacenter[0]['id']
def get_item_prices_by_location(self, location, package_keyname):
"""Returns the item prices by location.
:param string package_keyname: The package for which to get the items.
:param string location: location name or keyname to get the item prices.
"""
object_mask = "filteredMask[pricingLocationGroup[locations]]"
location_name = self.resolve_location_name(location)
object_filter = {
"itemPrices": {"pricingLocationGroup": {"locations": {"name": {"operation": location_name}}}}}
package = self.get_package_by_key(package_keyname)
return self.client.call('SoftLayer_Product_Package', 'getItemPrices', mask=object_mask, filter=object_filter,
id=package['id'])
def resolve_location_name(self, location_key):
"""Resolves a location name using a string location key.
:param string location_key: A string location used to resolve the location name.
:return: An location name.
"""
default_region_keyname = 'unknown'
if not location_key or location_key == default_region_keyname:
raise exceptions.SoftLayerError(f"Invalid location {location_key}")
default_regions = [{'keyname': default_region_keyname}]
index_first = 0
object_mask = "mask[regions]"
locations = self.client.call('SoftLayer_Location', 'getDatacenters', mask=object_mask)
for location in locations:
location_name = location.get('name')
if location_name == location_key:
return location_key
if location.get('regions', default_regions)[index_first].get('keyname') == location_key:
return location_name
raise exceptions.SoftLayerError(f"Location {location_key} does not exist")
def get_items(self, package_id, storage_filter=None, mask=None):
""""Returns the items .
:param int package_id: The package for which to get the items.
:param dict storage_filter: object filter.
"""
return self.client.call('SoftLayer_Product_Package', 'getItems', filter=storage_filter,
id=package_id, mask=mask)
def get_regions(self, package_id, location=None):
"""returns the all regions.
:param int package_id: The package for which to get the items.
"""
_filter = ''
if location:
_filter = {"regions": {"location": {"location": {"name": {"operation": location}}}}}
return self.client.call('SoftLayer_Product_Package', 'getRegions', id=package_id, filter=_filter)
def delete_quote(self, quote_id):
"""Delete the quote of an order.
:param quote_id: ID number of target quote
"""
return self.client['SoftLayer_Billing_Order_Quote'].deleteQuote(id=quote_id)
def get_all_cancelation(self, limit=50):
"""returns the all cancelations, completed orders"""
mask = 'mask[id,itemCount,modifyDate,createDate,ticketId,' \
'ticket[assignedUserId,id,' \
'serviceProviderResourceId],status[name,id],user[id,firstName,lastName]]'
return self.client.call('SoftLayer_Billing_Item_Cancellation_Request', 'getAllCancellationRequests',
mask=mask, limit=limit)
|