Skip to content

Commit c93fa57

Browse files
#1575 added account bandwidth-pools and updated reports bandwidth to support the --pool option
1 parent 6539959 commit c93fa57

File tree

4 files changed

+93
-31
lines changed

4 files changed

+93
-31
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""Displays information about the accounts bandwidth pools"""
2+
# :license: MIT, see LICENSE for more details.
3+
import click
4+
5+
from SoftLayer.CLI import environment
6+
from SoftLayer.CLI import formatting
7+
from SoftLayer.managers.account import AccountManager as AccountManager
8+
from SoftLayer import utils
9+
10+
from pprint import pprint as pp
11+
12+
@click.command()
13+
@environment.pass_env
14+
def cli(env):
15+
"""Lists billing items with some other useful information.
16+
17+
Similiar to https://cloud.ibm.com/billing/billing-items
18+
"""
19+
20+
manager = AccountManager(env.client)
21+
items = manager.get_bandwidth_pools()
22+
# table = item_table(items)
23+
pp(items)
24+
table = formatting.Table([
25+
"Pool Name",
26+
"Region",
27+
"Servers",
28+
"Allocation",
29+
"Current Usage",
30+
"Projected Usage"
31+
], title="Bandwidth Pools")
32+
table.align = 'l'
33+
34+
for item in items:
35+
name = item.get('name')
36+
region = utils.lookup(item, 'locationGroup', 'name')
37+
servers = manager.get_bandwidth_pool_counts(identifier=item.get('id'))
38+
allocation = item.get('totalBandwidthAllocated', 0)
39+
current = item.get('billingCyclePublicUsageTotal', 0)
40+
projected = item.get('projectedPublicBandwidthUsage', 0)
41+
42+
table.add_row([name, region, servers, allocation, current, projected,])
43+
env.fout(table)

SoftLayer/CLI/report/bandwidth.py

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from SoftLayer.CLI import formatting
1010
from SoftLayer import utils
1111

12+
from pprint import pprint as pp
1213

1314
# pylint: disable=unused-argument
1415
def _validate_datetime(ctx, param, value):
@@ -47,23 +48,25 @@ def _get_pooled_bandwidth(env, start, end):
4748
label='Calculating for bandwidth pools',
4849
file=sys.stderr) as pools:
4950
for pool in pools:
50-
if not pool.get('metricTrackingObjectId'):
51-
continue
52-
53-
yield {
54-
'id': pool['id'],
51+
pool_detail = {
52+
'id': pool.get('id'),
5553
'type': 'pool',
56-
'name': pool['name'],
57-
'data': env.client.call(
54+
'name': pool.get('name'),
55+
'data': []
56+
}
57+
if pool.get('metricTrackingObjectId'):
58+
bw_data = env.client.call(
5859
'Metric_Tracking_Object',
5960
'getSummaryData',
6061
start.strftime('%Y-%m-%d %H:%M:%S %Z'),
6162
end.strftime('%Y-%m-%d %H:%M:%S %Z'),
6263
types,
6364
300,
64-
id=pool['metricTrackingObjectId'],
65-
),
66-
}
65+
id=pool.get('metricTrackingObjectId'),
66+
)
67+
pool_detail['data'] = bw_data
68+
69+
yield pool_detail
6770

6871

6972
def _get_hardware_bandwidth(env, start, end):
@@ -172,28 +175,20 @@ def _get_virtual_bandwidth(env, start, end):
172175

173176

174177
@click.command(short_help="Bandwidth report for every pool/server")
175-
@click.option(
176-
'--start',
177-
callback=_validate_datetime,
178-
default=(datetime.datetime.now() - datetime.timedelta(days=30)
179-
).strftime('%Y-%m-%d'),
180-
help="datetime in the format 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS'")
181-
@click.option(
182-
'--end',
183-
callback=_validate_datetime,
184-
default=datetime.datetime.now().strftime('%Y-%m-%d'),
185-
help="datetime in the format 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS'")
186-
@click.option('--sortby', help='Column to sort by',
187-
default='hostname',
188-
show_default=True)
189-
@click.option('--virtual', is_flag=True,
190-
help='Show the all bandwidth summary for each virtual server',
191-
default=False)
192-
@click.option('--server', is_flag=True,
193-
help='show the all bandwidth summary for each hardware server',
194-
default=False)
178+
@click.option('--start', callback=_validate_datetime,
179+
default=(datetime.datetime.now() - datetime.timedelta(days=30)).strftime('%Y-%m-%d'),
180+
help="datetime in the format 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS'")
181+
@click.option('--end', callback=_validate_datetime, default=datetime.datetime.now().strftime('%Y-%m-%d'),
182+
help="datetime in the format 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS'")
183+
@click.option('--sortby', help='Column to sort by', default='hostname', show_default=True)
184+
@click.option('--virtual', is_flag=True, default=False,
185+
help='Show only the bandwidth summary for each virtual server')
186+
@click.option('--server', is_flag=True, default=False,
187+
help='Show only the bandwidth summary for each hardware server')
188+
@click.option('--pool', is_flag=True, default=False,
189+
help='Show only the bandwidth pool summary.')
195190
@environment.pass_env
196-
def cli(env, start, end, sortby, virtual, server):
191+
def cli(env, start, end, sortby, virtual, server, pool):
197192
"""Bandwidth report for every pool/server.
198193
199194
This reports on the total data transfered for each virtual sever, hardware
@@ -243,6 +238,9 @@ def _input_to_table(item):
243238
for item in itertools.chain(_get_pooled_bandwidth(env, start, end),
244239
_get_hardware_bandwidth(env, start, end)):
245240
_input_to_table(item)
241+
elif pool:
242+
for item in _get_pooled_bandwidth(env, start, end):
243+
_input_to_table(item)
246244
else:
247245
for item in itertools.chain(_get_pooled_bandwidth(env, start, end),
248246
_get_hardware_bandwidth(env, start, end),

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
('account:item-detail', 'SoftLayer.CLI.account.item_detail:cli'),
2323
('account:cancel-item', 'SoftLayer.CLI.account.cancel_item:cli'),
2424
('account:orders', 'SoftLayer.CLI.account.orders:cli'),
25+
('account:bandwidth-pools', 'SoftLayer.CLI.account.bandwidth_pools:cli'),
2526

2627
('virtual', 'SoftLayer.CLI.virt'),
2728
('virtual:bandwidth', 'SoftLayer.CLI.virt.bandwidth:cli'),

SoftLayer/managers/account.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,23 @@ def get_active_account_licenses(self):
326326
_mask = """billingItem,softwareDescription"""
327327

328328
return self.client['SoftLayer_Account'].getActiveAccountLicenses(mask=_mask)
329+
330+
def get_bandwidth_pools(self, mask=None):
331+
"""Gets all the bandwidth pools on an account"""
332+
333+
if mask is None:
334+
mask = """mask[totalBandwidthAllocated,locationGroup, id, name, billingCyclePublicUsageTotal,
335+
projectedPublicBandwidthUsage]
336+
"""
337+
338+
return self.client.call('SoftLayer_Account', 'getBandwidthAllotments', mask=mask, iter=True)
339+
340+
def get_bandwidth_pool_counts(self, identifier):
341+
"""Gets a count of all servers in a bandwidth pool"""
342+
mask = "mask[id, bareMetalInstanceCount, hardwareCount, virtualGuestCount]"
343+
counts = self.client.call('SoftLayer_Network_Bandwidth_Version1_Allotment', 'getObject',
344+
id=identifier, mask=mask)
345+
total = counts.get('bareMetalInstanceCount', 0) + \
346+
counts.get('hardwareCount', 0) + \
347+
counts.get('virtualGuestCount', 0)
348+
return total

0 commit comments

Comments
 (0)