Skip to content

Commit 23d8188

Browse files
author
Fernando Ojeda
committed
Feature usage vs information.
1 parent 606ec64 commit 23d8188

File tree

7 files changed

+144
-0
lines changed

7 files changed

+144
-0
lines changed

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
('virtual:reboot', 'SoftLayer.CLI.virt.power:reboot'),
3737
('virtual:reload', 'SoftLayer.CLI.virt.reload:cli'),
3838
('virtual:upgrade', 'SoftLayer.CLI.virt.upgrade:cli'),
39+
('virtual:usage', 'SoftLayer.CLI.virt.usage:cli'),
3940
('virtual:credentials', 'SoftLayer.CLI.virt.credentials:cli'),
4041
('virtual:capacity', 'SoftLayer.CLI.virt.capacity:cli'),
4142
('virtual:placementgroup', 'SoftLayer.CLI.virt.placementgroup:cli'),

SoftLayer/CLI/virt/usage.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""Usage information of a virtual server."""
2+
# :license: MIT, see LICENSE for more details.
3+
4+
import click
5+
6+
import SoftLayer
7+
from SoftLayer.CLI import environment
8+
from SoftLayer.CLI import formatting
9+
from SoftLayer.CLI import helpers
10+
from SoftLayer.utils import clean_time
11+
12+
13+
@click.command()
14+
@click.argument('identifier')
15+
@click.option('--start_date', '-s', type=click.STRING, required=True, help="Start Date e.g. 2019-3-4 (yyyy-MM-dd)")
16+
@click.option('--end_date', '-e', type=click.STRING, required=True, help="End Date e.g. 2019-4-2 (yyyy-MM-dd)")
17+
@click.option('--valid_type', '-t', type=click.STRING, required=True,
18+
help="Metric_Data_Type keyName e.g. CPU0, CPU1, MEMORY_USAGE, etc.")
19+
@click.option('--summary_period', '-p', type=click.INT, default=1800,
20+
help="300, 600, 1800, 3600, 43200 or 86400 seconds")
21+
@environment.pass_env
22+
def cli(env, identifier, start_date, end_date, valid_type, summary_period):
23+
"""Usage information of a virtual server."""
24+
25+
vsi = SoftLayer.VSManager(env.client)
26+
table = formatting.Table(['counter', 'dateTime', 'type'])
27+
table_average = formatting.Table(['Average'])
28+
29+
vs_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS')
30+
31+
result = vsi.get_summary_data_usage(vs_id, start_date=start_date, end_date=end_date,
32+
valid_type=valid_type, summary_period=summary_period)
33+
34+
count = 0
35+
counter = 0.0
36+
for data in result:
37+
table.add_row([
38+
data['counter'],
39+
clean_time(data['dateTime']),
40+
data['type'],
41+
])
42+
counter = counter + float(data['counter'])
43+
count = count + 1
44+
45+
if type == "MEMORY_USAGE":
46+
average = (counter / count) / 2 ** 30
47+
else:
48+
average = counter / count
49+
50+
env.fout(table_average.add_row([average]))
51+
52+
env.fout(table_average)
53+
env.fout(table)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
getSummaryData = [
2+
{
3+
"counter": 1.44,
4+
"dateTime": "2019-03-04T00:00:00-06:00",
5+
"type": "cpu0"
6+
},
7+
{
8+
"counter": 1.53,
9+
"dateTime": "2019-03-04T00:05:00-06:00",
10+
"type": "cpu0"
11+
},
12+
]

SoftLayer/fixtures/SoftLayer_Virtual_Guest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,3 +626,5 @@
626626
}
627627
},
628628
]
629+
630+
getMetricTrackingObjectId = 1000

SoftLayer/managers/vs.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,27 @@ def _get_price_id_for_upgrade_option(self, upgrade_prices, option, value, public
10021002
else:
10031003
return price.get('id')
10041004

1005+
def get_summary_data_usage(self, instance_id, start_date=None, end_date=None, valid_type=None, summary_period=None):
1006+
"""Retrieve the usage information of a virtual server.
1007+
1008+
:param string instance_id: a string identifier used to resolve ids
1009+
:param string start_date: the start data to retrieve the vs usage information
1010+
:param string end_date: the start data to retrieve the vs usage information
1011+
:param string string valid_type: the Metric_Data_Type keyName.
1012+
:param int summary_period: summary period.
1013+
"""
1014+
valid_types = [
1015+
{
1016+
"keyName": valid_type,
1017+
"summaryType": "max"
1018+
}
1019+
]
1020+
1021+
metric_tracking_id = self.guest.getMetricTrackingObjectId(id=instance_id)
1022+
1023+
return self.client.call('Metric_Tracking_Object', 'getSummaryData', start_date, end_date, valid_types,
1024+
summary_period, id=metric_tracking_id, iter=True)
1025+
10051026
# pylint: disable=inconsistent-return-statements
10061027
def _get_price_id_for_upgrade(self, package_items, option, value, public=True):
10071028
"""Find the price id for the option and value to upgrade.

tests/CLI/modules/vs/vs_tests.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,3 +660,30 @@ def test_vs_capture(self):
660660
result = self.run_command(['vs', 'capture', '100', '--name', 'TestName'])
661661
self.assert_no_fail(result)
662662
self.assert_called_with('SoftLayer_Virtual_Guest', 'createArchiveTransaction', identifier=100)
663+
664+
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
665+
def test_usage_no_confirm(self, confirm_mock):
666+
confirm_mock.return_value = False
667+
668+
result = self.run_command(['vs', 'usage', '100'])
669+
self.assertEqual(result.exit_code, 2)
670+
671+
def test_usage_vs(self):
672+
result = self.run_command(
673+
['vs', 'usage', '100'])
674+
self.assertEqual(result.exit_code, 2)
675+
676+
def test_usage_vs_cpu(self):
677+
result = self.run_command(
678+
['vs', 'usage', '100', '--start_date=2019-3-4', '--end_date=2019-4-2', '--valid_type=CPU0',
679+
'--summary_period=300'])
680+
681+
self.assert_no_fail(result)
682+
683+
def test_usage_vs_memory(self):
684+
685+
result = self.run_command(
686+
['vs', 'usage', '100', '--start_date=2019-3-4', '--end_date=2019-4-2', '--valid_type=MEMORY_USAGE',
687+
'--summary_period=300'])
688+
689+
self.assert_no_fail(result)

tests/managers/vs/vs_tests.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,3 +830,31 @@ def test_capture_additional_disks(self):
830830
'createArchiveTransaction',
831831
args=args,
832832
identifier=1)
833+
834+
def test_usage_vs_cpu(self):
835+
result = self.vs.get_summary_data_usage('100',
836+
start_date='2019-3-4',
837+
end_date='2019-4-2',
838+
valid_type='CPU0',
839+
summary_period=300)
840+
841+
expected = fixtures.SoftLayer_Metric_Tracking_Object.getSummaryData
842+
self.assertEqual(result, expected)
843+
844+
args = ('2019-3-4', '2019-4-2', [{"keyName": "CPU0", "summaryType": "max"}], 300)
845+
846+
self.assert_called_with('SoftLayer_Metric_Tracking_Object', 'getSummaryData', args=args, identifier=1000)
847+
848+
def test_usage_vs_memory(self):
849+
result = self.vs.get_summary_data_usage('100',
850+
start_date='2019-3-4',
851+
end_date='2019-4-2',
852+
valid_type='MEMORY_USAGE',
853+
summary_period=300)
854+
855+
expected = fixtures.SoftLayer_Metric_Tracking_Object.getSummaryData
856+
self.assertEqual(result, expected)
857+
858+
args = ('2019-3-4', '2019-4-2', [{"keyName": "MEMORY_USAGE", "summaryType": "max"}], 300)
859+
860+
self.assert_called_with('SoftLayer_Metric_Tracking_Object', 'getSummaryData', args=args, identifier=1000)

0 commit comments

Comments
 (0)