Skip to content

Commit 68b20d0

Browse files
Merge pull request #2008 from softlayer/jayasilan-issue2007
Multithreading implementation for slcli bandwidth pools command
2 parents 7b530a0 + bac88e7 commit 68b20d0

File tree

2 files changed

+58
-21
lines changed

2 files changed

+58
-21
lines changed

SoftLayer/CLI/bandwidth/pools.py

Lines changed: 36 additions & 21 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([
@@ -35,31 +42,39 @@ def cli(env):
3542
"Deletion"
3643
], title="Bandwidth Pools")
3744
table.align = 'l'
38-
for item in items:
39-
id_bandwidth = item.get('id')
40-
name = item.get('name')
41-
region = utils.lookup(item, 'locationGroup', 'name')
42-
servers = manager.get_bandwidth_pool_counts(identifier=item.get('id'))
43-
allocation = f"{item.get('totalBandwidthAllocated', 0)} GB"
4445

45-
current = utils.lookup(item, 'billingCyclePublicBandwidthUsage', 'amountOut')
46-
if current is not None:
47-
current = f"{current} GB"
48-
else:
49-
current = "0 GB"
46+
start_m = time.perf_counter()
47+
48+
with cf.ThreadPoolExecutor(max_workers=5) as executor:
49+
for item, servers in zip(items, executor.map(manager.get_bandwidth_pool_counts,
50+
[item.get('id') for item in items])):
51+
52+
id_bandwidth = item.get('id')
53+
name = item.get('name')
54+
region = utils.lookup(item, 'locationGroup', 'name')
55+
56+
allocation = f"{item.get('totalBandwidthAllocated', 0)} GB"
57+
58+
current = utils.lookup(item, 'billingCyclePublicBandwidthUsage', 'amountOut')
59+
if current is not None:
60+
current = f"{current} GB"
61+
else:
62+
current = "0 GB"
5063

51-
projected = f"{item.get('projectedPublicBandwidthUsage', 0)} GB"
64+
projected = f"{item.get('projectedPublicBandwidthUsage', 0)} GB"
5265

53-
cost = utils.lookup(item, 'billingItem', 'nextInvoiceTotalRecurringAmount')
54-
if cost is not None:
55-
cost = f"${cost}"
56-
else:
57-
cost = "$0.0"
66+
cost = utils.lookup(item, 'billingItem', 'nextInvoiceTotalRecurringAmount')
67+
if cost is not None:
68+
cost = f"${cost}"
69+
else:
70+
cost = "$0.0"
5871

59-
deletion = utils.clean_time(item.get('endDate'))
72+
deletion = utils.clean_time(item.get('endDate'))
73+
if deletion == '':
74+
deletion = formatting.blank()
6075

61-
if deletion == '':
62-
deletion = formatting.blank()
76+
table.add_row([id_bandwidth, name, region, servers, allocation, current, projected, cost, deletion])
6377

64-
table.add_row([id_bandwidth, name, region, servers, allocation, current, projected, cost, deletion])
78+
end_m = time.perf_counter()
79+
LOGGER.debug('Total API Call time %s', end_m - start_m)
6580
env.fout(table)

tests/CLI/modules/bandwidth_tests.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,28 @@ def test_create_bandwidth_single_region(self):
7777
self.assertEqual('NewRegion', json_output['Name Pool'])
7878
self.assertEqual('AMS/LON/MAD/PAR', json_output['Region'])
7979

80+
def test_bandwidth_pools_no_amount(self):
81+
bandwidth_mock = self.set_mock('SoftLayer_Account', 'getBandwidthAllotments')
82+
bandwidth_mock.return_value = [{
83+
'billingCyclePublicBandwidthUsage': {
84+
'amountIn': '6.94517'
85+
},
86+
'id': 309961,
87+
'locationGroup': {
88+
'description': 'All Datacenters in Mexico',
89+
'id': 262,
90+
'locationGroupTypeId': 1,
91+
'name': 'MEX',
92+
'securityLevelId': None
93+
},
94+
'billingItem': {'nextInvoiceTotalRecurringAmount': 0.0},
95+
'name': 'MexRegion',
96+
'projectedPublicBandwidthUsage': 9.88,
97+
'totalBandwidthAllocated': 3361
98+
}]
99+
result = self.run_command(['bandwidth', 'pools'])
100+
self.assert_no_fail(result)
101+
80102

81103
def _bandwidth_advanced_search():
82104
result = [

0 commit comments

Comments
 (0)