diff --git a/SoftLayer/CLI/account/bandwidth_pools.py b/SoftLayer/CLI/account/bandwidth_pools.py new file mode 100644 index 000000000..358bfd4d0 --- /dev/null +++ b/SoftLayer/CLI/account/bandwidth_pools.py @@ -0,0 +1,41 @@ +"""Displays information about the accounts bandwidth pools""" +# :license: MIT, see LICENSE for more details. +import click + +from SoftLayer.CLI import environment +from SoftLayer.CLI import formatting +from SoftLayer.managers.account import AccountManager as AccountManager +from SoftLayer import utils + + +@click.command() +@environment.pass_env +def cli(env): + """Displays bandwidth pool information + + Similiar to https://cloud.ibm.com/classic/network/bandwidth/vdr + """ + + manager = AccountManager(env.client) + items = manager.get_bandwidth_pools() + + table = formatting.Table([ + "Pool Name", + "Region", + "Servers", + "Allocation", + "Current Usage", + "Projected Usage" + ], title="Bandwidth Pools") + table.align = 'l' + + for item in items: + name = item.get('name') + region = utils.lookup(item, 'locationGroup', 'name') + servers = manager.get_bandwidth_pool_counts(identifier=item.get('id')) + allocation = "{} GB".format(item.get('totalBandwidthAllocated', 0)) + current = "{} GB".format(utils.lookup(item, 'billingCyclePublicBandwidthUsage', 'amountOut')) + projected = "{} GB".format(item.get('projectedPublicBandwidthUsage', 0)) + + table.add_row([name, region, servers, allocation, current, projected]) + env.fout(table) diff --git a/SoftLayer/CLI/report/bandwidth.py b/SoftLayer/CLI/report/bandwidth.py index 4ae2d0f68..50b002304 100644 --- a/SoftLayer/CLI/report/bandwidth.py +++ b/SoftLayer/CLI/report/bandwidth.py @@ -47,23 +47,25 @@ def _get_pooled_bandwidth(env, start, end): label='Calculating for bandwidth pools', file=sys.stderr) as pools: for pool in pools: - if not pool.get('metricTrackingObjectId'): - continue - - yield { - 'id': pool['id'], + pool_detail = { + 'id': pool.get('id'), 'type': 'pool', - 'name': pool['name'], - 'data': env.client.call( + 'name': pool.get('name'), + 'data': [] + } + if pool.get('metricTrackingObjectId'): + bw_data = env.client.call( 'Metric_Tracking_Object', 'getSummaryData', start.strftime('%Y-%m-%d %H:%M:%S %Z'), end.strftime('%Y-%m-%d %H:%M:%S %Z'), types, 300, - id=pool['metricTrackingObjectId'], - ), - } + id=pool.get('metricTrackingObjectId'), + ) + pool_detail['data'] = bw_data + + yield pool_detail def _get_hardware_bandwidth(env, start, end): @@ -172,28 +174,20 @@ def _get_virtual_bandwidth(env, start, end): @click.command(short_help="Bandwidth report for every pool/server") -@click.option( - '--start', - callback=_validate_datetime, - default=(datetime.datetime.now() - datetime.timedelta(days=30) - ).strftime('%Y-%m-%d'), - help="datetime in the format 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS'") -@click.option( - '--end', - callback=_validate_datetime, - default=datetime.datetime.now().strftime('%Y-%m-%d'), - help="datetime in the format 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS'") -@click.option('--sortby', help='Column to sort by', - default='hostname', - show_default=True) -@click.option('--virtual', is_flag=True, - help='Show the all bandwidth summary for each virtual server', - default=False) -@click.option('--server', is_flag=True, - help='show the all bandwidth summary for each hardware server', - default=False) +@click.option('--start', callback=_validate_datetime, + default=(datetime.datetime.now() - datetime.timedelta(days=30)).strftime('%Y-%m-%d'), + help="datetime in the format 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS'") +@click.option('--end', callback=_validate_datetime, default=datetime.datetime.now().strftime('%Y-%m-%d'), + help="datetime in the format 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS'") +@click.option('--sortby', help='Column to sort by', default='hostname', show_default=True) +@click.option('--virtual', is_flag=True, default=False, + help='Show only the bandwidth summary for each virtual server') +@click.option('--server', is_flag=True, default=False, + help='Show only the bandwidth summary for each hardware server') +@click.option('--pool', is_flag=True, default=False, + help='Show only the bandwidth pool summary.') @environment.pass_env -def cli(env, start, end, sortby, virtual, server): +def cli(env, start, end, sortby, virtual, server, pool): """Bandwidth report for every pool/server. This reports on the total data transfered for each virtual sever, hardware @@ -243,6 +237,9 @@ def _input_to_table(item): for item in itertools.chain(_get_pooled_bandwidth(env, start, end), _get_hardware_bandwidth(env, start, end)): _input_to_table(item) + elif pool: + for item in _get_pooled_bandwidth(env, start, end): + _input_to_table(item) else: for item in itertools.chain(_get_pooled_bandwidth(env, start, end), _get_hardware_bandwidth(env, start, end), diff --git a/SoftLayer/CLI/routes.py b/SoftLayer/CLI/routes.py index 2dbb519e6..02d3420d3 100644 --- a/SoftLayer/CLI/routes.py +++ b/SoftLayer/CLI/routes.py @@ -22,6 +22,7 @@ ('account:item-detail', 'SoftLayer.CLI.account.item_detail:cli'), ('account:cancel-item', 'SoftLayer.CLI.account.cancel_item:cli'), ('account:orders', 'SoftLayer.CLI.account.orders:cli'), + ('account:bandwidth-pools', 'SoftLayer.CLI.account.bandwidth_pools:cli'), ('virtual', 'SoftLayer.CLI.virt'), ('virtual:bandwidth', 'SoftLayer.CLI.virt.bandwidth:cli'), diff --git a/SoftLayer/fixtures/SoftLayer_Account.py b/SoftLayer/fixtures/SoftLayer_Account.py index 35216be76..fb5aedb67 100644 --- a/SoftLayer/fixtures/SoftLayer_Account.py +++ b/SoftLayer/fixtures/SoftLayer_Account.py @@ -1207,3 +1207,21 @@ "version": 4 } }] + +getBandwidthAllotments = [{ + 'billingCyclePublicBandwidthUsage': { + 'amountIn': '6.94517', + 'amountOut': '6.8859' + }, + 'id': 309961, + 'locationGroup': { + 'description': 'All Datacenters in Mexico', + 'id': 262, + 'locationGroupTypeId': 1, + 'name': 'MEX', + 'securityLevelId': None + }, + 'name': 'MexRegion', + 'projectedPublicBandwidthUsage': 9.88, + 'totalBandwidthAllocated': 3361 +}] diff --git a/SoftLayer/fixtures/SoftLayer_Network_Bandwidth_Version1_Allotment.py b/SoftLayer/fixtures/SoftLayer_Network_Bandwidth_Version1_Allotment.py new file mode 100644 index 000000000..422e7721d --- /dev/null +++ b/SoftLayer/fixtures/SoftLayer_Network_Bandwidth_Version1_Allotment.py @@ -0,0 +1,6 @@ +getObject = { + 'id': 309961, + 'bareMetalInstanceCount': 0, + 'hardwareCount': 2, + 'virtualGuestCount': 0 +} diff --git a/SoftLayer/managers/account.py b/SoftLayer/managers/account.py index 51c9c889c..4e6a3a26a 100644 --- a/SoftLayer/managers/account.py +++ b/SoftLayer/managers/account.py @@ -326,3 +326,27 @@ def get_active_account_licenses(self): _mask = """billingItem,softwareDescription""" return self.client['SoftLayer_Account'].getActiveAccountLicenses(mask=_mask) + + def get_bandwidth_pools(self, mask=None): + """Gets all the bandwidth pools on an account""" + + if mask is None: + mask = """mask[totalBandwidthAllocated,locationGroup, id, name, projectedPublicBandwidthUsage, + billingCyclePublicBandwidthUsage[amountOut,amountIn]] + """ + + return self.client.call('SoftLayer_Account', 'getBandwidthAllotments', mask=mask, iter=True) + + def get_bandwidth_pool_counts(self, identifier): + """Gets a count of all servers in a bandwidth pool + + Getting the server counts individually is significantly faster than pulling them in + with the get_bandwidth_pools api call. + """ + mask = "mask[id, bareMetalInstanceCount, hardwareCount, virtualGuestCount]" + counts = self.client.call('SoftLayer_Network_Bandwidth_Version1_Allotment', 'getObject', + id=identifier, mask=mask) + total = counts.get('bareMetalInstanceCount', 0) + \ + counts.get('hardwareCount', 0) + \ + counts.get('virtualGuestCount', 0) + return total diff --git a/docs/cli/account.rst b/docs/cli/account.rst index 719c44fde..8cb855f13 100644 --- a/docs/cli/account.rst +++ b/docs/cli/account.rst @@ -43,3 +43,7 @@ Account Commands .. click:: SoftLayer.CLI.account.licenses:cli :prog: account licenses :show-nested: + +.. click:: SoftLayer.CLI.account.bandwidth_pools:cli + :prog: account bandwidth-pools + :show-nested: diff --git a/tests/CLI/modules/account_tests.py b/tests/CLI/modules/account_tests.py index 8428d3306..b33c38c6e 100644 --- a/tests/CLI/modules/account_tests.py +++ b/tests/CLI/modules/account_tests.py @@ -136,3 +136,9 @@ def test_acccount_licenses(self): self.assert_no_fail(result) self.assert_called_with('SoftLayer_Account', 'getActiveVirtualLicenses') self.assert_called_with('SoftLayer_Account', 'getActiveAccountLicenses') + + def test_bandwidth_pools(self): + result = self.run_command(['account', 'bandwidth-pools']) + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Account', 'getBandwidthAllotments') + self.assert_called_with('SoftLayer_Network_Bandwidth_Version1_Allotment', 'getObject') diff --git a/tests/CLI/modules/report_tests.py b/tests/CLI/modules/report_tests.py index 8489aeeab..f756704c0 100644 --- a/tests/CLI/modules/report_tests.py +++ b/tests/CLI/modules/report_tests.py @@ -8,6 +8,8 @@ import json +from pprint import pprint as pp + class ReportTests(testing.TestCase): @@ -76,8 +78,7 @@ def test_bandwidth_report(self): 'hostname': 'host3', 'virtualRack': {'id': 2, 'bandwidthAllotmentTypeId': 2}, }] - summary_data = self.set_mock('SoftLayer_Metric_Tracking_Object', - 'getSummaryData') + summary_data = self.set_mock('SoftLayer_Metric_Tracking_Object', 'getSummaryData') summary_data.return_value = [ {'type': 'publicIn_net_octet', 'counter': 10}, {'type': 'publicOut_net_octet', 'counter': 20}, @@ -93,105 +94,42 @@ def test_bandwidth_report(self): ]) self.assert_no_fail(result) + stripped_output = '[' + result.output.split('[', 1)[1] - self.assertEqual([ - { - 'hostname': 'pool1', - 'pool': None, - 'private_in': 30, - 'private_out': 40, - 'public_in': 10, - 'public_out': 20, - 'type': 'pool' - }, { - 'hostname': 'pool3', - 'pool': None, - 'private_in': 30, - 'private_out': 40, - 'public_in': 10, - 'public_out': 20, - 'type': 'pool' - }, { - 'hostname': 'host1', - 'pool': None, - 'private_in': 30, - 'private_out': 40, - 'public_in': 10, - 'public_out': 20, - 'type': 'hardware' - }, { - 'hostname': 'host3', - 'pool': None, - 'private_in': 30, - 'private_out': 40, - 'public_in': 10, - 'public_out': 20, - 'type': 'hardware' - }, { - 'hostname': 'host1', - 'pool': None, - 'private_in': 30, - 'private_out': 40, - 'public_in': 10, - 'public_out': 20, - 'type': 'virtual' - }, { - 'hostname': 'host3', - 'pool': 2, - 'private_in': 30, - 'private_out': 40, - 'public_in': 10, - 'public_out': 20, - 'type': 'virtual'}], - json.loads(stripped_output), - ) - self.assertEqual( - 6, - len(self.calls('SoftLayer_Metric_Tracking_Object', - 'getSummaryData')), - ) - self.assert_called_with('SoftLayer_Metric_Tracking_Object', - 'getSummaryData', - identifier=1) - self.assert_called_with('SoftLayer_Metric_Tracking_Object', - 'getSummaryData', - identifier=3) - self.assert_called_with('SoftLayer_Metric_Tracking_Object', - 'getSummaryData', - identifier=101) - self.assert_called_with('SoftLayer_Metric_Tracking_Object', - 'getSummaryData', - identifier=103) - self.assert_called_with('SoftLayer_Metric_Tracking_Object', - 'getSummaryData', - identifier=201) - self.assert_called_with('SoftLayer_Metric_Tracking_Object', - 'getSummaryData', - identifier=203) - call = self.calls('SoftLayer_Metric_Tracking_Object', 'getSummaryData', - identifier=1)[0] - expected_args = ( - '2016-02-04 00:00:00 ', - '2016-03-04 12:34:56 ', - [{ - 'keyName': 'PUBLICIN', - 'name': 'publicIn', - 'summaryType': 'sum', - }, { - 'keyName': 'PUBLICOUT', - 'name': 'publicOut', - 'summaryType': 'sum', - }, { - 'keyName': 'PRIVATEIN', - 'name': 'privateIn', - 'summaryType': 'sum', - }, { - 'keyName': 'PRIVATEOUT', - 'name': 'privateOut', - 'summaryType': 'sum', - }], - 300, - ) + json_output = json.loads(stripped_output) + pp(json.loads(stripped_output)) + print("======= ^^^^^^^^^ ==============") + self.assertEqual(json_output[0]['hostname'], 'pool1') + self.assertEqual(json_output[0]['private_in'], 30) + + self.assertEqual(6, len(self.calls('SoftLayer_Metric_Tracking_Object', 'getSummaryData'))) + self.assert_called_with('SoftLayer_Metric_Tracking_Object', 'getSummaryData', identifier=1) + self.assert_called_with('SoftLayer_Metric_Tracking_Object', 'getSummaryData', identifier=3) + self.assert_called_with('SoftLayer_Metric_Tracking_Object', 'getSummaryData', identifier=101) + self.assert_called_with('SoftLayer_Metric_Tracking_Object', 'getSummaryData', identifier=103) + self.assert_called_with('SoftLayer_Metric_Tracking_Object', 'getSummaryData', identifier=201) + self.assert_called_with('SoftLayer_Metric_Tracking_Object', 'getSummaryData', identifier=203) + call = self.calls('SoftLayer_Metric_Tracking_Object', 'getSummaryData', identifier=1)[0] + expected_args = ('2016-02-04 00:00:00 ', '2016-03-04 12:34:56 ', + [{ + 'keyName': 'PUBLICIN', + 'name': 'publicIn', + 'summaryType': 'sum', + }, { + 'keyName': 'PUBLICOUT', + 'name': 'publicOut', + 'summaryType': 'sum', + }, { + 'keyName': 'PRIVATEIN', + 'name': 'privateIn', + 'summaryType': 'sum', + }, { + 'keyName': 'PRIVATEOUT', + 'name': 'privateOut', + 'summaryType': 'sum', + }], + 300, + ) self.assertEqual(expected_args, call.args) def test_virtual_bandwidth_report(self): @@ -242,83 +180,38 @@ def test_virtual_bandwidth_report(self): self.assert_no_fail(result) stripped_output = '[' + result.output.split('[', 1)[1] - self.assertEqual([ - { - 'hostname': 'pool1', - 'pool': None, - 'private_in': 30, - 'private_out': 40, - 'public_in': 10, - 'public_out': 20, - 'type': 'pool', - }, { - 'hostname': 'pool3', - 'pool': None, - 'private_in': 30, - 'private_out': 40, - 'public_in': 10, - 'public_out': 20, - 'type': 'pool', - }, { - 'hostname': 'host1', - 'pool': None, - 'private_in': 30, - 'private_out': 40, - 'public_in': 10, - 'public_out': 20, - 'type': 'virtual', - }, { - 'hostname': 'host3', - 'pool': 2, - 'private_in': 30, - 'private_out': 40, - 'public_in': 10, - 'public_out': 20, - 'type': 'virtual', - }], - json.loads(stripped_output), - ) - self.assertEqual( - 4, - len(self.calls('SoftLayer_Metric_Tracking_Object', - 'getSummaryData')), - ) - self.assert_called_with('SoftLayer_Metric_Tracking_Object', - 'getSummaryData', - identifier=1) - self.assert_called_with('SoftLayer_Metric_Tracking_Object', - 'getSummaryData', - identifier=3) - self.assert_called_with('SoftLayer_Metric_Tracking_Object', - 'getSummaryData', - identifier=201) - self.assert_called_with('SoftLayer_Metric_Tracking_Object', - 'getSummaryData', - identifier=203) - call = self.calls('SoftLayer_Metric_Tracking_Object', 'getSummaryData', - identifier=1)[0] - expected_args = ( - '2016-02-04 00:00:00 ', - '2016-03-04 12:34:56 ', - [{ - 'keyName': 'PUBLICIN', - 'name': 'publicIn', - 'summaryType': 'sum', - }, { - 'keyName': 'PUBLICOUT', - 'name': 'publicOut', - 'summaryType': 'sum', - }, { - 'keyName': 'PRIVATEIN', - 'name': 'privateIn', - 'summaryType': 'sum', - }, { - 'keyName': 'PRIVATEOUT', - 'name': 'privateOut', - 'summaryType': 'sum', - }], - 300, - ) + json_output = json.loads(stripped_output) + self.assertEqual(json_output[0]['hostname'], 'pool1') + self.assertEqual(json_output[1]['private_in'], 0) + self.assertEqual(json_output[2]['private_in'], 30) + self.assertEqual(json_output[3]['type'], 'virtual') + + self.assertEqual(4, len(self.calls('SoftLayer_Metric_Tracking_Object', 'getSummaryData'))) + self.assert_called_with('SoftLayer_Metric_Tracking_Object', 'getSummaryData', identifier=1) + self.assert_called_with('SoftLayer_Metric_Tracking_Object', 'getSummaryData', identifier=3) + self.assert_called_with('SoftLayer_Metric_Tracking_Object', 'getSummaryData', identifier=201) + self.assert_called_with('SoftLayer_Metric_Tracking_Object', 'getSummaryData', identifier=203) + call = self.calls('SoftLayer_Metric_Tracking_Object', 'getSummaryData', identifier=1)[0] + expected_args = ('2016-02-04 00:00:00 ', '2016-03-04 12:34:56 ', + [{ + 'keyName': 'PUBLICIN', + 'name': 'publicIn', + 'summaryType': 'sum', + }, { + 'keyName': 'PUBLICOUT', + 'name': 'publicOut', + 'summaryType': 'sum', + }, { + 'keyName': 'PRIVATEIN', + 'name': 'privateIn', + 'summaryType': 'sum', + }, { + 'keyName': 'PRIVATEOUT', + 'name': 'privateOut', + 'summaryType': 'sum', + }], + 300, + ) self.assertEqual(expected_args, call.args) def test_server_bandwidth_report(self): @@ -370,76 +263,35 @@ def test_server_bandwidth_report(self): self.assert_no_fail(result) stripped_output = '[' + result.output.split('[', 1)[1] - self.assertEqual([ - { - 'hostname': 'pool1', - 'pool': None, - 'private_in': 30, - 'private_out': 40, - 'public_in': 10, - 'public_out': 20, - 'type': 'pool', - }, { - 'hostname': 'pool3', - 'pool': None, - 'private_in': 30, - 'private_out': 40, - 'public_in': 10, - 'public_out': 20, - 'type': 'pool', - }, { - 'hostname': 'host1', - 'pool': None, - 'private_in': 30, - 'private_out': 40, - 'public_in': 10, - 'public_out': 20, - 'type': 'hardware', - }, { - 'hostname': 'host3', - 'pool': None, - 'private_in': 30, - 'private_out': 40, - 'public_in': 10, - 'public_out': 20, - 'type': 'hardware', - }, ], - json.loads(stripped_output), - ) - self.assertEqual( - 4, - len(self.calls('SoftLayer_Metric_Tracking_Object', - 'getSummaryData')), - ) - self.assert_called_with('SoftLayer_Metric_Tracking_Object', - 'getSummaryData', - identifier=101) - self.assert_called_with('SoftLayer_Metric_Tracking_Object', - 'getSummaryData', - identifier=103) + json_output = json.loads(stripped_output) + self.assertEqual(json_output[0]['hostname'], 'pool1') + self.assertEqual(json_output[1]['private_in'], 0) + self.assertEqual(json_output[2]['private_in'], 30) + self.assertEqual(json_output[3]['type'], 'hardware') - call = self.calls('SoftLayer_Metric_Tracking_Object', 'getSummaryData', - identifier=1)[0] - expected_args = ( - '2016-02-04 00:00:00 ', - '2016-03-04 12:34:56 ', - [{ - 'keyName': 'PUBLICIN', - 'name': 'publicIn', - 'summaryType': 'sum', - }, { - 'keyName': 'PUBLICOUT', - 'name': 'publicOut', - 'summaryType': 'sum', - }, { - 'keyName': 'PRIVATEIN', - 'name': 'privateIn', - 'summaryType': 'sum', - }, { - 'keyName': 'PRIVATEOUT', - 'name': 'privateOut', - 'summaryType': 'sum', - }], - 300, - ) + self.assertEqual(4, len(self.calls('SoftLayer_Metric_Tracking_Object', 'getSummaryData'))) + self.assert_called_with('SoftLayer_Metric_Tracking_Object', 'getSummaryData', identifier=101) + self.assert_called_with('SoftLayer_Metric_Tracking_Object', 'getSummaryData', identifier=103) + + call = self.calls('SoftLayer_Metric_Tracking_Object', 'getSummaryData', identifier=1)[0] + expected_args = ('2016-02-04 00:00:00 ', '2016-03-04 12:34:56 ', + [{ + 'keyName': 'PUBLICIN', + 'name': 'publicIn', + 'summaryType': 'sum', + }, { + 'keyName': 'PUBLICOUT', + 'name': 'publicOut', + 'summaryType': 'sum', + }, { + 'keyName': 'PRIVATEIN', + 'name': 'privateIn', + 'summaryType': 'sum', + }, { + 'keyName': 'PRIVATEOUT', + 'name': 'privateOut', + 'summaryType': 'sum', + }], + 300, + ) self.assertEqual(expected_args, call.args) diff --git a/tests/managers/account_tests.py b/tests/managers/account_tests.py index 6d515de38..b32c223f6 100644 --- a/tests/managers/account_tests.py +++ b/tests/managers/account_tests.py @@ -3,6 +3,7 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """ +from unittest import mock as mock from SoftLayer.managers.account import AccountManager as AccountManager from SoftLayer import SoftLayerAPIError @@ -166,3 +167,12 @@ def test_get_routers_with_datacenter(self): self.manager.get_routers(location='dal13') object_filter = {'routers': {'topLevelLocation': {'name': {'operation': 'dal13'}}}} self.assert_called_with("SoftLayer_Account", "getRouters", filter=object_filter) + + def test_get_bandwidth_pools(self): + self.manager.get_bandwidth_pools() + self.assert_called_with('SoftLayer_Account', 'getBandwidthAllotments', mask=mock.ANY) + + def test_get_bandwidth_pool_counts(self): + total = self.manager.get_bandwidth_pool_counts(1234) + self.assert_called_with('SoftLayer_Network_Bandwidth_Version1_Allotment', 'getObject', identifier=1234) + self.assertEqual(total, 2)