Skip to content

Commit 119d2b3

Browse files
Merge pull request #1685 from edsonarios/issue1682
Update command - slcli object-storage endpoints
2 parents c468ddb + 09e00c0 commit 119d2b3

File tree

4 files changed

+171
-63
lines changed

4 files changed

+171
-63
lines changed

SoftLayer/CLI/object_storage/list_endpoints.py

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,95 @@
22
# :license: MIT, see LICENSE for more details.
33

44
import click
5-
65
import SoftLayer
76
from SoftLayer.CLI import environment
87
from SoftLayer.CLI import formatting
8+
from SoftLayer import utils
99

1010

1111
@click.command(cls=SoftLayer.CLI.command.SLCommand, )
12+
@click.argument('identifier')
1213
@environment.pass_env
13-
def cli(env):
14+
def cli(env, identifier):
1415
"""List object storage endpoints."""
1516

1617
mgr = SoftLayer.ObjectStorageManager(env.client)
17-
endpoints = mgr.list_endpoints()
18+
endpoints = mgr.list_endpoints(identifier)
19+
20+
final_end_points = []
1821

19-
table = formatting.Table(['datacenter', 'public', 'private'])
22+
table = formatting.Table(['Location/Region', 'Url', 'EndPoint Type', 'Public/Private', 'Legacy'])
23+
table.align['Location/Region'] = 'l'
24+
table.align['Url'] = 'l'
2025
for endpoint in endpoints:
21-
table.add_row([
22-
endpoint['datacenter']['name'],
23-
endpoint['public'],
24-
endpoint['private'],
25-
])
26+
data = {
27+
'Location/Region': location_region(endpoint),
28+
'Url': endpoint['url'],
29+
'EndPoint Type': end_point_return(endpoint['region']),
30+
'Public/Private': public_private(endpoint['type']),
31+
'Legacy': endpoint['legacy']
32+
}
33+
final_end_points.append(data)
34+
35+
final_end_points = sort_endpoint(final_end_points)
36+
table = add_array_to_table(table, final_end_points)
2637

2738
env.fout(table)
39+
40+
41+
def add_array_to_table(table, array_datas):
42+
"""Add an array to a table"""
43+
for array in array_datas:
44+
table.add_row([array['Location/Region'],
45+
array['Url'],
46+
array['EndPoint Type'],
47+
array['Public/Private'],
48+
array['Legacy']])
49+
return table
50+
51+
52+
def end_point_return(endpoint):
53+
"""Returns end point type"""
54+
if endpoint == 'singleSite':
55+
return 'Single Site'
56+
if endpoint == 'regional':
57+
return 'Region'
58+
return 'Cross Region'
59+
60+
61+
def public_private(data):
62+
"""Returns public or private in capital letter"""
63+
if data == 'public':
64+
return 'Public'
65+
return 'Private'
66+
67+
68+
def location_region(endpoint):
69+
"""Returns location if it exists otherwise region"""
70+
if utils.lookup(endpoint, 'location'):
71+
return endpoint['location']
72+
return endpoint['region']
73+
74+
75+
def sort_endpoint(endpoints):
76+
"""Sort the all endpoints for public or private"""
77+
first_data = 0
78+
endpoint_type = ''
79+
if len(endpoints) > 0:
80+
endpoint_type = endpoints[first_data]['EndPoint Type']
81+
public = []
82+
private = []
83+
array_final = []
84+
for endpoint in endpoints:
85+
if endpoint['EndPoint Type'] != endpoint_type:
86+
endpoint_type = endpoint['EndPoint Type']
87+
array_final = array_final + public + private
88+
public.clear()
89+
private.clear()
90+
if endpoint['Public/Private'] == 'Public':
91+
public.append(endpoint)
92+
else:
93+
private.append(endpoint)
94+
95+
array_final = array_final + public + private
96+
return array_final

SoftLayer/managers/object_storage.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
id,username,notes,vendorName,serviceResource
1414
]'''
1515

16-
ENDPOINT_MASK = '''mask(SoftLayer_Network_Storage_Hub_Swift)[
17-
id,storageNodes[id,backendIpAddress,frontendIpAddress,datacenter]
18-
]'''
19-
2016

2117
class ObjectStorageManager(utils.IdentifierMixin, object):
2218
"""Manager for SoftLayer Object Storage accounts.
@@ -40,26 +36,10 @@ def list_accounts(self, object_mask=None, object_filter=None, limit=10):
4036
filter=object_filter,
4137
limit=limit)
4238

43-
def list_endpoints(self):
39+
def list_endpoints(self, identifier):
4440
"""Lists the known object storage endpoints."""
45-
_filter = {
46-
'hubNetworkStorage': {'vendorName': {'operation': 'Swift'}},
47-
}
48-
endpoints = []
49-
network_storage = self.client.call('Account',
50-
'getHubNetworkStorage',
51-
mask=ENDPOINT_MASK,
52-
limit=1,
53-
filter=_filter)
54-
if network_storage:
55-
for node in network_storage['storageNodes']:
56-
endpoints.append({
57-
'datacenter': node['datacenter'],
58-
'public': node['frontendIpAddress'],
59-
'private': node['backendIpAddress'],
60-
})
61-
62-
return endpoints
41+
42+
return self.client.call('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getEndpoints', id=identifier)
6343

6444
def create_credential(self, identifier):
6545
"""Create object storage credential.

tests/CLI/modules/object_storage_tests.py

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,61 @@ def test_list_accounts(self):
2222
)
2323

2424
def test_list_endpoints(self):
25-
accounts = self.set_mock('SoftLayer_Account', 'getHubNetworkStorage')
26-
accounts.return_value = {
27-
'storageNodes': [{
28-
'datacenter': {'name': 'dal05'},
29-
'frontendIpAddress': 'https://dal05/auth/v1.0/',
30-
'backendIpAddress': 'https://dal05/auth/v1.0/'}
31-
],
32-
}
33-
34-
result = self.run_command(['object-storage', 'endpoints'])
25+
accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getEndpoints')
26+
accounts.return_value = [
27+
{
28+
'legacy': False,
29+
'region': 'us-geo',
30+
'type': 'public',
31+
'url': 's3.us.cloud-object-storage.appdomain.cloud'
32+
},
33+
{
34+
'legacy': False,
35+
'region': 'us-geo',
36+
'type': 'private',
37+
'url': 's3.private.us.cloud-object-storage.appdomain.cloud'
38+
},
39+
{
40+
'legacy': True,
41+
'location': 'dal06',
42+
'region': 'regional',
43+
'type': 'public',
44+
'url': 's3.dal.cloud-object-storage.appdomain.cloud'
45+
},
46+
{
47+
'legacy': True,
48+
'location': 'ams03',
49+
'region': 'singleSite',
50+
'type': 'private',
51+
'url': 's3.ams.cloud-object-storage.appdomain.cloud'
52+
},
53+
]
54+
55+
result = self.run_command(['object-storage', 'endpoints', '123'])
3556

3657
self.assert_no_fail(result)
3758
self.assertEqual(json.loads(result.output),
38-
[{'datacenter': 'dal05',
39-
'private': 'https://dal05/auth/v1.0/',
40-
'public': 'https://dal05/auth/v1.0/'}])
59+
[{'Legacy': False,
60+
'EndPoint Type': 'Cross Region',
61+
'Public/Private': 'Public',
62+
'Location/Region': 'us-geo',
63+
'Url': 's3.us.cloud-object-storage.appdomain.cloud'},
64+
{'Legacy': False,
65+
'EndPoint Type': 'Cross Region',
66+
'Public/Private': 'Private',
67+
'Location/Region': 'us-geo',
68+
'Url': 's3.private.us.cloud-object-storage.appdomain.cloud'},
69+
{'Legacy': True,
70+
'EndPoint Type': 'Region',
71+
'Public/Private': 'Public',
72+
'Location/Region': 'dal06',
73+
'Url': 's3.dal.cloud-object-storage.appdomain.cloud'},
74+
{'Legacy': True,
75+
'EndPoint Type': 'Single Site',
76+
'Public/Private': 'Private',
77+
'Location/Region': 'ams03',
78+
'Url': 's3.ams.cloud-object-storage.appdomain.cloud'}
79+
])
4180

4281
def test_create_credential(self):
4382
result = self.run_command(['object-storage', 'credential', 'create', '100'])

tests/managers/object_storage_tests.py

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,48 @@ def test_list_accounts(self):
2121
fixtures.SoftLayer_Account.getHubNetworkStorage)
2222

2323
def test_list_endpoints(self):
24-
accounts = self.set_mock('SoftLayer_Account', 'getHubNetworkStorage')
25-
accounts.return_value = {
26-
'storageNodes': [{
27-
'datacenter': {'name': 'dal05'},
28-
'frontendIpAddress': 'https://dal05/auth/v1.0/',
29-
'backendIpAddress': 'https://dal05/auth/v1.0/'}
30-
],
31-
}
32-
endpoints = self.object_storage.list_endpoints()
24+
accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getEndpoints')
25+
accounts.return_value = [
26+
{
27+
'legacy': False,
28+
'region': 'us-geo',
29+
'type': 'public',
30+
'url': 's3.us.cloud-object-storage.appdomain.cloud'
31+
},
32+
{
33+
'legacy': False,
34+
'region': 'us-geo',
35+
'type': 'private',
36+
'url': 's3.private.us.cloud-object-storage.appdomain.cloud'
37+
}
38+
]
39+
40+
endpoints = self.object_storage.list_endpoints(123)
3341
self.assertEqual(endpoints,
34-
[{'datacenter': {'name': 'dal05'},
35-
'private': 'https://dal05/auth/v1.0/',
36-
'public': 'https://dal05/auth/v1.0/'}])
42+
[
43+
{
44+
'legacy': False,
45+
'region': 'us-geo',
46+
'type': 'public',
47+
'url': 's3.us.cloud-object-storage.appdomain.cloud'
48+
},
49+
{
50+
'legacy': False,
51+
'region': 'us-geo',
52+
'type': 'private',
53+
'url': 's3.private.us.cloud-object-storage.appdomain.cloud'
54+
}
55+
])
3756

3857
def test_list_endpoints_no_results(self):
39-
accounts = self.set_mock('SoftLayer_Account', 'getHubNetworkStorage')
40-
accounts.return_value = {
41-
'storageNodes': [],
42-
}
43-
endpoints = self.object_storage.list_endpoints()
58+
accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getEndpoints')
59+
accounts.return_value = [
60+
{}
61+
]
62+
63+
endpoints = self.object_storage.list_endpoints(123)
4464
self.assertEqual(endpoints,
45-
[])
65+
[{}])
4666

4767
def test_create_credential(self):
4868
accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'credentialCreate')

0 commit comments

Comments
 (0)