Skip to content

Commit ab9552d

Browse files
Merge pull request #1223 from ATGE/issue1222
#1222 load balancer by name
2 parents 8c87513 + fd7b0cf commit ab9552d

File tree

6 files changed

+65
-14
lines changed

6 files changed

+65
-14
lines changed

SoftLayer/CLI/loadbal/list.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ def location_sort(location):
3030
def generate_lbaas_table(lbaas):
3131
"""Takes a list of SoftLayer_Network_LBaaS_LoadBalancer and makes a table"""
3232
table = formatting.Table([
33-
'Id', 'Location', 'Address', 'Description', 'Public', 'Create Date', 'Members', 'Listeners'
33+
'Id', 'Location', 'Name', 'Description', 'Public', 'Create Date', 'Members', 'Listeners'
3434
], title="IBM Cloud LoadBalancer")
3535

36-
table.align['Address'] = 'l'
36+
table.align['Name'] = 'l'
3737
table.align['Description'] = 'l'
3838
table.align['Location'] = 'l'
3939
for this_lb in sorted(lbaas, key=location_sort):
4040
table.add_row([
4141
this_lb.get('id'),
4242
utils.lookup(this_lb, 'datacenter', 'longName'),
43-
this_lb.get('address'),
43+
this_lb.get('name'),
4444
this_lb.get('description'),
4545
'Yes' if this_lb.get('isPublic', 1) == 1 else 'No',
4646
utils.clean_time(this_lb.get('createDate')),

SoftLayer/fixtures/SoftLayer_Network_LBaaS_LoadBalancer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
getObject = {
22
'accountId': 1234,
3-
'address': '01-307608-ams01.clb.appdomain.cloud',
3+
'address': 'test-01-307608-ams01.clb.appdomain.cloud',
44
'createDate': '2019-08-12T07:49:43-06:00',
55
'id': 1111111,
66
'isPublic': 0,
77
'locationId': 265592,
88
'modifyDate': '2019-08-13T16:26:06-06:00',
9-
'name': 'dcabero-01',
9+
'name': 'test-01',
1010
'operatingStatus': 'ONLINE',
1111
'provisioningStatus': 'ACTIVE',
1212
'type': 0,

SoftLayer/managers/load_balancer.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
:license: MIT, see LICENSE for more details.
77
"""
8+
from SoftLayer import exceptions
89
from SoftLayer.managers import ordering
910
from SoftLayer import utils
1011

@@ -92,15 +93,32 @@ def update_lb_health_monitors(self, uuid, checks):
9293
def get_lbaas_uuid_id(self, identifier):
9394
"""Gets a LBaaS uuid, id. Since sometimes you need one or the other.
9495
95-
:param identifier: either the LB Id, or UUID, this function will return both.
96+
:param identifier: either the LB Id, UUID or Name, this function will return UUI and LB Id.
9697
:return (uuid, id):
9798
"""
98-
# int objects don't have a len property.
99-
if not isinstance(identifier, int) and len(identifier) == 36:
100-
this_lb = self.lbaas.getLoadBalancer(identifier, mask="mask[id,uuid]")
99+
mask = "mask[id,uuid]"
100+
if isinstance(identifier, int) or identifier.isdigit():
101+
this_lb = self.lbaas.getObject(id=identifier, mask=mask)
102+
elif len(identifier) == 36 and utils.UUID_RE.match(identifier):
103+
this_lb = self.lbaas.getLoadBalancer(identifier, mask=mask)
101104
else:
102-
this_lb = self.lbaas.getObject(id=identifier, mask="mask[id,uuid]")
103-
return this_lb['uuid'], this_lb['id']
105+
this_lb = self.get_lbaas_by_name(identifier, mask=mask)
106+
107+
return this_lb.get('uuid'), this_lb.get('id')
108+
109+
def get_lbaas_by_name(self, name, mask=None):
110+
"""Gets a LBaaS by name.
111+
112+
:param name: Name of the LBaaS instance
113+
:param mask:
114+
:returns: SoftLayer_Network_LBaaS_LoadBalancer.
115+
"""
116+
object_filter = {'name': {'operation': name}}
117+
this_lbs = self.lbaas.getAllObjects(filter=object_filter, mask=mask)
118+
if not this_lbs:
119+
raise exceptions.SoftLayerError("Unable to find LBaaS with name: {}".format(name))
120+
121+
return this_lbs[0]
104122

105123
def delete_lb_member(self, identifier, member_id):
106124
"""Removes a member from a LBaaS instance
@@ -197,7 +215,7 @@ def order_lbaas(self, datacenter, name, desc, protocols, subnet_id, public=False
197215
'description': desc,
198216
'location': datacenter,
199217
'packageId': package.get('id'),
200-
'useHourlyPricing': True, # Required since LBaaS is an hourly service
218+
'useHourlyPricing': True, # Required since LBaaS is an hourly service
201219
'prices': [{'id': price_id} for price_id in prices],
202220
'protocolConfigurations': protocols,
203221
'subnets': [{'id': subnet_id}],

SoftLayer/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
# pylint: disable=no-member, invalid-name
1313

14-
UUID_RE = re.compile(r'^[0-9a-f\-]{36}$', re.I)
14+
UUID_RE = re.compile(r'^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$', re.I)
1515
KNOWN_OPERATIONS = ['<=', '>=', '<', '>', '~', '!~', '*=', '^=', '$=', '_=']
1616

1717

tests/CLI/modules/loadbal_tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ def test_lb_detail(self):
216216
result = self.run_command(['lb', 'detail', '1111111'])
217217
self.assert_no_fail(result)
218218

219+
def test_lb_detail_by_name(self):
220+
name = SoftLayer_Network_LBaaS_LoadBalancer.getObject.get('name')
221+
result = self.run_command(['lb', 'detail', name])
222+
self.assert_no_fail(result)
223+
224+
def test_lb_detail_uuid(self):
225+
uuid = SoftLayer_Network_LBaaS_LoadBalancer.getObject.get('uuid')
226+
result = self.run_command(['lb', 'detail', uuid])
227+
self.assert_no_fail(result)
228+
219229
def test_order(self):
220230
result = self.run_command(['loadbal', 'order', '--name', 'test', '--datacenter', 'par01', '--label',
221231
'labeltest', '--subnet', '759282'])

tests/managers/loadbal_tests.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
them directly to the API.
99
"""
1010
import SoftLayer
11+
from SoftLayer import exceptions
12+
from SoftLayer.fixtures import SoftLayer_Network_LBaaS_LoadBalancer
1113
from SoftLayer import testing
1214

1315

@@ -78,6 +80,15 @@ def test_get_lbaas_uuid_id_id(self):
7880
self.assertEqual(lb_uuid, uuid)
7981
self.assertEqual(lb_id, my_id)
8082

83+
def test_get_lbaas_uuid_id_name(self):
84+
uuid = '1a1aa111-4474-4e16-9f02-4de959229b85'
85+
my_id = 1111111
86+
name = 'test-01'
87+
lb_uuid, lb_id = self.lb_mgr.get_lbaas_uuid_id(name)
88+
self.assert_called_with('SoftLayer_Network_LBaaS_LoadBalancer', 'getAllObjects')
89+
self.assertEqual(lb_uuid, uuid)
90+
self.assertEqual(lb_id, my_id)
91+
8192
def test_delete_lb_member(self):
8293
uuid = 'aa-bb-cc'
8394
member_id = 'dd-ee-ff'
@@ -155,7 +166,7 @@ def test_order_lbaas(self):
155166
'description': desc,
156167
'location': datacenter,
157168
'packageId': package[0]['id'],
158-
'useHourlyPricing': True, # Required since LBaaS is an hourly service
169+
'useHourlyPricing': True, # Required since LBaaS is an hourly service
159170
'prices': [{'id': package[0]['itemPrices'][0]['id']}],
160171
'protocolConfigurations': protocols,
161172
'subnets': [{'id': subnet_id}],
@@ -176,3 +187,15 @@ def test_cancel_lbaas(self):
176187
uuid = 'aa-bb-cc'
177188
self.lb_mgr.cancel_lbaas(uuid)
178189
self.assert_called_with('SoftLayer_Network_LBaaS_LoadBalancer', 'cancelLoadBalancer', args=(uuid,))
190+
191+
def test_get_lbaas_by_name(self):
192+
name = SoftLayer_Network_LBaaS_LoadBalancer.getObject.get('name')
193+
load_bal = self.lb_mgr.get_lbaas_by_name(name)
194+
self.assert_called_with('SoftLayer_Network_LBaaS_LoadBalancer', 'getAllObjects')
195+
self.assertIsNotNone(load_bal)
196+
197+
def test_get_lbaas_by_name_fails(self):
198+
load_bal_mock = self.set_mock('SoftLayer_Network_LBaaS_LoadBalancer', 'getAllObjects')
199+
load_bal_mock.return_value = []
200+
name = 'test'
201+
self.assertRaises(exceptions.SoftLayerError, self.lb_mgr.get_lbaas_by_name, name)

0 commit comments

Comments
 (0)