Repository: softlayer/softlayer-python
Pull request #1685 (2022-07-06 16:43:08)

Update command - slcli object-storage endpoints
Issue link: #1682 
```
softlayer-python/ (issue1682 *) $ slcli object-storage endpoints 123456
┌────────┬───────────────┬────────────────┬─────────────────┬──────────────────────────────────────────────────────────┐
│ Legacy │ EndPoint Type │ Public/Private │ Location/Region │                           Url                            │
├────────┼───────────────┼────────────────┼─────────────────┼──────────────────────────────────────────────────────────┤
│ False  │ Cross Region  │     Public     │     us-geo      │        s3.us.cloud-object-storage.appdomain.cloud        │
│ False  │ Cross Region  │     Public     │     Dallas      │      s3.dal.us.cloud-object-storage.appdomain.cloud      │
│ False  │ Cross Region  │     Public     │   Washington    │      s3.wdc.us.cloud-object-storage.appdomain.cloud      │
│ False  │ Cross Region  │     Public     │    San Jose     │      s3.sjc.us.cloud-object-storage.appdomain.cloud      │
│ False  │ Cross Region  │     Public     │     eu-geo      │        s3.eu.cloud-object-storage.appdomain.cloud        │
│ False  │ Cross Region  │     Public     │    Amsterdam    │      s3.ams.eu.cloud-object-storage.appdomain.cloud      │
│ False  │ Cross Region  │     Public     │    Frankfurt    │      s3.fra.eu.cloud-object-storage.appdomain.cloud      │
│ False  │ Cross Region  │     Public     │      Milan      │      s3.mil.eu.cloud-object-storage.appdomain.cloud      │
│ False  │ Cross Region  │     Public     │     ap-geo      │        s3.ap.cloud-object-storage.appdomain.cloud        │
│ False  │ Cross Region  │     Public     │      Tokyo      │      s3.tok.ap.cloud-object-storage.appdomain.cloud      │
│ False  │ Cross Region  │     Public     │      Seoul      │      s3.seo.ap.cloud-object-storage.appdomain.cloud      │
│ False  │ Cross Region  │     Public     │    Hong Kong    │      s3.hkg.ap.cloud-object-storage.appdomain.cloud      │
│ False  │ Cross Region  │    Private     │     us-geo      │    s3.private.us.cloud-object-storage.appdomain.cloud    │
│ False  │ Cross Region  │    Private     │     Dallas      │  s3.private.dal.us.cloud-object-storage.appdomain.cloud  │
│ False  │ Cross Region  │    Private     │   Washington    │  s3.private.wdc.us.cloud-object-storage.appdomain.cloud  │
│ False  │ Cross Region  │    Private     │    San Jose     │  s3.private.sjc.us.cloud-object-storage.appdomain.cloud  │
│ False  │ Cross Region  │    Private     │     eu-geo      │    s3.private.eu.cloud-object-storage.appdomain.cloud    │
│ False  │ Cross Region  │    Private     │    Amsterdam    │  s3.private.ams.eu.cloud-object-storage.appdomain.cloud  │
│ False  │ Cross Region  │    Private     │    Frankfurt    │  s3.private.fra.eu.cloud-object-storage.appdomain.cloud  │
```


Patch:
diff --git a/SoftLayer/CLI/object_storage/list_endpoints.py b/SoftLayer/CLI/object_storage/list_endpoints.py
index f78fd46f..a437e473 100644
--- a/SoftLayer/CLI/object_storage/list_endpoints.py
+++ b/SoftLayer/CLI/object_storage/list_endpoints.py
@@ -2,26 +2,95 @@
 # :license: MIT, see LICENSE for more details.
 
 import click
-
 import SoftLayer
 from SoftLayer.CLI import environment
 from SoftLayer.CLI import formatting
+from SoftLayer import utils
 
 
 @click.command(cls=SoftLayer.CLI.command.SLCommand, )
+@click.argument('identifier')
 @environment.pass_env
-def cli(env):
+def cli(env, identifier):
     """List object storage endpoints."""
 
     mgr = SoftLayer.ObjectStorageManager(env.client)
-    endpoints = mgr.list_endpoints()
+    endpoints = mgr.list_endpoints(identifier)
+
+    final_end_points = []
 
-    table = formatting.Table(['datacenter', 'public', 'private'])
+    table = formatting.Table(['Location/Region', 'Url', 'EndPoint Type', 'Public/Private', 'Legacy'])
+    table.align['Location/Region'] = 'l'
+    table.align['Url'] = 'l'
     for endpoint in endpoints:
-        table.add_row([
-            endpoint['datacenter']['name'],
-            endpoint['public'],
-            endpoint['private'],
-        ])
+        data = {
+            'Location/Region': location_region(endpoint),
+            'Url': endpoint['url'],
+            'EndPoint Type': end_point_return(endpoint['region']),
+            'Public/Private': public_private(endpoint['type']),
+            'Legacy': endpoint['legacy']
+        }
+        final_end_points.append(data)
+
+    final_end_points = sort_endpoint(final_end_points)
+    table = add_array_to_table(table, final_end_points)
 
     env.fout(table)
+
+
+def add_array_to_table(table, array_datas):
+    """Add an array to a table"""
+    for array in array_datas:
+        table.add_row([array['Location/Region'],
+                       array['Url'],
+                       array['EndPoint Type'],
+                       array['Public/Private'],
+                       array['Legacy']])
+    return table
+
+
+def end_point_return(endpoint):
+    """Returns end point type"""
+    if endpoint == 'singleSite':
+        return 'Single Site'
+    if endpoint == 'regional':
+        return 'Region'
+    return 'Cross Region'
+
+
+def public_private(data):
+    """Returns public or private in capital letter"""
+    if data == 'public':
+        return 'Public'
+    return 'Private'
+
+
+def location_region(endpoint):
+    """Returns location if it exists otherwise region"""
+    if utils.lookup(endpoint, 'location'):
+        return endpoint['location']
+    return endpoint['region']
+
+
+def sort_endpoint(endpoints):
+    """Sort the all endpoints for public or private"""
+    first_data = 0
+    endpoint_type = ''
+    if len(endpoints) > 0:
+        endpoint_type = endpoints[first_data]['EndPoint Type']
+    public = []
+    private = []
+    array_final = []
+    for endpoint in endpoints:
+        if endpoint['EndPoint Type'] != endpoint_type:
+            endpoint_type = endpoint['EndPoint Type']
+            array_final = array_final + public + private
+            public.clear()
+            private.clear()
+        if endpoint['Public/Private'] == 'Public':
+            public.append(endpoint)
+        else:
+            private.append(endpoint)
+
+    array_final = array_final + public + private
+    return array_final
diff --git a/SoftLayer/managers/object_storage.py b/SoftLayer/managers/object_storage.py
index 5efadeab..a0ed5012 100644
--- a/SoftLayer/managers/object_storage.py
+++ b/SoftLayer/managers/object_storage.py
@@ -13,10 +13,6 @@
     id,username,notes,vendorName,serviceResource
 ]'''
 
-ENDPOINT_MASK = '''mask(SoftLayer_Network_Storage_Hub_Swift)[
-    id,storageNodes[id,backendIpAddress,frontendIpAddress,datacenter]
-]'''
-
 
 class ObjectStorageManager(utils.IdentifierMixin, object):
     """Manager for SoftLayer Object Storage accounts.
@@ -40,26 +36,10 @@ def list_accounts(self, object_mask=None, object_filter=None, limit=10):
                                 filter=object_filter,
                                 limit=limit)
 
-    def list_endpoints(self):
+    def list_endpoints(self, identifier):
         """Lists the known object storage endpoints."""
-        _filter = {
-            'hubNetworkStorage': {'vendorName': {'operation': 'Swift'}},
-        }
-        endpoints = []
-        network_storage = self.client.call('Account',
-                                           'getHubNetworkStorage',
-                                           mask=ENDPOINT_MASK,
-                                           limit=1,
-                                           filter=_filter)
-        if network_storage:
-            for node in network_storage['storageNodes']:
-                endpoints.append({
-                    'datacenter': node['datacenter'],
-                    'public': node['frontendIpAddress'],
-                    'private': node['backendIpAddress'],
-                })
-
-        return endpoints
+
+        return self.client.call('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getEndpoints', id=identifier)
 
     def create_credential(self, identifier):
         """Create object storage credential.


Test patch:
diff --git a/tests/CLI/modules/object_storage_tests.py b/tests/CLI/modules/object_storage_tests.py
index e76fddff..38612a1c 100644
--- a/tests/CLI/modules/object_storage_tests.py
+++ b/tests/CLI/modules/object_storage_tests.py
@@ -22,22 +22,61 @@ def test_list_accounts(self):
                          )
 
     def test_list_endpoints(self):
-        accounts = self.set_mock('SoftLayer_Account', 'getHubNetworkStorage')
-        accounts.return_value = {
-            'storageNodes': [{
-                'datacenter': {'name': 'dal05'},
-                'frontendIpAddress': 'https://dal05/auth/v1.0/',
-                'backendIpAddress': 'https://dal05/auth/v1.0/'}
-            ],
-        }
-
-        result = self.run_command(['object-storage', 'endpoints'])
+        accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getEndpoints')
+        accounts.return_value = [
+            {
+                'legacy': False,
+                'region': 'us-geo',
+                'type': 'public',
+                'url': 's3.us.cloud-object-storage.appdomain.cloud'
+            },
+            {
+                'legacy': False,
+                'region': 'us-geo',
+                'type': 'private',
+                'url': 's3.private.us.cloud-object-storage.appdomain.cloud'
+            },
+            {
+                'legacy': True,
+                'location': 'dal06',
+                'region': 'regional',
+                'type': 'public',
+                'url': 's3.dal.cloud-object-storage.appdomain.cloud'
+            },
+            {
+                'legacy': True,
+                'location': 'ams03',
+                'region': 'singleSite',
+                'type': 'private',
+                'url': 's3.ams.cloud-object-storage.appdomain.cloud'
+            },
+        ]
+
+        result = self.run_command(['object-storage', 'endpoints', '123'])
 
         self.assert_no_fail(result)
         self.assertEqual(json.loads(result.output),
-                         [{'datacenter': 'dal05',
-                           'private': 'https://dal05/auth/v1.0/',
-                           'public': 'https://dal05/auth/v1.0/'}])
+                         [{'Legacy': False,
+                           'EndPoint Type': 'Cross Region',
+                           'Public/Private': 'Public',
+                           'Location/Region': 'us-geo',
+                           'Url': 's3.us.cloud-object-storage.appdomain.cloud'},
+                          {'Legacy': False,
+                           'EndPoint Type': 'Cross Region',
+                           'Public/Private': 'Private',
+                           'Location/Region': 'us-geo',
+                           'Url': 's3.private.us.cloud-object-storage.appdomain.cloud'},
+                          {'Legacy': True,
+                           'EndPoint Type': 'Region',
+                           'Public/Private': 'Public',
+                           'Location/Region': 'dal06',
+                           'Url': 's3.dal.cloud-object-storage.appdomain.cloud'},
+                          {'Legacy': True,
+                           'EndPoint Type': 'Single Site',
+                           'Public/Private': 'Private',
+                           'Location/Region': 'ams03',
+                           'Url': 's3.ams.cloud-object-storage.appdomain.cloud'}
+                          ])
 
     def test_create_credential(self):
         result = self.run_command(['object-storage', 'credential', 'create', '100'])
diff --git a/tests/managers/object_storage_tests.py b/tests/managers/object_storage_tests.py
index 16081cc8..5d2b0783 100644
--- a/tests/managers/object_storage_tests.py
+++ b/tests/managers/object_storage_tests.py
@@ -21,28 +21,48 @@ def test_list_accounts(self):
                          fixtures.SoftLayer_Account.getHubNetworkStorage)
 
     def test_list_endpoints(self):
-        accounts = self.set_mock('SoftLayer_Account', 'getHubNetworkStorage')
-        accounts.return_value = {
-            'storageNodes': [{
-                'datacenter': {'name': 'dal05'},
-                'frontendIpAddress': 'https://dal05/auth/v1.0/',
-                'backendIpAddress': 'https://dal05/auth/v1.0/'}
-            ],
-        }
-        endpoints = self.object_storage.list_endpoints()
+        accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getEndpoints')
+        accounts.return_value = [
+            {
+                'legacy': False,
+                'region': 'us-geo',
+                'type': 'public',
+                'url': 's3.us.cloud-object-storage.appdomain.cloud'
+            },
+            {
+                'legacy': False,
+                'region': 'us-geo',
+                'type': 'private',
+                'url': 's3.private.us.cloud-object-storage.appdomain.cloud'
+            }
+        ]
+
+        endpoints = self.object_storage.list_endpoints(123)
         self.assertEqual(endpoints,
-                         [{'datacenter': {'name': 'dal05'},
-                           'private': 'https://dal05/auth/v1.0/',
-                           'public': 'https://dal05/auth/v1.0/'}])
+                         [
+                             {
+                                 'legacy': False,
+                                 'region': 'us-geo',
+                                 'type': 'public',
+                                 'url': 's3.us.cloud-object-storage.appdomain.cloud'
+                             },
+                             {
+                                 'legacy': False,
+                                 'region': 'us-geo',
+                                 'type': 'private',
+                                 'url': 's3.private.us.cloud-object-storage.appdomain.cloud'
+                             }
+                         ])
 
     def test_list_endpoints_no_results(self):
-        accounts = self.set_mock('SoftLayer_Account', 'getHubNetworkStorage')
-        accounts.return_value = {
-            'storageNodes': [],
-        }
-        endpoints = self.object_storage.list_endpoints()
+        accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getEndpoints')
+        accounts.return_value = [
+            {}
+        ]
+
+        endpoints = self.object_storage.list_endpoints(123)
         self.assertEqual(endpoints,
-                         [])
+                         [{}])
 
     def test_create_credential(self):
         accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'credentialCreate')