Skip to content

Commit 91c3b8e

Browse files
added lookup function for datacenter names
1 parent 4ec8756 commit 91c3b8e

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

SoftLayer/fixtures/SoftLayer_Location.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
"longName": "San Jose 1",
1010
"name": "sjc01"
1111
}]
12+
getDatacenters = [{'id': 1854895, 'name': 'dal13', 'regions': [{'keyname': 'DALLAS13'}]}]

SoftLayer/managers/ordering.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"""
88
# pylint: disable=no-self-use
99

10+
from re import match
11+
1012
from SoftLayer import exceptions
1113

1214
CATEGORY_MASK = '''id,
@@ -443,7 +445,7 @@ def generate_order(self, package_keyname, location, item_keynames, complex_type=
443445
# 'domain': 'softlayer.com'}]}
444446
order.update(extras)
445447
order['packageId'] = package['id']
446-
order['location'] = location
448+
order['location'] = self.get_location_id(location)
447449
order['quantity'] = quantity
448450
order['useHourlyPricing'] = hourly
449451

@@ -471,3 +473,23 @@ def package_locations(self, package_keyname):
471473

472474
regions = self.package_svc.getRegions(id=package['id'], mask=mask)
473475
return regions
476+
477+
def get_location_id(self, location):
478+
"""Finds the location ID of a given datacenter
479+
480+
This is mostly used so either a dc name, or regions keyname can be used when ordering
481+
:param str location: Region Keyname (DALLAS13) or datacenter name (dal13)
482+
:returns: integer id of the datacenter
483+
"""
484+
485+
mask = "mask[id,name,regions[keyname]]"
486+
if match(r'[a-zA-Z]{3}[0-9]{2}', location) is not None:
487+
search = {'name' : {'operation': location}}
488+
else:
489+
search = {'regions' : {'keyname' : {'operation': location}}}
490+
datacenter = self.client.call('SoftLayer_Location', 'getDatacenters', mask=mask, filter=search)
491+
# [{'id': 1854895, 'name': 'dal13', 'regions': [{'keyname': 'DALLAS13'}]}]
492+
if len(datacenter) != 1:
493+
raise exceptions.SoftLayerError("Unable to find location: %s" % location)
494+
return datacenter[0]['id']
495+

tests/managers/ordering_tests.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def test_generate_order_with_preset(self):
264264
items = ['ITEM1', 'ITEM2']
265265
preset = 'PRESET_KEYNAME'
266266
expected_order = {'complexType': 'SoftLayer_Container_Foo',
267-
'location': 'DALLAS13',
267+
'location': 1854895,
268268
'packageId': 1234,
269269
'presetId': 5678,
270270
'prices': [{'id': 1111}, {'id': 2222}],
@@ -285,7 +285,7 @@ def test_generate_order(self):
285285
items = ['ITEM1', 'ITEM2']
286286
complex_type = 'My_Type'
287287
expected_order = {'complexType': 'My_Type',
288-
'location': 'DALLAS13',
288+
'location': 1854895,
289289
'packageId': 1234,
290290
'prices': [{'id': 1111}, {'id': 2222}],
291291
'quantity': 1,
@@ -374,3 +374,21 @@ def _patch_for_generate(self):
374374
to_return[1].return_value = {'id': 5678}
375375
to_return[2].return_value = [1111, 2222]
376376
return to_return
377+
378+
def test_get_location_id_short(self):
379+
locations = self.set_mock('SoftLayer_Location', 'getDatacenters')
380+
locations.return_value = [{'id': 1854895, 'name': 'dal13', 'regions': [{'keyname': 'DALLAS13'}]}]
381+
dc_id = self.ordering.get_location_id('dal13')
382+
self.assertEqual(1854895, dc_id)
383+
384+
def test_get_location_id_keyname(self):
385+
locations = self.set_mock('SoftLayer_Location', 'getDatacenters')
386+
locations.return_value =[{'id': 1854895, 'name': 'dal13', 'regions': [{'keyname': 'DALLAS13'}]}]
387+
dc_id = self.ordering.get_location_id('DALLAS13')
388+
self.assertEqual(1854895, dc_id)
389+
390+
def test_get_location_id_exception(self):
391+
locations = self.set_mock('SoftLayer_Location', 'getDatacenters')
392+
locations.return_value = []
393+
self.assertRaises(exceptions.SoftLayerError, self.ordering.get_location_id, "BURMUDA")
394+

0 commit comments

Comments
 (0)