Skip to content

Commit c94209d

Browse files
committed
Added new command - slcli bandwidth pools-edit
1 parent aaaca0a commit c94209d

File tree

6 files changed

+93
-0
lines changed

6 files changed

+93
-0
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ def test_create_bandwidth(self):
5656
self.assertEqual('NewRegion', json_output['Name Pool'])
5757
self.assertEqual('SJC/DAL/WDC/TOR/MON', json_output['Region'])
5858

59+
def test_edit_bandwidth(self):
60+
result = self.run_command(['bandwidth', 'pools-edit', '123456', '--name=MexRegionEdited'])
61+
self.assert_no_fail(result)
62+
self.assert_called_with('SoftLayer_Network_Bandwidth_Version1_Allotment', 'editObject')
63+
json_output = json.loads(result.output)
64+
pp(json_output)
65+
self.assertEqual(123456, json_output['Id'])
66+
self.assertEqual('MexRegionEdited', json_output['Name Pool'])
67+
self.assertEqual('MEX', json_output['Region'])
68+
5969

6070
def _bandwidth_advanced_search():
6171
result = [

0 commit comments

Comments
 (0)