Skip to content

Commit ee748de

Browse files
committed
#1222 Added logic to lookup LBaaS by name instead of the address
1 parent 332f300 commit ee748de

File tree

6 files changed

+41
-49
lines changed

6 files changed

+41
-49
lines changed

SoftLayer/CLI/loadbal/detail.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import SoftLayer
55
from SoftLayer.CLI import environment
6-
from SoftLayer.CLI import exceptions
76
from SoftLayer.CLI import formatting
87
from SoftLayer import utils
98

@@ -14,15 +13,8 @@
1413
def cli(env, identifier):
1514
"""Get Load Balancer as a Service details."""
1615
mgr = SoftLayer.LoadBalancerManager(env.client)
17-
18-
if utils.valid_domain(identifier):
19-
lbaas = mgr.get_lbaas_by_address(identifier)
20-
if not lbaas:
21-
raise exceptions.CLIAbort("{} address not found".format(identifier))
22-
this_lb = mgr.get_lb(lbaas.get('id'))
23-
else:
24-
_, lbid = mgr.get_lbaas_uuid_id(identifier)
25-
this_lb = mgr.get_lb(lbid)
16+
_, lbid = mgr.get_lbaas_uuid_id(identifier)
17+
this_lb = mgr.get_lb(lbid)
2618
if this_lb.get('previousErrorText'):
2719
print(this_lb.get('previousErrorText'))
2820
table = lbaas_table(this_lb)

SoftLayer/fixtures/SoftLayer_Network_LBaaS_LoadBalancer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,28 +92,30 @@ def update_lb_health_monitors(self, uuid, checks):
9292
def get_lbaas_uuid_id(self, identifier):
9393
"""Gets a LBaaS uuid, id. Since sometimes you need one or the other.
9494
95-
:param identifier: either the LB Id, or UUID, this function will return both.
95+
:param identifier: either the LB Id, UUID or Name, this function will return UUI and LB Id.
9696
:return (uuid, id):
9797
"""
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]")
98+
mask = "mask[id,uuid]"
99+
if isinstance(identifier, int):
100+
this_lb = self.lbaas.getObject(id=identifier, mask=mask)
101+
elif len(identifier) == 36 and utils.UUID_RE.match(identifier):
102+
this_lb = self.lbaas.getLoadBalancer(identifier, mask=mask)
101103
else:
102-
this_lb = self.lbaas.getObject(id=identifier, mask="mask[id,uuid]")
104+
this_lb = self.get_lbaas_by_name(identifier, mask=mask)
105+
103106
return this_lb.get('uuid'), this_lb.get('id')
104107

105-
def get_lbaas_by_address(self, address):
106-
"""Gets a LBaaS by address.
108+
def get_lbaas_by_name(self, name, mask=None):
109+
"""Gets a LBaaS by name.
107110
108-
:param address: Address of the LBaaS instance
111+
:param name: Name of the LBaaS instance
112+
:param mask:
113+
:returns: SoftLayer_Network_LBaaS_LoadBalancer or an empty dictionary if the name is not found.
109114
"""
110-
this_lb = {}
111-
this_lbs = self.lbaas.getAllObjects()
112-
for lbaas in this_lbs:
113-
if lbaas.get('address') == address:
114-
this_lb = lbaas
115-
break
116-
return this_lb
115+
object_filter = {'name': {'operation': name}}
116+
this_lbs = self.lbaas.getAllObjects(filter=object_filter, mask=mask)
117+
118+
return this_lbs[0] if this_lbs else {}
117119

118120
def delete_lb_member(self, identifier, member_id):
119121
"""Removes a member from a LBaaS instance
@@ -210,7 +212,7 @@ def order_lbaas(self, datacenter, name, desc, protocols, subnet_id, public=False
210212
'description': desc,
211213
'location': datacenter,
212214
'packageId': package.get('id'),
213-
'useHourlyPricing': True, # Required since LBaaS is an hourly service
215+
'useHourlyPricing': True, # Required since LBaaS is an hourly service
214216
'prices': [{'id': price_id} for price_id in prices],
215217
'protocolConfigurations': protocols,
216218
'subnets': [{'id': subnet_id}],

SoftLayer/utils.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,8 @@
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 = ['<=', '>=', '<', '>', '~', '!~', '*=', '^=', '$=', '_=']
16-
DOMAIN_RE = re.compile(r'[-a-zA-Z0-9.]{1,40}\.')
17-
18-
19-
def valid_domain(domain_name):
20-
"""Return whether or not given value is a valid domain.
21-
22-
:param domain_name: domain string to validate.
23-
24-
"""
25-
return DOMAIN_RE.match(domain_name)
2616

2717

2818
def lookup(dic, key, *keys):

tests/CLI/modules/loadbal_tests.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import SoftLayer
99
from SoftLayer.CLI.exceptions import ArgumentError
10-
from SoftLayer.CLI.exceptions import CLIAbort
1110
from SoftLayer import exceptions
1211
from SoftLayer.fixtures import SoftLayer_Network_LBaaS_LoadBalancer
1312
from SoftLayer.fixtures import SoftLayer_Product_Package
@@ -217,15 +216,15 @@ def test_lb_detail(self):
217216
result = self.run_command(['lb', 'detail', '1111111'])
218217
self.assert_no_fail(result)
219218

220-
def test_lb_detail_by_address(self):
221-
address = SoftLayer_Network_LBaaS_LoadBalancer.getObject.get('address')
222-
result = self.run_command(['lb', 'detail', address])
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])
223222
self.assert_no_fail(result)
224223

225-
def test_lb_detail_address_not_found(self):
226-
address = 'test-01-ams01.clb.appdomain.cloud'
227-
result = self.run_command(['lb', 'detail', address])
228-
self.assertIsInstance(result.exception, CLIAbort)
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)
229228

230229
def test_order(self):
231230
result = self.run_command(['loadbal', 'order', '--name', 'test', '--datacenter', 'par01', '--label',

tests/managers/loadbal_tests.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ def test_get_lbaas_uuid_id_id(self):
7979
self.assertEqual(lb_uuid, uuid)
8080
self.assertEqual(lb_id, my_id)
8181

82+
def test_get_lbaas_uuid_id_name(self):
83+
uuid = '1a1aa111-4474-4e16-9f02-4de959229b85'
84+
my_id = 1111111
85+
name = 'test-01'
86+
lb_uuid, lb_id = self.lb_mgr.get_lbaas_uuid_id(name)
87+
self.assert_called_with('SoftLayer_Network_LBaaS_LoadBalancer', 'getAllObjects')
88+
self.assertEqual(lb_uuid, uuid)
89+
self.assertEqual(lb_id, my_id)
90+
8291
def test_delete_lb_member(self):
8392
uuid = 'aa-bb-cc'
8493
member_id = 'dd-ee-ff'
@@ -178,8 +187,8 @@ def test_cancel_lbaas(self):
178187
self.lb_mgr.cancel_lbaas(uuid)
179188
self.assert_called_with('SoftLayer_Network_LBaaS_LoadBalancer', 'cancelLoadBalancer', args=(uuid,))
180189

181-
def test_get_lbaas_by_address(self):
182-
address = SoftLayer_Network_LBaaS_LoadBalancer.getObject.get('address')
183-
load_bal = self.lb_mgr.get_lbaas_by_address(address)
190+
def test_get_lbaas_by_name(self):
191+
name = SoftLayer_Network_LBaaS_LoadBalancer.getObject.get('name')
192+
load_bal = self.lb_mgr.get_lbaas_by_name(name)
184193
self.assert_called_with('SoftLayer_Network_LBaaS_LoadBalancer', 'getAllObjects')
185194
self.assertIsNotNone(load_bal)

0 commit comments

Comments
 (0)