diff --git a/SoftLayer/fixtures/SoftLayer_Location.py b/SoftLayer/fixtures/SoftLayer_Location.py index 7ec26fa18..d770d4d82 100644 --- a/SoftLayer/fixtures/SoftLayer_Location.py +++ b/SoftLayer/fixtures/SoftLayer_Location.py @@ -9,3 +9,4 @@ "longName": "San Jose 1", "name": "sjc01" }] +getDatacenters = [{'id': 1854895, 'name': 'dal13', 'regions': [{'keyname': 'DALLAS13'}]}] diff --git a/SoftLayer/managers/ordering.py b/SoftLayer/managers/ordering.py index 4d8e94c13..12744dcee 100644 --- a/SoftLayer/managers/ordering.py +++ b/SoftLayer/managers/ordering.py @@ -7,6 +7,8 @@ """ # pylint: disable=no-self-use +from re import match + from SoftLayer import exceptions CATEGORY_MASK = '''id, @@ -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] prices.append(price_id) return prices @@ -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 @@ -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'] diff --git a/tests/CLI/modules/order_tests.py b/tests/CLI/modules/order_tests.py index 3d049912d..cde0abc31 100644 --- a/tests/CLI/modules/order_tests.py +++ b/tests/CLI/modules/order_tests.py @@ -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] diff --git a/tests/managers/ordering_tests.py b/tests/managers/ordering_tests.py index 50e3e220a..276249cdf 100644 --- a/tests/managers/ordering_tests.py +++ b/tests/managers/ordering_tests.py @@ -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: @@ -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}], @@ -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, @@ -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")