Skip to content

Commit 056cb87

Browse files
Merge pull request #1415 from ATGE/issues/1410
Add username lookup to slcli object-storage credential
2 parents 6fb1e64 + e51759d commit 056cb87

File tree

8 files changed

+101
-103
lines changed

8 files changed

+101
-103
lines changed

Diff for: SoftLayer/CLI/object_storage/credential/create.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import SoftLayer
77
from SoftLayer.CLI import environment
88
from SoftLayer.CLI import formatting
9+
from SoftLayer.CLI import helpers
910

1011

1112
@click.command()
@@ -15,14 +16,15 @@ def cli(env, identifier):
1516
"""Create credentials for an IBM Cloud Object Storage Account"""
1617

1718
mgr = SoftLayer.ObjectStorageManager(env.client)
18-
credential = mgr.create_credential(identifier)
19+
storage_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'Object Storage')
20+
credential = mgr.create_credential(storage_id)
1921
table = formatting.Table(['id', 'password', 'username', 'type_name'])
2022
table.sortby = 'id'
2123
table.add_row([
22-
credential['id'],
23-
credential['password'],
24-
credential['username'],
25-
credential['type']['name']
24+
credential.get('id'),
25+
credential.get('password'),
26+
credential.get('username'),
27+
credential.get('type', {}).get('name')
2628
])
2729

2830
env.fout(table)

Diff for: SoftLayer/CLI/object_storage/credential/delete.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import SoftLayer
77
from SoftLayer.CLI import environment
8+
from SoftLayer.CLI import helpers
89

910

1011
@click.command()
@@ -16,6 +17,7 @@ def cli(env, identifier, credential_id):
1617
"""Delete the credential of an Object Storage Account."""
1718

1819
mgr = SoftLayer.ObjectStorageManager(env.client)
19-
credential = mgr.delete_credential(identifier, credential_id=credential_id)
20+
storage_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'Object Storage')
21+
credential = mgr.delete_credential(storage_id, credential_id=credential_id)
2022

2123
env.fout(credential)

Diff for: SoftLayer/CLI/object_storage/credential/limit.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import SoftLayer
77
from SoftLayer.CLI import environment
88
from SoftLayer.CLI import formatting
9+
from SoftLayer.CLI import helpers
910

1011

1112
@click.command()
@@ -15,7 +16,8 @@ def cli(env, identifier):
1516
"""Credential limits for this IBM Cloud Object Storage account."""
1617

1718
mgr = SoftLayer.ObjectStorageManager(env.client)
18-
credential_limit = mgr.limit_credential(identifier)
19+
storage_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'Object Storage')
20+
credential_limit = mgr.limit_credential(storage_id)
1921
table = formatting.Table(['limit'])
2022
table.add_row([
2123
credential_limit,

Diff for: SoftLayer/CLI/object_storage/credential/list.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import SoftLayer
77
from SoftLayer.CLI import environment
88
from SoftLayer.CLI import formatting
9+
from SoftLayer.CLI import helpers
910

1011

1112
@click.command()
@@ -15,15 +16,17 @@ def cli(env, identifier):
1516
"""Retrieve credentials used for generating an AWS signature. Max of 2."""
1617

1718
mgr = SoftLayer.ObjectStorageManager(env.client)
18-
credential_list = mgr.list_credential(identifier)
19+
storage_id = helpers.resolve_id(mgr.resolve_ids, identifier, 'Object Storage')
20+
credential_list = mgr.list_credential(storage_id)
21+
1922
table = formatting.Table(['id', 'password', 'username', 'type_name'])
2023

2124
for credential in credential_list:
2225
table.add_row([
23-
credential['id'],
24-
credential['password'],
25-
credential['username'],
26-
credential['type']['name']
26+
credential.get('id'),
27+
credential.get('password'),
28+
credential.get('username'),
29+
credential.get('type', {}).get('name')
2730
])
2831

2932
env.fout(table)

Diff for: SoftLayer/fixtures/SoftLayer_Network_Storage_Hub_Cleversafe_Account.py

+4
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@
4646
"storageLocation": "us-standard"
4747
}
4848
]
49+
50+
getCredentialLimit = 2
51+
52+
credentialDelete = True

Diff for: SoftLayer/managers/object_storage.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
:license: MIT, see LICENSE for more details.
77
"""
88

9+
from SoftLayer.exceptions import SoftLayerError
10+
from SoftLayer import utils
11+
912
LIST_ACCOUNTS_MASK = '''mask[
1013
id,username,notes,vendorName,serviceResource
1114
]'''
@@ -15,7 +18,7 @@
1518
]'''
1619

1720

18-
class ObjectStorageManager(object):
21+
class ObjectStorageManager(utils.IdentifierMixin, object):
1922
"""Manager for SoftLayer Object Storage accounts.
2023
2124
See product information here: https://www.ibm.com/cloud/object-storage
@@ -26,11 +29,16 @@ class ObjectStorageManager(object):
2629

2730
def __init__(self, client):
2831
self.client = client
32+
self.resolvers = [self._get_id_from_username]
2933

30-
def list_accounts(self, limit=10):
34+
def list_accounts(self, object_mask=None, object_filter=None, limit=10):
3135
"""Lists your object storage accounts."""
32-
return self.client.call('Account', 'getHubNetworkStorage',
33-
mask=LIST_ACCOUNTS_MASK, iter=True, limit=limit)
36+
object_mask = object_mask if object_mask else LIST_ACCOUNTS_MASK
37+
return self.client.call('Account',
38+
'getHubNetworkStorage',
39+
mask=object_mask,
40+
filter=object_filter,
41+
limit=limit)
3442

3543
def list_endpoints(self):
3644
"""Lists the known object storage endpoints."""
@@ -96,3 +104,19 @@ def list_credential(self, identifier):
96104

97105
return self.client.call('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getCredentials',
98106
id=identifier)
107+
108+
def _get_id_from_username(self, username):
109+
"""Looks up a username's id
110+
111+
:param string username: Username to lookup
112+
:returns: The id that matches username.
113+
"""
114+
_mask = "mask[id,username]"
115+
_filter = {'hubNetworkStorage': {'username': utils.query_filter(username)}}
116+
account = self.list_accounts(_mask, _filter)
117+
if len(account) == 1:
118+
return [account[0]['id']]
119+
elif len(account) > 1:
120+
raise SoftLayerError("Multiple object storage accounts found with the name: {}".format(username))
121+
else:
122+
raise SoftLayerError("Unable to find object storage account id for: {}".format(username))

Diff for: tests/CLI/modules/object_storage_tests.py

+24-47
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
:license: MIT, see LICENSE for more details.
66
"""
77
import json
8+
from unittest import mock
89

910
from SoftLayer import testing
1011

@@ -39,68 +40,44 @@ def test_list_endpoints(self):
3940
'public': 'https://dal05/auth/v1.0/'}])
4041

4142
def test_create_credential(self):
42-
accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'credentialCreate')
43-
accounts.return_value = {
44-
"accountId": "12345",
45-
"createDate": "2019-04-05T13:25:25-06:00",
46-
"id": 11111,
47-
"password": "nwUEUsx6PiEoN0B1Xe9z9hUCy",
48-
"username": "XfHhBNBPlPdl",
49-
"type": {
50-
"description": "A credential for generating S3 Compatible Signatures.",
51-
"keyName": "S3_COMPATIBLE_SIGNATURE",
52-
"name": "S3 Compatible Signature"
53-
}
54-
}
55-
5643
result = self.run_command(['object-storage', 'credential', 'create', '100'])
44+
self.assert_no_fail(result)
5745

46+
@mock.patch('SoftLayer.CLI.helpers.resolve_id')
47+
def test_create_credential_by_username(self, resolve_id_mock):
48+
resolve_id_mock.return_value = 100
49+
result = self.run_command(['object-storage', 'credential', 'create', 'test'])
5850
self.assert_no_fail(result)
59-
self.assertEqual(json.loads(result.output),
60-
[{'id': 11111,
61-
'password': 'nwUEUsx6PiEoN0B1Xe9z9hUCy',
62-
'type_name': 'S3 Compatible Signature',
63-
'username': 'XfHhBNBPlPdl'}]
64-
)
6551

6652
def test_delete_credential(self):
67-
accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'credentialDelete')
68-
accounts.return_value = True
69-
7053
result = self.run_command(['object-storage', 'credential', 'delete', '-c', 100, '100'])
71-
7254
self.assert_no_fail(result)
7355
self.assertEqual(result.output, 'True\n')
7456

75-
def test_limit_credential(self):
76-
accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getCredentialLimit')
77-
accounts.return_value = 2
57+
@mock.patch('SoftLayer.CLI.helpers.resolve_id')
58+
def test_delete_credential_by_username(self, resolve_id_mock):
59+
resolve_id_mock.return_value = 100
60+
result = self.run_command(['object-storage', 'credential', 'delete', 'test'])
61+
self.assert_no_fail(result)
7862

63+
def test_limit_credential(self):
7964
result = self.run_command(['object-storage', 'credential', 'limit', '100'])
8065

8166
self.assert_no_fail(result)
82-
self.assertEqual(json.loads(result.output), [{'limit': 2}])
67+
self.assertIn('limit', result.output)
8368

84-
def test_list_credential(self):
85-
accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getCredentials')
86-
accounts.return_value = [{'id': 1103123,
87-
'password': 'nwUEUsx6PiEoN0B1Xe9z9hUCyXM',
88-
'type': {'name': 'S3 Compatible Signature'},
89-
'username': 'XfHhBNBPlPdlWya'},
90-
{'id': 1103333,
91-
'password': 'nwUEUsx6PiEoN0B1Xe9z9',
92-
'type': {'name': 'S3 Compatible Signature'},
93-
'username': 'XfHhBNBPlPd'}]
69+
@mock.patch('SoftLayer.CLI.helpers.resolve_id')
70+
def test_limit_credential_by_username(self, resolve_id_mock):
71+
resolve_id_mock.return_value = 100
72+
result = self.run_command(['object-storage', 'credential', 'limit', 'test'])
73+
self.assert_no_fail(result)
9474

75+
def test_list_credential(self):
9576
result = self.run_command(['object-storage', 'credential', 'list', '100'])
77+
self.assert_no_fail(result)
9678

79+
@mock.patch('SoftLayer.CLI.helpers.resolve_id')
80+
def test_list_credential_by_username(self, resolve_id_mock):
81+
resolve_id_mock.return_value = 100
82+
result = self.run_command(['object-storage', 'credential', 'list', 'test'])
9783
self.assert_no_fail(result)
98-
self.assertEqual(json.loads(result.output),
99-
[{'id': 1103123,
100-
'password': 'nwUEUsx6PiEoN0B1Xe9z9hUCyXM',
101-
'type_name': 'S3 Compatible Signature',
102-
'username': 'XfHhBNBPlPdlWya'},
103-
{'id': 1103333,
104-
'password': 'nwUEUsx6PiEoN0B1Xe9z9',
105-
'type_name': 'S3 Compatible Signature',
106-
'username': 'XfHhBNBPlPd'}])

Diff for: tests/managers/object_storage_tests.py

+24-40
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
import SoftLayer
88
from SoftLayer import fixtures
9+
from SoftLayer import SoftLayerError
910
from SoftLayer import testing
1011

1112

@@ -79,43 +80,26 @@ def test_limit_credential(self):
7980
self.assertEqual(credential, 2)
8081

8182
def test_list_credential(self):
82-
accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getCredentials')
83-
accounts.return_value = [
84-
{
85-
"id": 1103123,
86-
"password": "nwUEUsx6PiEoN0B1Xe9z9hUCyXsf4sf",
87-
"username": "XfHhBNBPlPdlWyaP3fsd",
88-
"type": {
89-
"name": "S3 Compatible Signature"
90-
}
91-
},
92-
{
93-
"id": 1102341,
94-
"password": "nwUEUsx6PiEoN0B1Xe9z9hUCyXMkAF",
95-
"username": "XfHhBNBPlPdlWyaP",
96-
"type": {
97-
"name": "S3 Compatible Signature"
98-
}
99-
}
100-
]
101-
credential = self.object_storage.list_credential(100)
102-
self.assertEqual(credential,
103-
[
104-
{
105-
"id": 1103123,
106-
"password": "nwUEUsx6PiEoN0B1Xe9z9hUCyXsf4sf",
107-
"username": "XfHhBNBPlPdlWyaP3fsd",
108-
"type": {
109-
"name": "S3 Compatible Signature"
110-
}
111-
},
112-
{
113-
"id": 1102341,
114-
"password": "nwUEUsx6PiEoN0B1Xe9z9hUCyXMkAF",
115-
"username": "XfHhBNBPlPdlWyaP",
116-
"type": {
117-
"name": "S3 Compatible Signature"
118-
}
119-
}
120-
]
121-
)
83+
credentials = self.object_storage.list_credential(100)
84+
self.assertIsInstance(credentials, list)
85+
self.assert_called_with('SoftLayer_Network_Storage_Hub_Cleversafe_Account',
86+
'getCredentials',
87+
identifier=100)
88+
89+
def test_resolve_ids(self):
90+
accounts = self.set_mock('SoftLayer_Account', 'getHubNetworkStorage')
91+
accounts.return_value = [{'id': 12345, 'username': 'test'}]
92+
identifier = self.object_storage.resolve_ids('test')
93+
self.assertEqual(identifier, [12345])
94+
self.assert_called_with('SoftLayer_Account', 'getHubNetworkStorage')
95+
96+
def test_resolve_ids_fail_multiple(self):
97+
accounts = self.set_mock('SoftLayer_Account', 'getHubNetworkStorage')
98+
accounts.return_value = [{'id': 12345, 'username': 'test'},
99+
{'id': 12345, 'username': 'test'}]
100+
self.assertRaises(SoftLayerError, self.object_storage.resolve_ids, 'test')
101+
102+
def test_resolve_ids_fail_no_found(self):
103+
accounts = self.set_mock('SoftLayer_Account', 'getHubNetworkStorage')
104+
accounts.return_value = []
105+
self.assertRaises(SoftLayerError, self.object_storage.resolve_ids, 'test')

0 commit comments

Comments
 (0)