Skip to content

Commit

Permalink
Merge pull request #1579 from softlayer/issues1575
Browse files Browse the repository at this point in the history
Bandwidth pool features
  • Loading branch information
allmightyspiff authored Feb 1, 2022
2 parents d3588cc + 2665d36 commit bbdff7b
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 279 deletions.
41 changes: 41 additions & 0 deletions SoftLayer/CLI/account/bandwidth_pools.py
Original file line number Diff line number Diff line change
@@ -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)
59 changes: 28 additions & 31 deletions SoftLayer/CLI/report/bandwidth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions SoftLayer/CLI/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
18 changes: 18 additions & 0 deletions SoftLayer/fixtures/SoftLayer_Account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
getObject = {
'id': 309961,
'bareMetalInstanceCount': 0,
'hardwareCount': 2,
'virtualGuestCount': 0
}
24 changes: 24 additions & 0 deletions SoftLayer/managers/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions docs/cli/account.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
6 changes: 6 additions & 0 deletions tests/CLI/modules/account_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Loading

0 comments on commit bbdff7b

Please sign in to comment.