Skip to content

Commit 83a4f29

Browse files
committed
updated command +-------------------------------+
| datacenter | public | private | |------------+--------+---------| +-------------------------------+ and updated unit test
1 parent 950a54b commit 83a4f29

File tree

4 files changed

+159
-63
lines changed

4 files changed

+159
-63
lines changed

SoftLayer/CLI/object_storage/list_endpoints.py

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,83 @@
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(['Legacy', 'EndPoint Type', 'Public/Private', 'Location/Region', 'Url'])
2023
for endpoint in endpoints:
21-
table.add_row([
22-
endpoint['datacenter']['name'],
23-
endpoint['public'],
24-
endpoint['private'],
25-
])
24+
data = [endpoint['legacy'], end_point_return(endpoint['region']), public_private(endpoint['type']),
25+
location_region(endpoint), endpoint['url']]
26+
final_end_points.append(data)
27+
28+
final_end_points = sort_endpoint(final_end_points)
29+
table = add_array_to_table(table, final_end_points)
2630

2731
env.fout(table)
32+
33+
34+
def add_array_to_table(table, array_datas):
35+
"""Add an array to a table"""
36+
for array in array_datas:
37+
table.add_row([array[0], array[1], array[2], array[3], array[4]])
38+
return table
39+
40+
41+
def end_point_return(endpoint):
42+
"""Returns end point type"""
43+
if endpoint == 'singleSite':
44+
return 'Single Site'
45+
if endpoint == 'regional':
46+
return 'Region'
47+
return 'Cross Region'
48+
49+
50+
def public_private(data):
51+
"""Returns public or private in capital letter"""
52+
if data == 'public':
53+
return 'Public'
54+
return 'Private'
55+
56+
57+
def location_region(endpoint):
58+
"""Returns location if it exists otherwise region"""
59+
if utils.lookup(endpoint, 'location'):
60+
return endpoint['location']
61+
return endpoint['region']
62+
63+
64+
def sort_endpoint(endpoints):
65+
"""Sort the all endpoints for public or private"""
66+
endpoint_type = ''
67+
if len(endpoints) > 0:
68+
endpoint_type = endpoints[0][1]
69+
public = []
70+
private = []
71+
array_final = []
72+
for endpoint in endpoints:
73+
if endpoint[1] != endpoint_type:
74+
endpoint_type = endpoint[1]
75+
array_final = array_final + public + private
76+
public.clear()
77+
private.clear()
78+
if endpoint[2] == 'Public':
79+
public.append(endpoint)
80+
else:
81+
private.append(endpoint)
82+
83+
array_final = array_final + public + private
84+
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)