diff --git a/SoftLayer/CLI/account/bandwidth_pools_detail.py b/SoftLayer/CLI/account/bandwidth_pools_detail.py new file mode 100644 index 000000000..7e609e961 --- /dev/null +++ b/SoftLayer/CLI/account/bandwidth_pools_detail.py @@ -0,0 +1,79 @@ +"""Get bandwidth pools.""" +# :license: MIT, see LICENSE for more details. +import click + +from SoftLayer import AccountManager +from SoftLayer.CLI import environment +from SoftLayer.CLI import formatting +from SoftLayer import utils + + +@click.command() +@click.argument('identifier') +@environment.pass_env +def cli(env, identifier): + """Get bandwidth pool details.""" + + manager = AccountManager(env.client) + bandwidths = manager.getBandwidthDetail(identifier) + + table = formatting.KeyValueTable(['name', 'value']) + table.align['name'] = 'r' + table.align['value'] = 'l' + table.add_row(['Id', bandwidths['id']]) + table.add_row(['Name', bandwidths['name']]) + table.add_row(['Create Date', utils.clean_time(bandwidths.get('createDate'), '%Y-%m-%d')]) + current = "{} GB".format(utils.lookup(bandwidths, 'billingCyclePublicBandwidthUsage', 'amountOut')) + if current is None: + current = '-' + table.add_row(['Current Usage', current]) + projected = "{} GB".format(bandwidths.get('projectedPublicBandwidthUsage', 0)) + if projected is None: + projected = '-' + table.add_row(['Projected Usage', projected]) + inbound = "{} GB".format(bandwidths.get('inboundPublicBandwidthUsage', 0)) + if inbound is None: + inbound = '-' + table.add_row(['Inbound Usage', inbound]) + if bandwidths['hardware'] != []: + table.add_row(['hardware', _bw_table(bandwidths['hardware'])]) + else: + table.add_row(['hardware', 'Not Found']) + + if bandwidths['virtualGuests'] != []: + table.add_row(['virtualGuests', _virtual_table(bandwidths['virtualGuests'])]) + else: + table.add_row(['virtualGuests', 'Not Found']) + + if bandwidths['bareMetalInstances'] != []: + table.add_row(['Netscaler', _bw_table(bandwidths['bareMetalInstances'])]) + else: + table.add_row(['Netscaler', 'Not Found']) + + env.fout(table) + + +def _bw_table(bw_data): + """Generates a bandwidth useage table""" + table_data = formatting.Table(['Id', 'HostName', "IP Address", 'Amount', "Current Usage"]) + for bw_point in bw_data: + amount = "{} GB".format(utils.lookup(bw_point, 'bandwidthAllotmentDetail', 'allocation', 'amount')) + current = "{} GB".format(bw_point.get('outboundBandwidthUsage', 0)) + ip_address = bw_point.get('primaryIpAddress') + if ip_address is None: + ip_address = '-' + table_data.add_row([bw_point['id'], bw_point['fullyQualifiedDomainName'], ip_address, amount, current]) + return [table_data] + + +def _virtual_table(bw_data): + """Generates a virtual bandwidth usage table""" + table_data = formatting.Table(['Id', 'HostName', "IP Address", 'Amount', "Current Usage"]) + for bw_point in bw_data: + amount = "{} GB".format(utils.lookup(bw_point, 'bandwidthAllotmentDetail', 'allocation', 'amount')) + current = "{} GB".format(bw_point.get('outboundBandwidthUsage', 0)) + ip_address = bw_point.get('primaryIpAddress') + if ip_address is None: + ip_address = '-' + table_data.add_row([bw_point['id'], bw_point['fullyQualifiedDomainName'], ip_address, amount, current]) + return [table_data] diff --git a/SoftLayer/CLI/routes.py b/SoftLayer/CLI/routes.py index 02d3420d3..3bd02eae9 100644 --- a/SoftLayer/CLI/routes.py +++ b/SoftLayer/CLI/routes.py @@ -15,6 +15,7 @@ ('account:invoice-detail', 'SoftLayer.CLI.account.invoice_detail:cli'), ('account:invoices', 'SoftLayer.CLI.account.invoices:cli'), ('account:events', 'SoftLayer.CLI.account.events:cli'), + ('account:bandwidth-pools-detail', 'SoftLayer.CLI.account.bandwidth_pools_detail:cli'), ('account:event-detail', 'SoftLayer.CLI.account.event_detail:cli'), ('account:licenses', 'SoftLayer.CLI.account.licenses:cli'), ('account:summary', 'SoftLayer.CLI.account.summary:cli'), diff --git a/SoftLayer/fixtures/SoftLayer_Network_Bandwidth_Version1_Allotment.py b/SoftLayer/fixtures/SoftLayer_Network_Bandwidth_Version1_Allotment.py index 422e7721d..eeb72aa9d 100644 --- a/SoftLayer/fixtures/SoftLayer_Network_Bandwidth_Version1_Allotment.py +++ b/SoftLayer/fixtures/SoftLayer_Network_Bandwidth_Version1_Allotment.py @@ -1,6 +1,148 @@ getObject = { - 'id': 309961, + 'bandwidthAllotmentTypeId': 2, + 'createDate': '2016-07-25T08:31:17-07:00', + 'id': 123456, + 'locationGroupId': 262, + 'name': 'MexRegion', + 'serviceProviderId': 1, 'bareMetalInstanceCount': 0, 'hardwareCount': 2, - 'virtualGuestCount': 0 + 'virtualGuestCount': 0, + 'activeDetails': [ + { + 'allocationId': 48293300, + 'bandwidthAllotmentId': 309961, + 'effectiveDate': '2022-02-04T00:00:00-06:00', + 'id': 48882086, + 'serviceProviderId': 1, + 'allocation': { + 'amount': '5000', + 'id': 48293300, + } + }, + { + 'allocationId': 48293302, + 'bandwidthAllotmentId': 309961, + 'effectiveDate': '2022-02-04T00:00:00-06:00', + 'id': 48882088, + 'serviceProviderId': 1, + 'allocation': { + 'amount': '5000', + 'id': 48293302, + } + } + ], + 'bareMetalInstances': [], + 'billingCyclePublicBandwidthUsage': { + 'amountIn': '.23642', + 'amountOut': '.05475', + 'bandwidthUsageDetailTypeId': '1', + 'trackingObject': { + 'id': 258963, + 'resourceTableId': 309961, + 'startDate': '2021-03-10T11:04:56-06:00', + } + }, + 'hardware': [ + { + 'domain': 'test.com', + 'fullyQualifiedDomainName': 'testpooling.test.com', + 'hardwareStatusId': 5, + 'hostname': 'testpooling', + 'id': 36589, + 'manufacturerSerialNumber': 'J122Y7N', + 'provisionDate': '2022-01-24T15:17:03-06:00', + 'serialNumber': 'SL018EA8', + 'serviceProviderId': 1, + 'bandwidthAllotmentDetail': { + 'allocationId': 48293302, + 'bandwidthAllotmentId': 309961, + 'effectiveDate': '2022-02-04T00:00:00-06:00', + 'id': 48882088, + 'allocation': { + 'amount': '5000', + 'id': 48293302, + } + }, + 'globalIdentifier': '36e63026-5fa1-456d-a04f-adf34e60e2f4', + 'hardwareStatus': { + 'id': 5, + 'status': 'ACTIVE' + }, + 'networkManagementIpAddress': '10.130.97.247', + 'outboundBandwidthUsage': '.02594', + 'primaryBackendIpAddress': '10.130.97.227', + 'primaryIpAddress': '169.57.4.70', + 'privateIpAddress': '10.130.97.227' + }, + { + 'domain': 'testtest.com', + 'fullyQualifiedDomainName': 'testpooling2.test.com', + 'hardwareStatusId': 5, + 'hostname': 'testpooling2', + 'id': 25478, + 'manufacturerSerialNumber': 'J12935M', + 'notes': '', + 'provisionDate': '2022-01-24T15:44:20-06:00', + 'serialNumber': 'SL01HIIB', + 'serviceProviderId': 1, + 'bandwidthAllotmentDetail': { + 'allocationId': 48293300, + 'bandwidthAllotmentId': 309961, + 'effectiveDate': '2022-02-04T00:00:00-06:00', + 'id': 48882086, + 'serviceProviderId': 1, + 'allocation': { + 'amount': '5000', + 'id': 478965, + } + }, + 'globalIdentifier': '6ea407bd-9c07-4129-9103-9fda8a9e7028', + 'hardwareStatus': { + 'id': 5, + 'status': 'ACTIVE' + }, + 'networkManagementIpAddress': '10.130.97.252', + 'outboundBandwidthUsage': '.02884', + 'primaryBackendIpAddress': '10.130.97.248', + 'primaryIpAddress': '169.57.4.73', + 'privateIpAddress': '10.130.97.248' + } + ], + 'inboundPublicBandwidthUsage': '.23642', + 'projectedPublicBandwidthUsage': 0.43, + 'virtualGuests': [{ + 'createDate': '2021-06-09T13:49:28-07:00', + 'deviceStatusId': 8, + 'domain': 'cgallo.com', + 'fullyQualifiedDomainName': 'KVM-Test.test.com', + 'hostname': 'KVM-Test', + 'id': 3578963, + 'maxCpu': 2, + 'maxCpuUnits': 'CORE', + 'maxMemory': 4096, + 'startCpus': 2, + 'statusId': 1001, + 'typeId': 1, + 'uuid': '15951561-6171-0dfc-f3d2-be039e51cc10', + 'bandwidthAllotmentDetail': { + 'allocationId': 45907006, + 'bandwidthAllotmentId': 138442, + 'effectiveDate': '2021-06-09T13:49:31-07:00', + 'id': 46467342, + 'serviceProviderId': 1, + 'allocation': { + 'amount': '0', + 'id': 45907006, + } + }, + 'globalIdentifier': 'a245a7dd-acd1-4d1a-9356-cc1ac6b55b98', + 'outboundPublicBandwidthUsage': '.02845', + 'primaryBackendIpAddress': '10.208.73.53', + 'primaryIpAddress': '169.48.96.27', + 'status': { + 'keyName': 'ACTIVE', + 'name': 'Active' + } + }] } diff --git a/SoftLayer/managers/account.py b/SoftLayer/managers/account.py index 4e6a3a26a..aadb4af94 100644 --- a/SoftLayer/managers/account.py +++ b/SoftLayer/managers/account.py @@ -350,3 +350,14 @@ def get_bandwidth_pool_counts(self, identifier): counts.get('hardwareCount', 0) + \ counts.get('virtualGuestCount', 0) return total + + def getBandwidthDetail(self, identifier): + """Gets bandwidth pool detail. + + :returns: bandwidth pool detail + """ + _mask = """activeDetails[allocation],projectedPublicBandwidthUsage, billingCyclePublicBandwidthUsage, + hardware[outboundBandwidthUsage,bandwidthAllotmentDetail[allocation]],inboundPublicBandwidthUsage, + virtualGuests[outboundPublicBandwidthUsage,bandwidthAllotmentDetail[allocation]], + bareMetalInstances[outboundBandwidthUsage,bandwidthAllotmentDetail[allocation]]""" + return self.client['SoftLayer_Network_Bandwidth_Version1_Allotment'].getObject(id=identifier, mask=_mask) diff --git a/docs/cli/account.rst b/docs/cli/account.rst index 8cb855f13..c215d708f 100644 --- a/docs/cli/account.rst +++ b/docs/cli/account.rst @@ -47,3 +47,7 @@ Account Commands .. click:: SoftLayer.CLI.account.bandwidth_pools:cli :prog: account bandwidth-pools :show-nested: + +.. click:: SoftLayer.CLI.account.bandwidth_pools_detail:cli + :prog: account bandwidth-pools-detail + :show-nested: diff --git a/tests/CLI/modules/account_tests.py b/tests/CLI/modules/account_tests.py index b33c38c6e..8b4fe973d 100644 --- a/tests/CLI/modules/account_tests.py +++ b/tests/CLI/modules/account_tests.py @@ -142,3 +142,8 @@ def test_bandwidth_pools(self): self.assert_no_fail(result) self.assert_called_with('SoftLayer_Account', 'getBandwidthAllotments') self.assert_called_with('SoftLayer_Network_Bandwidth_Version1_Allotment', 'getObject') + + def test_acccount_bandwidth_pool_detail(self): + result = self.run_command(['account', 'bandwidth-pools-detail', '123456']) + self.assert_no_fail(result) + self.assert_called_with('SoftLayer_Network_Bandwidth_Version1_Allotment', 'getObject')