Skip to content

Commit f6830c8

Browse files
committed
Added new command - slcli bandwidth create-pools
1 parent afd8c87 commit f6830c8

File tree

10 files changed

+256
-1
lines changed

10 files changed

+256
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""Create 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.option('--name', required=True, help="Pool name")
27+
@click.option('--region', required=True,
28+
type=click.Choice(['SJC/DAL/WDC/TOR/MON', 'AMS/LON/MAD/PAR', 'SNG/HKG/OSA/TOK',
29+
'SYD', 'MEX', 'SAO', 'CHE', 'MIL', 'SEO', 'FRA']),
30+
help="Region selected")
31+
@environment.pass_env
32+
def cli(env, name, region):
33+
"""Create bandwidth pool."""
34+
35+
manager = BandwidthManager(env.client)
36+
locations = manager.get_location_group()
37+
id_location_group = get_id_from_location_group(locations, location_groups[region])
38+
created_pool = manager.create_pool(name, id_location_group)
39+
40+
table = formatting.KeyValueTable(['Name', 'Value'])
41+
table.add_row(['Id', created_pool.get('id')])
42+
table.add_row(['Name Pool', name])
43+
table.add_row(['Region', region])
44+
table.add_row(['Created Date', utils.clean_time(created_pool.get('createDate'))])
45+
env.fout(table)
46+
47+
48+
def get_id_from_location_group(locations, name):
49+
"""Gets the ID location group, from name"""
50+
for location in locations:
51+
if location['name'] == name:
52+
return location['id']
53+
54+
return None

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@
424424
('bandwidth:summary', 'SoftLayer.CLI.bandwidth.summary:cli'),
425425
('bandwidth:pools', 'SoftLayer.CLI.bandwidth.pools:cli'),
426426
('bandwidth:pools-detail', 'SoftLayer.CLI.bandwidth.pools_detail:cli'),
427+
('bandwidth:pools-create', 'SoftLayer.CLI.bandwidth.pools_create:cli'),
427428
]
428429

429430
ALL_ALIASES = {

SoftLayer/fixtures/SoftLayer_Account.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,8 @@
456456
if ticket['statusId'] == 1002]
457457

458458
getCurrentUser = {'id': 12345, 'username': 'testAccount',
459-
'apiAuthenticationKeys': [{'authenticationKey': 'A' * 64}]}
459+
'apiAuthenticationKeys': [{'authenticationKey': 'A' * 64}],
460+
'accountId': 123456789}
460461

461462
getCdnAccounts = [
462463
{
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
getAllObjects = [
2+
{
3+
"description": "All Datacenters in the USA and Canada.",
4+
"id": 1,
5+
"locationGroupTypeId": 1,
6+
"name": "US/Canada",
7+
"securityLevelId": None,
8+
"locationGroupType": {
9+
"name": "VDR"
10+
}
11+
},
12+
{
13+
"description": "All Datacenters in Amsterdam, London, Paris, and Madrid.",
14+
"id": 2,
15+
"locationGroupTypeId": 1,
16+
"name": "AMS/LON/MAD/PAR",
17+
"securityLevelId": None,
18+
"locationGroupType": {
19+
"name": "VDR"
20+
}
21+
},
22+
{
23+
"description": "All Datacenters in Singapore, Hong Kong and Japan.",
24+
"id": 3,
25+
"locationGroupTypeId": 1,
26+
"name": "SNG/HKG/JPN",
27+
"securityLevelId": None,
28+
"locationGroupType": {
29+
"name": "VDR"
30+
}
31+
},
32+
{
33+
"description": "All Datacenters in Australia",
34+
"id": 222,
35+
"locationGroupTypeId": 1,
36+
"name": "AUS",
37+
"securityLevelId": None,
38+
"locationGroupType": {
39+
"name": "VDR"
40+
}
41+
},
42+
{
43+
"description": "All Datacenters in Mexico",
44+
"id": 262,
45+
"locationGroupTypeId": 1,
46+
"name": "MEX",
47+
"securityLevelId": None,
48+
"locationGroupType": {
49+
"name": "VDR"
50+
}
51+
},
52+
{
53+
"description": "All Datacenters in Brazil",
54+
"id": 665,
55+
"locationGroupTypeId": 1,
56+
"name": "BRA",
57+
"securityLevelId": None,
58+
"locationGroupType": {
59+
"name": "VDR"
60+
}
61+
},
62+
{
63+
"description": "All Datacenters in India",
64+
"id": 745,
65+
"locationGroupTypeId": 1,
66+
"name": "IND",
67+
"securityLevelId": None,
68+
"locationGroupType": {
69+
"name": "VDR"
70+
}
71+
},
72+
{
73+
"description": "All Datacenters in Italy",
74+
"id": 863,
75+
"locationGroupTypeId": 1,
76+
"name": "ITA",
77+
"securityLevelId": None,
78+
"locationGroupType": {
79+
"name": "VDR"
80+
}
81+
},
82+
{
83+
"description": "All Datacenters in South Korea",
84+
"id": 1023,
85+
"locationGroupTypeId": 1,
86+
"name": "KOR",
87+
"securityLevelId": None,
88+
"locationGroupType": {
89+
"name": "VDR"
90+
}
91+
},
92+
{
93+
"description": "All Datacenters in Frankfurt",
94+
"id": 2023,
95+
"locationGroupTypeId": 1,
96+
"name": "FRA",
97+
"securityLevelId": None,
98+
"locationGroupType": {
99+
"name": "VDR"
100+
}
101+
}
102+
]

SoftLayer/fixtures/SoftLayer_Network_Bandwidth_Version1_Allotment.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,13 @@
146146
}
147147
}]
148148
}
149+
createObject = {
150+
"accountId": 654321,
151+
"bandwidthAllotmentTypeId": 2,
152+
"createDate": "2023-05-31T07:22:27-06:00",
153+
"endDate": None,
154+
"id": 123456789,
155+
"locationGroupId": 2,
156+
"name": "NewRegion",
157+
"serviceProviderId": 1
158+
}

SoftLayer/managers/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
:license: MIT, see LICENSE for more details.
99
"""
1010
from SoftLayer.managers.account import AccountManager
11+
from SoftLayer.managers.bandwidth import BandwidthManager
1112
from SoftLayer.managers.block import BlockStorageManager
1213
from SoftLayer.managers.cdn import CDNManager
1314
from SoftLayer.managers.dedicated_host import DedicatedHostManager
@@ -36,6 +37,7 @@
3637

3738
__all__ = [
3839
'AccountManager',
40+
'BandwidthManager',
3941
'BlockStorageManager',
4042
'CapacityManager',
4143
'CDNManager',

SoftLayer/managers/bandwidth.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
SoftLayer.bandwidth
3+
~~~~~~~~~~~~~~~~~~~~~~~
4+
Bandwidth manager
5+
6+
:license: MIT, see License for more details.
7+
"""
8+
9+
from SoftLayer import utils
10+
11+
# pylint: disable=invalid-name
12+
13+
14+
class BandwidthManager(utils.IdentifierMixin, object):
15+
"""Common functions for getting information from the Bandwidth service
16+
17+
:param SoftLayer.API.BaseClient client: the client instance
18+
"""
19+
20+
def __init__(self, client):
21+
self.client = client
22+
23+
def get_location_group(self):
24+
"""Gets some basic bandwidth information
25+
26+
:return: Bandwidth object
27+
"""
28+
_filters = {"locationGroupTypeId": {"operation": 1}}
29+
30+
return self.client.call('Location_Group', 'getAllObjects', filter=_filters)
31+
32+
def create_pool(self, name_pool, id_location_group):
33+
"""Gets some basic bandwidth information
34+
35+
:return: Bandwidth object
36+
"""
37+
current_user = self.client.call('Account', 'getCurrentUser')
38+
template = {
39+
"accountId": current_user['accountId'],
40+
"bandwidthAllotmentTypeId": 2,
41+
"locationGroupId": id_location_group,
42+
"name": name_pool
43+
}
44+
45+
return self.client.call('Network_Bandwidth_Version1_Allotment', 'createObject', template)

docs/cli/bandwidth.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ bandwidth Commands
1515
.. click:: SoftLayer.CLI.bandwidth.summary:cli
1616
:prog: bandwidth summary
1717
:show-nested:
18+
19+
.. click:: SoftLayer.CLI.bandwidth.pools_create:cli
20+
:prog: bandwidth pools-create
21+
:show-nested:

tests/CLI/modules/bandwidth_tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ def test_bandwidth_summary(self):
4646
self.assertEqual('0.00 MB', json_output[3]['Data out'])
4747
self.assertEqual('Pay-As-You-Go', json_output[4]['Allocation'])
4848

49+
def test_create_bandwidth(self):
50+
result = self.run_command(['bandwidth', 'pools-create', '--name=NewRegion', '--region=SJC/DAL/WDC/TOR/MON'])
51+
self.assert_no_fail(result)
52+
self.assert_called_with('SoftLayer_Network_Bandwidth_Version1_Allotment', 'createObject')
53+
json_output = json.loads(result.output)
54+
pp(json_output)
55+
self.assertEqual(123456789, json_output['Id'])
56+
self.assertEqual('NewRegion', json_output['Name Pool'])
57+
self.assertEqual('SJC/DAL/WDC/TOR/MON', json_output['Region'])
58+
4959

5060
def _bandwidth_advanced_search():
5161
result = [

tests/managers/bandwidth_tests.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
SoftLayer.tests.managers.bandwidth_tests
3+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4+
5+
"""
6+
from SoftLayer.managers.bandwidth import BandwidthManager as BandwidthManager
7+
from SoftLayer import testing
8+
9+
10+
class BandwidthManagerTests(testing.TestCase):
11+
12+
def set_up(self):
13+
self.manager = BandwidthManager(self.client)
14+
15+
def test_get_location_group(self):
16+
self.manager.get_location_group()
17+
_filter = {
18+
"locationGroupTypeId": {
19+
"operation": 1
20+
}
21+
}
22+
self.assert_called_with('SoftLayer_Location_Group', 'getAllObjects', filter=_filter)
23+
24+
def test_create_pool(self):
25+
self.manager.create_pool(name_pool='New region', id_location_group=2)
26+
self.assert_called_with('SoftLayer_Network_Bandwidth_Version1_Allotment', 'createObject')

0 commit comments

Comments
 (0)