Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SoftLayer/fixtures/SoftLayer_Location.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
"longName": "San Jose 1",
"name": "sjc01"
}]
getDatacenters = [{'id': 1854895, 'name': 'dal13', 'regions': [{'keyname': 'DALLAS13'}]}]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to combine the fixtures since they are for the same method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that my method expects only 1 result back, but else where expects multiple results back.... I need to figure out a better way to do these fixtures to deal with different object masks/filters. but that might be for a different project.

26 changes: 24 additions & 2 deletions SoftLayer/managers/ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"""
# pylint: disable=no-self-use

from re import match

from SoftLayer import exceptions

CATEGORY_MASK = '''id,
Expand Down Expand Up @@ -341,7 +343,7 @@ def get_price_id_list(self, package_keyname, item_keynames):
# can take that ID and create the proper price for us in the location
# in which the order is made
price_id = [p['id'] for p in matching_item['prices']
if p['locationGroupId'] == ''][0]
if p['locationGroupId'] is None][0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are still relying on this being an empty string.

prices.append(price_id)

return prices
Expand Down Expand Up @@ -443,7 +445,7 @@ def generate_order(self, package_keyname, location, item_keynames, complex_type=
# 'domain': 'softlayer.com'}]}
order.update(extras)
order['packageId'] = package['id']
order['location'] = location
order['location'] = self.get_location_id(location)
order['quantity'] = quantity
order['useHourlyPricing'] = hourly

Expand Down Expand Up @@ -471,3 +473,23 @@ def package_locations(self, package_keyname):

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
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']
4 changes: 2 additions & 2 deletions tests/CLI/modules/order_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ def test_location_list(self):

def _get_order_items(self):
item1 = {'keyName': 'ITEM1', 'description': 'description1',
'prices': [{'id': 1111, 'locationGroupId': ''}]}
'prices': [{'id': 1111, 'locationGroupId': None}]}
item2 = {'keyName': 'ITEM2', 'description': 'description2',
'prices': [{'id': 2222, 'locationGroupId': ''}]}
'prices': [{'id': 2222, 'locationGroupId': None}]}

return [item1, item2]

Expand Down
25 changes: 21 additions & 4 deletions tests/managers/ordering_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ def test_get_preset_by_key_preset_not_found(self):
self.assertEqual('Preset {} does not exist in package {}'.format(keyname, 'PACKAGE_KEYNAME'), str(exc))

def test_get_price_id_list(self):
price1 = {'id': 1234, 'locationGroupId': ''}
price1 = {'id': 1234, 'locationGroupId': None}
item1 = {'id': 1111, 'keyName': 'ITEM1', 'prices': [price1]}
price2 = {'id': 5678, 'locationGroupId': ''}
price2 = {'id': 5678, 'locationGroupId': None}
item2 = {'id': 2222, 'keyName': 'ITEM2', 'prices': [price2]}

with mock.patch.object(self.ordering, 'list_items') as list_mock:
Expand Down Expand Up @@ -264,7 +264,7 @@ def test_generate_order_with_preset(self):
items = ['ITEM1', 'ITEM2']
preset = 'PRESET_KEYNAME'
expected_order = {'complexType': 'SoftLayer_Container_Foo',
'location': 'DALLAS13',
'location': 1854895,
'packageId': 1234,
'presetId': 5678,
'prices': [{'id': 1111}, {'id': 2222}],
Expand All @@ -285,7 +285,7 @@ def test_generate_order(self):
items = ['ITEM1', 'ITEM2']
complex_type = 'My_Type'
expected_order = {'complexType': 'My_Type',
'location': 'DALLAS13',
'location': 1854895,
'packageId': 1234,
'prices': [{'id': 1111}, {'id': 2222}],
'quantity': 1,
Expand Down Expand Up @@ -374,3 +374,20 @@ def _patch_for_generate(self):
to_return[1].return_value = {'id': 5678}
to_return[2].return_value = [1111, 2222]
return to_return

def test_get_location_id_short(self):
locations = self.set_mock('SoftLayer_Location', 'getDatacenters')
locations.return_value = [{'id': 1854895, 'name': 'dal13', 'regions': [{'keyname': 'DALLAS13'}]}]
dc_id = self.ordering.get_location_id('dal13')
self.assertEqual(1854895, dc_id)

def test_get_location_id_keyname(self):
locations = self.set_mock('SoftLayer_Location', 'getDatacenters')
locations.return_value = [{'id': 1854895, 'name': 'dal13', 'regions': [{'keyname': 'DALLAS13'}]}]
dc_id = self.ordering.get_location_id('DALLAS13')
self.assertEqual(1854895, dc_id)

def test_get_location_id_exception(self):
locations = self.set_mock('SoftLayer_Location', 'getDatacenters')
locations.return_value = []
self.assertRaises(exceptions.SoftLayerError, self.ordering.get_location_id, "BURMUDA")