Skip to content

Commit 44a5df2

Browse files
J JayasilanJ Jayasilan
authored andcommitted
multithreading implementation for slcli bandwidth pools
1 parent e029225 commit 44a5df2

File tree

2 files changed

+59
-22
lines changed

2 files changed

+59
-22
lines changed

SoftLayer/CLI/bandwidth/pools.py

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"""Displays information about the bandwidth pools"""
22
# :license: MIT, see LICENSE for more details.
3+
import concurrent.futures as cf
4+
import logging
5+
import time
6+
37
import click
48

59
from SoftLayer.CLI.command import SLCommand as SLCommand
@@ -8,6 +12,8 @@
812
from SoftLayer.managers.account import AccountManager as AccountManager
913
from SoftLayer import utils
1014

15+
LOGGER = logging.getLogger(__name__)
16+
1117

1218
@click.command(cls=SLCommand, )
1319
@environment.pass_env
@@ -21,6 +27,7 @@ def cli(env):
2127
"""
2228

2329
manager = AccountManager(env.client)
30+
2431
items = manager.get_bandwidth_pools()
2532

2633
table = formatting.Table([
@@ -34,26 +41,34 @@ def cli(env):
3441
"Cost"
3542
], title="Bandwidth Pools")
3643
table.align = 'l'
37-
for item in items:
38-
id_bandwidth = item.get('id')
39-
name = item.get('name')
40-
region = utils.lookup(item, 'locationGroup', 'name')
41-
servers = manager.get_bandwidth_pool_counts(identifier=item.get('id'))
42-
allocation = f"{item.get('totalBandwidthAllocated', 0)} GB"
43-
44-
current = utils.lookup(item, 'billingCyclePublicBandwidthUsage', 'amountOut')
45-
if current is not None:
46-
current = f"{current} GB"
47-
else:
48-
current = "0 GB"
49-
50-
projected = f"{item.get('projectedPublicBandwidthUsage', 0)} GB"
51-
52-
cost = utils.lookup(item, 'billingItem', 'nextInvoiceTotalRecurringAmount')
53-
if cost is not None:
54-
cost = f"${cost}"
55-
else:
56-
cost = "$0.0"
57-
58-
table.add_row([id_bandwidth, name, region, servers, allocation, current, projected, cost])
44+
start_m = time.perf_counter()
45+
46+
with cf.ThreadPoolExecutor(max_workers=5) as executor:
47+
for item, servers in zip(items, executor.map(manager.get_bandwidth_pool_counts,
48+
[item.get('id') for item in items])):
49+
50+
id_bandwidth = item.get('id')
51+
name = item.get('name')
52+
region = utils.lookup(item, 'locationGroup', 'name')
53+
54+
allocation = f"{item.get('totalBandwidthAllocated', 0)} GB"
55+
56+
current = utils.lookup(item, 'billingCyclePublicBandwidthUsage', 'amountOut')
57+
if current is not None:
58+
current = f"{current} GB"
59+
else:
60+
current = "0 GB"
61+
62+
projected = f"{item.get('projectedPublicBandwidthUsage', 0)} GB"
63+
64+
cost = utils.lookup(item, 'billingItem', 'nextInvoiceTotalRecurringAmount')
65+
if cost is not None:
66+
cost = f"${cost}"
67+
else:
68+
cost = "$0.0"
69+
70+
table.add_row([id_bandwidth, name, region, servers, allocation, current, projected, cost])
71+
72+
end_m = time.perf_counter()
73+
LOGGER.debug('Total API Call time %s', end_m - start_m)
5974
env.fout(table)

tests/CLI/modules/bandwidth_tests.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ def test_edit_bandwidth(self):
6161
self.assertEqual('MexRegionEdited', json_output['Name Pool'])
6262
self.assertEqual('MEX', json_output['Region'])
6363

64+
def test_bandwidth_pools_no_amount(self):
65+
bandwidth_mock = self.set_mock('SoftLayer_Account', 'getBandwidthAllotments')
66+
bandwidth_mock.return_value = [{
67+
'billingCyclePublicBandwidthUsage': {
68+
'amountIn': '6.94517'
69+
},
70+
'id': 309961,
71+
'locationGroup': {
72+
'description': 'All Datacenters in Mexico',
73+
'id': 262,
74+
'locationGroupTypeId': 1,
75+
'name': 'MEX',
76+
'securityLevelId': None
77+
},
78+
'billingItem': {'nextInvoiceTotalRecurringAmount': 0.0},
79+
'name': 'MexRegion',
80+
'projectedPublicBandwidthUsage': 9.88,
81+
'totalBandwidthAllocated': 3361
82+
}]
83+
result = self.run_command(['bandwidth', 'pools'])
84+
self.assert_no_fail(result)
85+
6486

6587
def _bandwidth_advanced_search():
6688
result = [

0 commit comments

Comments
 (0)