Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions SoftLayer/CLI/object_storage/credential/__init__.py
Original file line number Diff line number Diff line change
@@ -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"""
28 changes: 28 additions & 0 deletions SoftLayer/CLI/object_storage/credential/create.py
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 21 additions & 0 deletions SoftLayer/CLI/object_storage/credential/delete.py
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 24 additions & 0 deletions SoftLayer/CLI/object_storage/credential/limit.py
Original file line number Diff line number Diff line change
@@ -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)
29 changes: 29 additions & 0 deletions SoftLayer/CLI/object_storage/credential/list.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions SoftLayer/CLI/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
44 changes: 44 additions & 0 deletions SoftLayer/managers/object_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
67 changes: 67 additions & 0 deletions tests/CLI/modules/object_storage_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}])
77 changes: 77 additions & 0 deletions tests/managers/object_storage_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
)