Skip to content
Closed
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)
22 changes: 22 additions & 0 deletions SoftLayer/CLI/object_storage/credential/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""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', '-id', 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)

if credential:
env.fout("The credential was deleted successful")
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)
limit = mgr.limit_credential(identifier)
table = formatting.Table(['limit'])
table.add_row([
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)
list = mgr.list_credential(identifier)
table = formatting.Table(['id', 'password', 'username', 'type_name'])

for credential in list:
table.add_row([
credential['id'],
credential['password'],
credential['username'],
credential['type']['name']
])

env.fout(table)
3 changes: 3 additions & 0 deletions SoftLayer/CLI/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
('virtual:reboot', 'SoftLayer.CLI.virt.power:reboot'),
('virtual:reload', 'SoftLayer.CLI.virt.reload:cli'),
('virtual:upgrade', 'SoftLayer.CLI.virt.upgrade:cli'),
('virtual:usage', 'SoftLayer.CLI.virt.usage:cli'),
('virtual:credentials', 'SoftLayer.CLI.virt.credentials:cli'),
('virtual:capacity', 'SoftLayer.CLI.virt.capacity:cli'),
('virtual:placementgroup', 'SoftLayer.CLI.virt.placementgroup:cli'),
Expand Down Expand Up @@ -199,6 +200,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
44 changes: 44 additions & 0 deletions SoftLayer/CLI/virt/usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Usage information of a virtual server."""
# :license: MIT, see LICENSE for more details.

import click

import SoftLayer
from SoftLayer.CLI import environment, helpers
from SoftLayer.CLI import exceptions
from SoftLayer.CLI import formatting


@click.command(short_help="Usage information of a virtual server.")
@click.argument('identifier')
@click.option('--start_date', '-s', type=click.STRING, required=True, help="Start Date e.g. 2019-3-4")
@click.option('--end_date', '-e', type=click.STRING, required=True, help="End Date e.g. 2019-4-2")
@click.option('--valid_type', '-t', type=click.STRING, required=True,
help="Metric_Data_Type keyName e.g. CPU0, CPU1, MEMORY_USAGE, etc.")
@click.option('--summary_period', '-summary', type=click.INT, required=True,
help="300, 600, 1800, 3600, 43200 or 86400 seconds")
@environment.pass_env
def cli(env, identifier, start_date, end_date, valid_type, summary_period):
"""Usage information of a virtual server."""

vsi = SoftLayer.VSManager(env.client)
table = formatting.Table(['counter', 'dateTime', 'type'])

if not any([start_date, end_date, valid_type, summary_period]):
raise exceptions.ArgumentError(
"Must provide [--start_date], [--end_date], [--valid_type], or [--summary_period] to retrieve the usage "
"information")

vs_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS')

result = vsi.get_summary_data_usage(instance_id=vs_id, start_date=start_date, end_date=end_date,
valid_type=valid_type, summary_period=summary_period)

for data in result:
table.add_row([
data['counter'],
data['dateTime'],
data['type'],
])

env.fout(table)
12 changes: 12 additions & 0 deletions SoftLayer/fixtures/SoftLayer_Metric_Tracking_Object.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
getSummaryData = [
{
"counter": 1.44,
"dateTime": "2019-03-04T00:00:00-06:00",
"type": "cpu0"
},
{
"counter": 1.53,
"dateTime": "2019-03-04T00:05:00-06:00",
"type": "cpu0"
},
]
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"
}
}
]
36 changes: 36 additions & 0 deletions SoftLayer/managers/object_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,39 @@ 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)
20 changes: 20 additions & 0 deletions SoftLayer/managers/vs.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(self, client, ordering_manager=None):
self.account = client['Account']
self.guest = client['Virtual_Guest']
self.package_svc = client['Product_Package']
self.metric_tracking_object = client['Metric_Tracking_Object']
self.resolvers = [self._get_ids_from_ip, self._get_ids_from_hostname]
if ordering_manager is None:
self.ordering_manager = ordering.OrderingManager(client)
Expand Down Expand Up @@ -1002,6 +1003,25 @@ def _get_price_id_for_upgrade_option(self, upgrade_prices, option, value, public
else:
return price.get('id')

def get_summary_data_usage(self, instance_id, start_date=None, end_date=None, valid_type=None, summary_period=None):
"""Retrieve the usage information of a virtual server.

:param string instance_id: a string identifier used to resolve ids
:param string start_date: the start data to retrieve the vs usage information
:param string end_date: the start data to retrieve the vs usage information
:param string string valid_type: the Metric_Data_Type keyName.
:param int summary_period: summary period.
"""
valid_types = [
{
"keyName": valid_type,
"summaryType": "max"
}
]

return self.metric_tracking_object.getSummaryData(start_date, end_date, valid_types, summary_period,
id=instance_id)

# pylint: disable=inconsistent-return-statements
def _get_price_id_for_upgrade(self, package_items, option, value, public=True):
"""Find the price id for the option and value to upgrade.
Expand Down
Loading