From 91c3b8e34b58b0d70adbb21afd0c3235c71b27b5 Mon Sep 17 00:00:00 2001 From: Christopher Gallo Date: Thu, 22 Mar 2018 18:53:17 -0500 Subject: [PATCH 1/4] added lookup function for datacenter names --- SoftLayer/fixtures/SoftLayer_Location.py | 1 + SoftLayer/managers/ordering.py | 24 +++++++++++++++++++++++- tests/managers/ordering_tests.py | 22 ++++++++++++++++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/SoftLayer/fixtures/SoftLayer_Location.py b/SoftLayer/fixtures/SoftLayer_Location.py index 7ec26fa18..fdb49d91f 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'}]}] \ No newline at end of file diff --git a/SoftLayer/managers/ordering.py b/SoftLayer/managers/ordering.py index 4d8e94c13..8a840afec 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, @@ -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 + """ + + 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) + # [{'id': 1854895, 'name': 'dal13', 'regions': [{'keyname': 'DALLAS13'}]}] + if len(datacenter) != 1: + raise exceptions.SoftLayerError("Unable to find location: %s" % location) + return datacenter[0]['id'] + diff --git a/tests/managers/ordering_tests.py b/tests/managers/ordering_tests.py index 50e3e220a..f1debf9c9 100644 --- a/tests/managers/ordering_tests.py +++ b/tests/managers/ordering_tests.py @@ -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,21 @@ 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") + From 87637add59d3620cc05f9bdee99f3f40b0d5834d Mon Sep 17 00:00:00 2001 From: Christopher Gallo Date: Fri, 23 Mar 2018 15:33:13 -0500 Subject: [PATCH 2/4] fixed some anaylsis complaints --- SoftLayer/fixtures/SoftLayer_Location.py | 2 +- SoftLayer/managers/ordering.py | 5 ++--- tests/managers/ordering_tests.py | 3 +-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/SoftLayer/fixtures/SoftLayer_Location.py b/SoftLayer/fixtures/SoftLayer_Location.py index fdb49d91f..d770d4d82 100644 --- a/SoftLayer/fixtures/SoftLayer_Location.py +++ b/SoftLayer/fixtures/SoftLayer_Location.py @@ -9,4 +9,4 @@ "longName": "San Jose 1", "name": "sjc01" }] -getDatacenters = [{'id': 1854895, 'name': 'dal13', 'regions': [{'keyname': 'DALLAS13'}]}] \ No newline at end of file +getDatacenters = [{'id': 1854895, 'name': 'dal13', 'regions': [{'keyname': 'DALLAS13'}]}] diff --git a/SoftLayer/managers/ordering.py b/SoftLayer/managers/ordering.py index 8a840afec..df663b610 100644 --- a/SoftLayer/managers/ordering.py +++ b/SoftLayer/managers/ordering.py @@ -484,12 +484,11 @@ def get_location_id(self, 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}} + search = {'name': {'operation': location}} else: - search = {'regions' : {'keyname' : {'operation': location}}} + search = {'regions': {'keyname': {'operation': location}}} datacenter = self.client.call('SoftLayer_Location', 'getDatacenters', mask=mask, filter=search) # [{'id': 1854895, 'name': 'dal13', 'regions': [{'keyname': 'DALLAS13'}]}] if len(datacenter) != 1: raise exceptions.SoftLayerError("Unable to find location: %s" % location) return datacenter[0]['id'] - diff --git a/tests/managers/ordering_tests.py b/tests/managers/ordering_tests.py index f1debf9c9..d75f0c25a 100644 --- a/tests/managers/ordering_tests.py +++ b/tests/managers/ordering_tests.py @@ -383,7 +383,7 @@ def test_get_location_id_short(self): def test_get_location_id_keyname(self): locations = self.set_mock('SoftLayer_Location', 'getDatacenters') - locations.return_value =[{'id': 1854895, 'name': 'dal13', 'regions': [{'keyname': 'DALLAS13'}]}] + locations.return_value = [{'id': 1854895, 'name': 'dal13', 'regions': [{'keyname': 'DALLAS13'}]}] dc_id = self.ordering.get_location_id('DALLAS13') self.assertEqual(1854895, dc_id) @@ -391,4 +391,3 @@ 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") - From e081d7dd34e768a5aa4725eccd348323886d4df3 Mon Sep 17 00:00:00 2001 From: Christopher Gallo Date: Tue, 27 Mar 2018 12:52:11 -0500 Subject: [PATCH 3/4] changed locatoinGroupId to check for None instead of empty string --- SoftLayer/managers/ordering.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SoftLayer/managers/ordering.py b/SoftLayer/managers/ordering.py index df663b610..b28766636 100644 --- a/SoftLayer/managers/ordering.py +++ b/SoftLayer/managers/ordering.py @@ -343,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 From d6a9453f21417272d2fb4ea72072e16c84d3c285 Mon Sep 17 00:00:00 2001 From: Christopher Gallo Date: Tue, 27 Mar 2018 18:03:00 -0500 Subject: [PATCH 4/4] check for default location set to None instead of empty string --- SoftLayer/managers/ordering.py | 3 ++- tests/CLI/modules/order_tests.py | 4 ++-- tests/managers/ordering_tests.py | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/SoftLayer/managers/ordering.py b/SoftLayer/managers/ordering.py index b28766636..12744dcee 100644 --- a/SoftLayer/managers/ordering.py +++ b/SoftLayer/managers/ordering.py @@ -482,13 +482,14 @@ def get_location_id(self, location): :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) - # [{'id': 1854895, 'name': 'dal13', 'regions': [{'keyname': 'DALLAS13'}]}] 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 d75f0c25a..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: