Repository: softlayer/softlayer-python
Pull request #1223 (2020-02-07 23:03:30)

#1222 load balancer by name


Patch:
diff --git a/SoftLayer/CLI/loadbal/list.py b/SoftLayer/CLI/loadbal/list.py
index 4eb2f573..02ce5bb7 100644
--- a/SoftLayer/CLI/loadbal/list.py
+++ b/SoftLayer/CLI/loadbal/list.py
@@ -30,17 +30,17 @@ def location_sort(location):
 def generate_lbaas_table(lbaas):
     """Takes a list of SoftLayer_Network_LBaaS_LoadBalancer and makes a table"""
     table = formatting.Table([
-        'Id', 'Location', 'Address', 'Description', 'Public', 'Create Date', 'Members', 'Listeners'
+        'Id', 'Location', 'Name', 'Description', 'Public', 'Create Date', 'Members', 'Listeners'
     ], title="IBM Cloud LoadBalancer")
 
-    table.align['Address'] = 'l'
+    table.align['Name'] = 'l'
     table.align['Description'] = 'l'
     table.align['Location'] = 'l'
     for this_lb in sorted(lbaas, key=location_sort):
         table.add_row([
             this_lb.get('id'),
             utils.lookup(this_lb, 'datacenter', 'longName'),
-            this_lb.get('address'),
+            this_lb.get('name'),
             this_lb.get('description'),
             'Yes' if this_lb.get('isPublic', 1) == 1 else 'No',
             utils.clean_time(this_lb.get('createDate')),
diff --git a/SoftLayer/fixtures/SoftLayer_Network_LBaaS_LoadBalancer.py b/SoftLayer/fixtures/SoftLayer_Network_LBaaS_LoadBalancer.py
index 1047a7ce..94220cde 100644
--- a/SoftLayer/fixtures/SoftLayer_Network_LBaaS_LoadBalancer.py
+++ b/SoftLayer/fixtures/SoftLayer_Network_LBaaS_LoadBalancer.py
@@ -1,12 +1,12 @@
 getObject = {
     'accountId': 1234,
-    'address': '01-307608-ams01.clb.appdomain.cloud',
+    'address': 'test-01-307608-ams01.clb.appdomain.cloud',
     'createDate': '2019-08-12T07:49:43-06:00',
     'id': 1111111,
     'isPublic': 0,
     'locationId': 265592,
     'modifyDate': '2019-08-13T16:26:06-06:00',
-    'name': 'dcabero-01',
+    'name': 'test-01',
     'operatingStatus': 'ONLINE',
     'provisioningStatus': 'ACTIVE',
     'type': 0,
diff --git a/SoftLayer/managers/load_balancer.py b/SoftLayer/managers/load_balancer.py
index ea67e469..3ffa0959 100644
--- a/SoftLayer/managers/load_balancer.py
+++ b/SoftLayer/managers/load_balancer.py
@@ -5,6 +5,7 @@
 
     :license: MIT, see LICENSE for more details.
 """
+from SoftLayer import exceptions
 from SoftLayer.managers import ordering
 from SoftLayer import utils
 
@@ -92,15 +93,32 @@ def update_lb_health_monitors(self, uuid, checks):
     def get_lbaas_uuid_id(self, identifier):
         """Gets a LBaaS uuid, id. Since sometimes you need one or the other.
 
-        :param identifier: either the LB Id, or UUID, this function will return both.
+        :param identifier: either the LB Id, UUID or Name, this function will return UUI and LB Id.
         :return (uuid, id):
         """
-        # int objects don't have a len property.
-        if not isinstance(identifier, int) and len(identifier) == 36:
-            this_lb = self.lbaas.getLoadBalancer(identifier, mask="mask[id,uuid]")
+        mask = "mask[id,uuid]"
+        if isinstance(identifier, int) or identifier.isdigit():
+            this_lb = self.lbaas.getObject(id=identifier, mask=mask)
+        elif len(identifier) == 36 and utils.UUID_RE.match(identifier):
+            this_lb = self.lbaas.getLoadBalancer(identifier, mask=mask)
         else:
-            this_lb = self.lbaas.getObject(id=identifier, mask="mask[id,uuid]")
-        return this_lb['uuid'], this_lb['id']
+            this_lb = self.get_lbaas_by_name(identifier, mask=mask)
+
+        return this_lb.get('uuid'), this_lb.get('id')
+
+    def get_lbaas_by_name(self, name, mask=None):
+        """Gets a LBaaS by name.
+
+        :param name: Name of the LBaaS instance
+        :param mask:
+        :returns: SoftLayer_Network_LBaaS_LoadBalancer.
+        """
+        object_filter = {'name': {'operation': name}}
+        this_lbs = self.lbaas.getAllObjects(filter=object_filter, mask=mask)
+        if not this_lbs:
+            raise exceptions.SoftLayerError("Unable to find LBaaS with name: {}".format(name))
+
+        return this_lbs[0]
 
     def delete_lb_member(self, identifier, member_id):
         """Removes a member from a LBaaS instance
@@ -197,7 +215,7 @@ def order_lbaas(self, datacenter, name, desc, protocols, subnet_id, public=False
             'description': desc,
             'location': datacenter,
             'packageId': package.get('id'),
-            'useHourlyPricing': True,       # Required since LBaaS is an hourly service
+            'useHourlyPricing': True,  # Required since LBaaS is an hourly service
             'prices': [{'id': price_id} for price_id in prices],
             'protocolConfigurations': protocols,
             'subnets': [{'id': subnet_id}],
diff --git a/SoftLayer/utils.py b/SoftLayer/utils.py
index 74ad84b9..0234bf72 100644
--- a/SoftLayer/utils.py
+++ b/SoftLayer/utils.py
@@ -11,7 +11,7 @@
 
 # pylint: disable=no-member, invalid-name
 
-UUID_RE = re.compile(r'^[0-9a-f\-]{36}$', re.I)
+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)
 KNOWN_OPERATIONS = ['<=', '>=', '<', '>', '~', '!~', '*=', '^=', '$=', '_=']
 
 


Test patch:
diff --git a/tests/CLI/modules/loadbal_tests.py b/tests/CLI/modules/loadbal_tests.py
index b87dab6e..84866d3f 100644
--- a/tests/CLI/modules/loadbal_tests.py
+++ b/tests/CLI/modules/loadbal_tests.py
@@ -216,6 +216,16 @@ def test_lb_detail(self):
         result = self.run_command(['lb', 'detail', '1111111'])
         self.assert_no_fail(result)
 
+    def test_lb_detail_by_name(self):
+        name = SoftLayer_Network_LBaaS_LoadBalancer.getObject.get('name')
+        result = self.run_command(['lb', 'detail', name])
+        self.assert_no_fail(result)
+
+    def test_lb_detail_uuid(self):
+        uuid = SoftLayer_Network_LBaaS_LoadBalancer.getObject.get('uuid')
+        result = self.run_command(['lb', 'detail', uuid])
+        self.assert_no_fail(result)
+
     def test_order(self):
         result = self.run_command(['loadbal', 'order', '--name', 'test', '--datacenter', 'par01', '--label',
                                    'labeltest', '--subnet', '759282'])
diff --git a/tests/managers/loadbal_tests.py b/tests/managers/loadbal_tests.py
index cf93ed9a..d8058edc 100644
--- a/tests/managers/loadbal_tests.py
+++ b/tests/managers/loadbal_tests.py
@@ -8,6 +8,8 @@
     them directly to the API.
 """
 import SoftLayer
+from SoftLayer import exceptions
+from SoftLayer.fixtures import SoftLayer_Network_LBaaS_LoadBalancer
 from SoftLayer import testing
 
 
@@ -78,6 +80,15 @@ def test_get_lbaas_uuid_id_id(self):
         self.assertEqual(lb_uuid, uuid)
         self.assertEqual(lb_id, my_id)
 
+    def test_get_lbaas_uuid_id_name(self):
+        uuid = '1a1aa111-4474-4e16-9f02-4de959229b85'
+        my_id = 1111111
+        name = 'test-01'
+        lb_uuid, lb_id = self.lb_mgr.get_lbaas_uuid_id(name)
+        self.assert_called_with('SoftLayer_Network_LBaaS_LoadBalancer', 'getAllObjects')
+        self.assertEqual(lb_uuid, uuid)
+        self.assertEqual(lb_id, my_id)
+
     def test_delete_lb_member(self):
         uuid = 'aa-bb-cc'
         member_id = 'dd-ee-ff'
@@ -155,7 +166,7 @@ def test_order_lbaas(self):
             'description': desc,
             'location': datacenter,
             'packageId': package[0]['id'],
-            'useHourlyPricing': True,       # Required since LBaaS is an hourly service
+            'useHourlyPricing': True,  # Required since LBaaS is an hourly service
             'prices': [{'id': package[0]['itemPrices'][0]['id']}],
             'protocolConfigurations': protocols,
             'subnets': [{'id': subnet_id}],
@@ -176,3 +187,15 @@ def test_cancel_lbaas(self):
         uuid = 'aa-bb-cc'
         self.lb_mgr.cancel_lbaas(uuid)
         self.assert_called_with('SoftLayer_Network_LBaaS_LoadBalancer', 'cancelLoadBalancer', args=(uuid,))
+
+    def test_get_lbaas_by_name(self):
+        name = SoftLayer_Network_LBaaS_LoadBalancer.getObject.get('name')
+        load_bal = self.lb_mgr.get_lbaas_by_name(name)
+        self.assert_called_with('SoftLayer_Network_LBaaS_LoadBalancer', 'getAllObjects')
+        self.assertIsNotNone(load_bal)
+
+    def test_get_lbaas_by_name_fails(self):
+        load_bal_mock = self.set_mock('SoftLayer_Network_LBaaS_LoadBalancer', 'getAllObjects')
+        load_bal_mock.return_value = []
+        name = 'test'
+        self.assertRaises(exceptions.SoftLayerError, self.lb_mgr.get_lbaas_by_name, name)