diff --git a/SoftLayer/CLI/object_storage/credential/__init__.py b/SoftLayer/CLI/object_storage/credential/__init__.py new file mode 100644 index 000000000..1f71754bb --- /dev/null +++ b/SoftLayer/CLI/object_storage/credential/__init__.py @@ -0,0 +1,42 @@ +"""Manages Object Storage S3 Credentials.""" +# :license: MIT, see LICENSE for more details. + +import importlib +import os + +import click + +CONTEXT = {'help_option_names': ['-h', '--help'], + 'max_content_width': 999} + + +class CapacityCommands(click.MultiCommand): + """Loads module for object storage S3 credentials related commands.""" + + def __init__(self, **attrs): + click.MultiCommand.__init__(self, **attrs) + self.path = os.path.dirname(__file__) + + def list_commands(self, ctx): + """List all sub-commands.""" + commands = [] + for filename in os.listdir(self.path): + if filename == '__init__.py': + continue + if filename.endswith('.py'): + commands.append(filename[:-3].replace("_", "-")) + commands.sort() + return commands + + def get_command(self, ctx, cmd_name): + """Get command for click.""" + path = "%s.%s" % (__name__, cmd_name) + path = path.replace("-", "_") + module = importlib.import_module(path) + return getattr(module, 'cli') + + +# Required to get the sub-sub-sub command to work. +@click.group(cls=CapacityCommands, context_settings=CONTEXT) +def cli(): + """Base command for all object storage credentials S3 related concerns""" diff --git a/SoftLayer/CLI/object_storage/credential/create.py b/SoftLayer/CLI/object_storage/credential/create.py new file mode 100644 index 000000000..934ac7651 --- /dev/null +++ b/SoftLayer/CLI/object_storage/credential/create.py @@ -0,0 +1,28 @@ +"""Create credentials for an IBM Cloud Object Storage Account.""" +# :license: MIT, see LICENSE for more details. + +import click + +import SoftLayer +from SoftLayer.CLI import environment +from SoftLayer.CLI import formatting + + +@click.command() +@click.argument('identifier') +@environment.pass_env +def cli(env, identifier): + """Create credentials for an IBM Cloud Object Storage Account""" + + mgr = SoftLayer.ObjectStorageManager(env.client) + credential = mgr.create_credential(identifier) + table = formatting.Table(['id', 'password', 'username', 'type_name']) + table.sortby = 'id' + table.add_row([ + credential['id'], + credential['password'], + credential['username'], + credential['type']['name'] + ]) + + env.fout(table) diff --git a/SoftLayer/CLI/object_storage/credential/delete.py b/SoftLayer/CLI/object_storage/credential/delete.py new file mode 100644 index 000000000..7b066ba59 --- /dev/null +++ b/SoftLayer/CLI/object_storage/credential/delete.py @@ -0,0 +1,21 @@ +"""Delete the credential of an Object Storage Account.""" +# :license: MIT, see LICENSE for more details. + +import click + +import SoftLayer +from SoftLayer.CLI import environment + + +@click.command() +@click.argument('identifier') +@click.option('--credential_id', '-c', type=click.INT, + help="This is the credential id associated with the volume") +@environment.pass_env +def cli(env, identifier, credential_id): + """Delete the credential of an Object Storage Account.""" + + mgr = SoftLayer.ObjectStorageManager(env.client) + credential = mgr.delete_credential(identifier, credential_id=credential_id) + + env.fout(credential) diff --git a/SoftLayer/CLI/object_storage/credential/limit.py b/SoftLayer/CLI/object_storage/credential/limit.py new file mode 100644 index 000000000..cc3ad115c --- /dev/null +++ b/SoftLayer/CLI/object_storage/credential/limit.py @@ -0,0 +1,24 @@ +""" Credential limits for this IBM Cloud Object Storage account.""" +# :license: MIT, see LICENSE for more details. + +import click + +import SoftLayer +from SoftLayer.CLI import environment +from SoftLayer.CLI import formatting + + +@click.command() +@click.argument('identifier') +@environment.pass_env +def cli(env, identifier): + """Credential limits for this IBM Cloud Object Storage account.""" + + mgr = SoftLayer.ObjectStorageManager(env.client) + credential_limit = mgr.limit_credential(identifier) + table = formatting.Table(['limit']) + table.add_row([ + credential_limit, + ]) + + env.fout(table) diff --git a/SoftLayer/CLI/object_storage/credential/list.py b/SoftLayer/CLI/object_storage/credential/list.py new file mode 100644 index 000000000..647e4224c --- /dev/null +++ b/SoftLayer/CLI/object_storage/credential/list.py @@ -0,0 +1,29 @@ +"""Retrieve credentials used for generating an AWS signature. Max of 2.""" +# :license: MIT, see LICENSE for more details. + +import click + +import SoftLayer +from SoftLayer.CLI import environment +from SoftLayer.CLI import formatting + + +@click.command() +@click.argument('identifier') +@environment.pass_env +def cli(env, identifier): + """Retrieve credentials used for generating an AWS signature. Max of 2.""" + + mgr = SoftLayer.ObjectStorageManager(env.client) + credential_list = mgr.list_credential(identifier) + table = formatting.Table(['id', 'password', 'username', 'type_name']) + + for credential in credential_list: + table.add_row([ + credential['id'], + credential['password'], + credential['username'], + credential['type']['name'] + ]) + + env.fout(table) diff --git a/SoftLayer/CLI/routes.py b/SoftLayer/CLI/routes.py index c04233c52..97551aeff 100644 --- a/SoftLayer/CLI/routes.py +++ b/SoftLayer/CLI/routes.py @@ -199,6 +199,8 @@ 'SoftLayer.CLI.object_storage.list_accounts:cli'), ('object-storage:endpoints', 'SoftLayer.CLI.object_storage.list_endpoints:cli'), + ('object-storage:credential', + 'SoftLayer.CLI.object_storage.credential:cli'), ('order', 'SoftLayer.CLI.order'), ('order:category-list', 'SoftLayer.CLI.order.category_list:cli'), diff --git a/SoftLayer/fixtures/SoftLayer_Network_Storage_Hub_Cleversafe_Account.py b/SoftLayer/fixtures/SoftLayer_Network_Storage_Hub_Cleversafe_Account.py new file mode 100644 index 000000000..4bc3f4fc7 --- /dev/null +++ b/SoftLayer/fixtures/SoftLayer_Network_Storage_Hub_Cleversafe_Account.py @@ -0,0 +1,39 @@ +credentialCreate = { + "accountId": "12345", + "createDate": "2019-04-05T13:25:25-06:00", + "id": 11111, + "password": "nwUEUsx6PiEoN0B1Xe9z9hUCyXMkAFhDOjHqYJva", + "username": "XfHhBNBPlPdlWyaPPJAI", + "type": { + "description": "A credential for generating S3 Compatible Signatures.", + "keyName": "S3_COMPATIBLE_SIGNATURE", + "name": "S3 Compatible Signature" + } +} + +getCredentials = [ + { + "accountId": "12345", + "createDate": "2019-04-05T13:25:25-06:00", + "id": 11111, + "password": "nwUEUsx6PiEoN0B1Xe9z9hUCyXMkAFhDOjHqYJva", + "username": "XfHhBNBPlPdlWyaPPJAI", + "type": { + "description": "A credential for generating S3 Compatible Signatures.", + "keyName": "S3_COMPATIBLE_SIGNATURE", + "name": "S3 Compatible Signature" + } + }, + { + "accountId": "12345", + "createDate": "2019-04-05T13:25:25-06:00", + "id": 11111, + "password": "nwUEUsx6PiEoN0B1Xe9z9hUCyXMkAFhDOjHqYJva", + "username": "XfHhBNBPlPdlWyaPPJAI", + "type": { + "description": "A credential for generating S3 Compatible Signatures.", + "keyName": "S3_COMPATIBLE_SIGNATURE", + "name": "S3 Compatible Signature" + } + } +] diff --git a/SoftLayer/managers/object_storage.py b/SoftLayer/managers/object_storage.py index b25a457e1..2560d26c8 100644 --- a/SoftLayer/managers/object_storage.py +++ b/SoftLayer/managers/object_storage.py @@ -52,3 +52,47 @@ def list_endpoints(self): }) return endpoints + + def create_credential(self, identifier): + """Create object storage credential. + + :param int identifier: The object storage account identifier. + + """ + + return self.client.call('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'credentialCreate', + id=identifier) + + def delete_credential(self, identifier, credential_id=None): + """Delete the object storage credential. + + :param int id: The object storage account identifier. + :param int credential_id: The credential id to be deleted. + + """ + credential = { + 'id': credential_id + } + + return self.client.call('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'credentialDelete', + credential, id=identifier) + + def limit_credential(self, identifier): + """Limit object storage credentials. + + :param int identifier: The object storage account identifier. + + """ + + return self.client.call('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getCredentialLimit', + id=identifier) + + def list_credential(self, identifier): + """List the object storage credentials. + + :param int identifier: The object storage account identifier. + + """ + + return self.client.call('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getCredentials', + id=identifier) diff --git a/tests/CLI/modules/object_storage_tests.py b/tests/CLI/modules/object_storage_tests.py index 7c8e7ec96..74d70152e 100644 --- a/tests/CLI/modules/object_storage_tests.py +++ b/tests/CLI/modules/object_storage_tests.py @@ -37,3 +37,70 @@ def test_list_endpoints(self): [{'datacenter': 'dal05', 'private': 'https://dal05/auth/v1.0/', 'public': 'https://dal05/auth/v1.0/'}]) + + def test_create_credential(self): + accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'credentialCreate') + accounts.return_value = { + "accountId": "12345", + "createDate": "2019-04-05T13:25:25-06:00", + "id": 11111, + "password": "nwUEUsx6PiEoN0B1Xe9z9hUCy", + "username": "XfHhBNBPlPdl", + "type": { + "description": "A credential for generating S3 Compatible Signatures.", + "keyName": "S3_COMPATIBLE_SIGNATURE", + "name": "S3 Compatible Signature" + } + } + + result = self.run_command(['object-storage', 'credential', 'create', '100']) + + self.assert_no_fail(result) + self.assertEqual(json.loads(result.output), + [{'id': 11111, + 'password': 'nwUEUsx6PiEoN0B1Xe9z9hUCy', + 'type_name': 'S3 Compatible Signature', + 'username': 'XfHhBNBPlPdl'}] + ) + + def test_delete_credential(self): + accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'credentialDelete') + accounts.return_value = True + + result = self.run_command(['object-storage', 'credential', 'delete', '-c', 100, '100']) + + self.assert_no_fail(result) + self.assertEqual(result.output, 'True\n') + + def test_limit_credential(self): + accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getCredentialLimit') + accounts.return_value = 2 + + result = self.run_command(['object-storage', 'credential', 'limit', '100']) + + self.assert_no_fail(result) + self.assertEqual(json.loads(result.output), [{'limit': 2}]) + + def test_list_credential(self): + accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getCredentials') + accounts.return_value = [{'id': 1103123, + 'password': 'nwUEUsx6PiEoN0B1Xe9z9hUCyXM', + 'type': {'name': 'S3 Compatible Signature'}, + 'username': 'XfHhBNBPlPdlWya'}, + {'id': 1103333, + 'password': 'nwUEUsx6PiEoN0B1Xe9z9', + 'type': {'name': 'S3 Compatible Signature'}, + 'username': 'XfHhBNBPlPd'}] + + result = self.run_command(['object-storage', 'credential', 'list', '100']) + + self.assert_no_fail(result) + self.assertEqual(json.loads(result.output), + [{'id': 1103123, + 'password': 'nwUEUsx6PiEoN0B1Xe9z9hUCyXM', + 'type_name': 'S3 Compatible Signature', + 'username': 'XfHhBNBPlPdlWya'}, + {'id': 1103333, + 'password': 'nwUEUsx6PiEoN0B1Xe9z9', + 'type_name': 'S3 Compatible Signature', + 'username': 'XfHhBNBPlPd'}]) diff --git a/tests/managers/object_storage_tests.py b/tests/managers/object_storage_tests.py index 478e5158b..e5042080d 100644 --- a/tests/managers/object_storage_tests.py +++ b/tests/managers/object_storage_tests.py @@ -42,3 +42,80 @@ def test_list_endpoints_no_results(self): endpoints = self.object_storage.list_endpoints() self.assertEqual(endpoints, []) + + def test_create_credential(self): + accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'credentialCreate') + accounts.return_value = { + "id": 1103123, + "password": "nwUEUsx6PiEoN0B1Xe9z9hUCyXMkAF", + "username": "XfHhBNBPlPdlWyaP", + "type": { + "name": "S3 Compatible Signature" + } + } + credential = self.object_storage.create_credential(100) + self.assertEqual(credential, + { + "id": 1103123, + "password": "nwUEUsx6PiEoN0B1Xe9z9hUCyXMkAF", + "username": "XfHhBNBPlPdlWyaP", + "type": { + "name": "S3 Compatible Signature" + } + }) + + def test_delete_credential(self): + accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'credentialDelete') + accounts.return_value = True + + credential = self.object_storage.delete_credential(100) + self.assertEqual(credential, True) + + def test_limit_credential(self): + accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getCredentialLimit') + accounts.return_value = 2 + + credential = self.object_storage.limit_credential(100) + self.assertEqual(credential, 2) + + def test_list_credential(self): + accounts = self.set_mock('SoftLayer_Network_Storage_Hub_Cleversafe_Account', 'getCredentials') + accounts.return_value = [ + { + "id": 1103123, + "password": "nwUEUsx6PiEoN0B1Xe9z9hUCyXsf4sf", + "username": "XfHhBNBPlPdlWyaP3fsd", + "type": { + "name": "S3 Compatible Signature" + } + }, + { + "id": 1102341, + "password": "nwUEUsx6PiEoN0B1Xe9z9hUCyXMkAF", + "username": "XfHhBNBPlPdlWyaP", + "type": { + "name": "S3 Compatible Signature" + } + } + ] + credential = self.object_storage.list_credential(100) + self.assertEqual(credential, + [ + { + "id": 1103123, + "password": "nwUEUsx6PiEoN0B1Xe9z9hUCyXsf4sf", + "username": "XfHhBNBPlPdlWyaP3fsd", + "type": { + "name": "S3 Compatible Signature" + } + }, + { + "id": 1102341, + "password": "nwUEUsx6PiEoN0B1Xe9z9hUCyXMkAF", + "username": "XfHhBNBPlPdlWyaP", + "type": { + "name": "S3 Compatible Signature" + } + } + ] + )