Skip to content

Commit bbdff7b

Browse files
Merge pull request #1579 from softlayer/issues1575
Bandwidth pool features
2 parents d3588cc + 2665d36 commit bbdff7b

File tree

10 files changed

+238
-279
lines changed

10 files changed

+238
-279
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
11+
@click.command()
12+
@environment.pass_env
13+
def cli(env):
14+
"""Displays bandwidth pool information
15+
16+
Similiar to https://cloud.ibm.com/classic/network/bandwidth/vdr
17+
"""
18+
19+
manager = AccountManager(env.client)
20+
items = manager.get_bandwidth_pools()
21+
22+
table = formatting.Table([
23+
"Pool Name",
24+
"Region",
25+
"Servers",
26+
"Allocation",
27+
"Current Usage",
28+
"Projected Usage"
29+
], title="Bandwidth Pools")
30+
table.align = 'l'
31+
32+
for item in items:
33+
name = item.get('name')
34+
region = utils.lookup(item, 'locationGroup', 'name')
35+
servers = manager.get_bandwidth_pool_counts(identifier=item.get('id'))
36+
allocation = "{} GB".format(item.get('totalBandwidthAllocated', 0))
37+
current = "{} GB".format(utils.lookup(item, 'billingCyclePublicBandwidthUsage', 'amountOut'))
38+
projected = "{} GB".format(item.get('projectedPublicBandwidthUsage', 0))
39+
40+
table.add_row([name, region, servers, allocation, current, projected])
41+
env.fout(table)

SoftLayer/CLI/report/bandwidth.py

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,25 @@ def _get_pooled_bandwidth(env, start, end):
4747
label='Calculating for bandwidth pools',
4848
file=sys.stderr) as pools:
4949
for pool in pools:
50-
if not pool.get('metricTrackingObjectId'):
51-
continue
52-
53-
yield {
54-
'id': pool['id'],
50+
pool_detail = {
51+
'id': pool.get('id'),
5552
'type': 'pool',
56-
'name': pool['name'],
57-
'data': env.client.call(
53+
'name': pool.get('name'),
54+
'data': []
55+
}
56+
if pool.get('metricTrackingObjectId'):
57+
bw_data = env.client.call(
5858
'Metric_Tracking_Object',
5959
'getSummaryData',
6060
start.strftime('%Y-%m-%d %H:%M:%S %Z'),
6161
end.strftime('%Y-%m-%d %H:%M:%S %Z'),
6262
types,
6363
300,
64-
id=pool['metricTrackingObjectId'],
65-
),
66-
}
64+
id=pool.get('metricTrackingObjectId'),
65+
)
66+
pool_detail['data'] = bw_data
67+
68+
yield pool_detail
6769

6870

6971
def _get_hardware_bandwidth(env, start, end):
@@ -172,28 +174,20 @@ def _get_virtual_bandwidth(env, start, end):
172174

173175

174176
@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)
177+
@click.option('--start', callback=_validate_datetime,
178+
default=(datetime.datetime.now() - datetime.timedelta(days=30)).strftime('%Y-%m-%d'),
179+
help="datetime in the format 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS'")
180+
@click.option('--end', callback=_validate_datetime, default=datetime.datetime.now().strftime('%Y-%m-%d'),
181+
help="datetime in the format 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS'")
182+
@click.option('--sortby', help='Column to sort by', default='hostname', show_default=True)
183+
@click.option('--virtual', is_flag=True, default=False,
184+
help='Show only the bandwidth summary for each virtual server')
185+
@click.option('--server', is_flag=True, default=False,
186+
help='Show only the bandwidth summary for each hardware server')
187+
@click.option('--pool', is_flag=True, default=False,
188+
help='Show only the bandwidth pool summary.')
195189
@environment.pass_env
196-
def cli(env, start, end, sortby, virtual, server):
190+
def cli(env, start, end, sortby, virtual, server, pool):
197191
"""Bandwidth report for every pool/server.
198192
199193
This reports on the total data transfered for each virtual sever, hardware
@@ -243,6 +237,9 @@ def _input_to_table(item):
243237
for item in itertools.chain(_get_pooled_bandwidth(env, start, end),
244238
_get_hardware_bandwidth(env, start, end)):
245239
_input_to_table(item)
240+
elif pool:
241+
for item in _get_pooled_bandwidth(env, start, end):
242+
_input_to_table(item)
246243
else:
247244
for item in itertools.chain(_get_pooled_bandwidth(env, start, end),
248245
_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/fixtures/SoftLayer_Account.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,3 +1207,21 @@
12071207
"version": 4
12081208
}
12091209
}]
1210+
1211+
getBandwidthAllotments = [{
1212+
'billingCyclePublicBandwidthUsage': {
1213+
'amountIn': '6.94517',
1214+
'amountOut': '6.8859'
1215+
},
1216+
'id': 309961,
1217+
'locationGroup': {
1218+
'description': 'All Datacenters in Mexico',
1219+
'id': 262,
1220+
'locationGroupTypeId': 1,
1221+
'name': 'MEX',
1222+
'securityLevelId': None
1223+
},
1224+
'name': 'MexRegion',
1225+
'projectedPublicBandwidthUsage': 9.88,
1226+
'totalBandwidthAllocated': 3361
1227+
}]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
getObject = {
2+
'id': 309961,
3+
'bareMetalInstanceCount': 0,
4+
'hardwareCount': 2,
5+
'virtualGuestCount': 0
6+
}

SoftLayer/managers/account.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,27 @@ 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, projectedPublicBandwidthUsage,
335+
billingCyclePublicBandwidthUsage[amountOut,amountIn]]
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+
343+
Getting the server counts individually is significantly faster than pulling them in
344+
with the get_bandwidth_pools api call.
345+
"""
346+
mask = "mask[id, bareMetalInstanceCount, hardwareCount, virtualGuestCount]"
347+
counts = self.client.call('SoftLayer_Network_Bandwidth_Version1_Allotment', 'getObject',
348+
id=identifier, mask=mask)
349+
total = counts.get('bareMetalInstanceCount', 0) + \
350+
counts.get('hardwareCount', 0) + \
351+
counts.get('virtualGuestCount', 0)
352+
return total

docs/cli/account.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ Account Commands
4343
.. click:: SoftLayer.CLI.account.licenses:cli
4444
:prog: account licenses
4545
:show-nested:
46+
47+
.. click:: SoftLayer.CLI.account.bandwidth_pools:cli
48+
:prog: account bandwidth-pools
49+
:show-nested:

tests/CLI/modules/account_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,9 @@ def test_acccount_licenses(self):
136136
self.assert_no_fail(result)
137137
self.assert_called_with('SoftLayer_Account', 'getActiveVirtualLicenses')
138138
self.assert_called_with('SoftLayer_Account', 'getActiveAccountLicenses')
139+
140+
def test_bandwidth_pools(self):
141+
result = self.run_command(['account', 'bandwidth-pools'])
142+
self.assert_no_fail(result)
143+
self.assert_called_with('SoftLayer_Account', 'getBandwidthAllotments')
144+
self.assert_called_with('SoftLayer_Network_Bandwidth_Version1_Allotment', 'getObject')

0 commit comments

Comments
 (0)