Skip to content

Commit f0cbd24

Browse files
Merge pull request #1991 from edsonarios/issue1989
New command `slcli bandwidth pools-edit`
2 parents 7a2792c + f74a13b commit f0cbd24

File tree

6 files changed

+92
-4
lines changed

6 files changed

+92
-4
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""Edit bandwidth pool."""
2+
# :license: MIT, see LICENSE for more details.
3+
import click
4+
5+
from SoftLayer import BandwidthManager
6+
from SoftLayer.CLI.command import SLCommand as SLCommand
7+
from SoftLayer.CLI import environment
8+
from SoftLayer.CLI import formatting
9+
from SoftLayer import utils
10+
11+
location_groups = {
12+
"SJC/DAL/WDC/TOR/MON": "US/Canada",
13+
"AMS/LON/MAD/PAR": "AMS/LON/MAD/PAR",
14+
"SNG/HKG/OSA/TOK": "SNG/HKG/JPN",
15+
"SYD": "AUS",
16+
"MEX": "MEX",
17+
"SAO": "BRA",
18+
"CHE": "IND",
19+
"MIL": "ITA",
20+
"SEO": "KOR",
21+
"FRA": "FRA"
22+
}
23+
24+
25+
@click.command(cls=SLCommand)
26+
@click.argument('identifier')
27+
@click.option('--name', required=True, help="Pool name")
28+
@environment.pass_env
29+
def cli(env, identifier, name):
30+
"""Edit bandwidth pool."""
31+
32+
manager = BandwidthManager(env.client)
33+
bandwidth_pool = manager.edit_pool(identifier, name)
34+
35+
if bandwidth_pool:
36+
37+
edited_pool = manager.get_bandwidth_detail(identifier)
38+
locations = manager.get_location_group()
39+
40+
location = next(
41+
(location for location in locations if location['id'] == edited_pool.get('locationGroupId')), None)
42+
43+
region_name = next((key for key, value in location_groups.items() if value == location.get('name')), None)
44+
45+
table = formatting.KeyValueTable(['Name', 'Value'])
46+
table.add_row(['Id', edited_pool.get('id')])
47+
table.add_row(['Name Pool', name])
48+
table.add_row(['Region', region_name])
49+
table.add_row(['Created Date', utils.clean_time(edited_pool.get('createDate'))])
50+
env.fout(table)

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@
425425
('bandwidth:pools', 'SoftLayer.CLI.bandwidth.pools:cli'),
426426
('bandwidth:pools-detail', 'SoftLayer.CLI.bandwidth.pools_detail:cli'),
427427
('bandwidth:pools-create', 'SoftLayer.CLI.bandwidth.pools_create:cli'),
428+
('bandwidth:pools-edit', 'SoftLayer.CLI.bandwidth.pools_edit:cli'),
428429
]
429430

430431
ALL_ALIASES = {

SoftLayer/fixtures/SoftLayer_Network_Bandwidth_Version1_Allotment.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,5 @@
156156
"name": "NewRegion",
157157
"serviceProviderId": 1
158158
}
159+
160+
editObject = True

SoftLayer/managers/bandwidth.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,29 @@ def create_pool(self, name_pool, id_location_group):
4343
}
4444

4545
return self.client.call('Network_Bandwidth_Version1_Allotment', 'createObject', template)
46+
47+
def get_bandwidth_detail(self, identifier):
48+
"""Gets bandwidth pool detail.
49+
50+
:returns: bandwidth pool detail
51+
"""
52+
_mask = """activeDetails[allocation],projectedPublicBandwidthUsage, billingCyclePublicBandwidthUsage,
53+
hardware[outboundBandwidthUsage,bandwidthAllotmentDetail[allocation]],inboundPublicBandwidthUsage,
54+
virtualGuests[outboundPublicBandwidthUsage,bandwidthAllotmentDetail[allocation]],
55+
bareMetalInstances[outboundBandwidthUsage,bandwidthAllotmentDetail[allocation]]"""
56+
return self.client['SoftLayer_Network_Bandwidth_Version1_Allotment'].getObject(id=identifier, mask=_mask)
57+
58+
def edit_pool(self, identifier, new_name_pool):
59+
"""Edit bandwidth pool name
60+
61+
:return: Bandwidth object
62+
"""
63+
actual_bandwidth = self.get_bandwidth_detail(identifier)
64+
template = {
65+
"accountId": actual_bandwidth.get('accountId'),
66+
"bandwidthAllotmentTypeId": actual_bandwidth.get('bandwidthAllotmentTypeId'),
67+
"locationGroupId": actual_bandwidth.get('locationGroupId'),
68+
"name": new_name_pool
69+
}
70+
71+
return self.client.call('Network_Bandwidth_Version1_Allotment', 'editObject', template, id=identifier)

docs/cli/bandwidth.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ bandwidth Commands
1919
.. click:: SoftLayer.CLI.bandwidth.pools_create:cli
2020
:prog: bandwidth pools-create
2121
:show-nested:
22+
23+
.. click:: SoftLayer.CLI.bandwidth.pools_edit:cli
24+
:prog: bandwidth pools-edit
25+
:show-nested:

tests/CLI/modules/bandwidth_tests.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
import json
1010

11-
from pprint import pprint as pp
12-
1311

1412
class BandwidthTests(testing.TestCase):
1513
def test_bandwidth_pools(self):
@@ -31,7 +29,6 @@ def test_bandwidth_summary(self):
3129
self.assert_no_fail(result)
3230
self.assert_called_with('SoftLayer_Search', 'advancedSearch')
3331
json_output = json.loads(result.output)
34-
pp(json_output)
3532
self.assertEqual(5, len(json_output))
3633
self.assertEqual(100250634, json_output[0]['Id'])
3734
self.assertEqual('TestRegion', json_output[0]['Pool'])
@@ -51,11 +48,19 @@ def test_create_bandwidth(self):
5148
self.assert_no_fail(result)
5249
self.assert_called_with('SoftLayer_Network_Bandwidth_Version1_Allotment', 'createObject')
5350
json_output = json.loads(result.output)
54-
pp(json_output)
5551
self.assertEqual(123456789, json_output['Id'])
5652
self.assertEqual('NewRegion', json_output['Name Pool'])
5753
self.assertEqual('SJC/DAL/WDC/TOR/MON', json_output['Region'])
5854

55+
def test_edit_bandwidth(self):
56+
result = self.run_command(['bandwidth', 'pools-edit', '123456', '--name=MexRegionEdited'])
57+
self.assert_no_fail(result)
58+
self.assert_called_with('SoftLayer_Network_Bandwidth_Version1_Allotment', 'editObject')
59+
json_output = json.loads(result.output)
60+
self.assertEqual(123456, json_output['Id'])
61+
self.assertEqual('MexRegionEdited', json_output['Name Pool'])
62+
self.assertEqual('MEX', json_output['Region'])
63+
5964

6065
def _bandwidth_advanced_search():
6166
result = [

0 commit comments

Comments
 (0)