Skip to content

Commit 706845b

Browse files
Merge pull request #1499 from caberos/issue1496
add new feature on vlan cli
2 parents 6861d23 + 6112c54 commit 706845b

File tree

10 files changed

+210
-1
lines changed

10 files changed

+210
-1
lines changed

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@
346346
('user:vpn-subnet', 'SoftLayer.CLI.user.vpn_subnet:cli'),
347347

348348
('vlan', 'SoftLayer.CLI.vlan'),
349+
('vlan:create', 'SoftLayer.CLI.vlan.create:cli'),
349350
('vlan:detail', 'SoftLayer.CLI.vlan.detail:cli'),
350351
('vlan:edit', 'SoftLayer.CLI.vlan.edit:cli'),
351352
('vlan:list', 'SoftLayer.CLI.vlan.list:cli'),

SoftLayer/CLI/vlan/create.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""Order/create a VLAN instance."""
2+
# :license: MIT, see LICENSE for more details.
3+
import click
4+
import SoftLayer
5+
from SoftLayer.managers import ordering
6+
7+
from SoftLayer.CLI import environment
8+
from SoftLayer.CLI import exceptions
9+
from SoftLayer.CLI import formatting
10+
11+
12+
@click.command()
13+
@click.option('--name', required=False, prompt=True, help="Vlan name")
14+
@click.option('--datacenter', '-d', required=False, help="Datacenter shortname")
15+
@click.option('--pod', '-p', required=False, help="Pod name. E.g dal05.pod01")
16+
@click.option('--network', default='public', show_default=True, type=click.Choice(['public', 'private']),
17+
help='Network vlan type')
18+
@click.option('--billing', default='hourly', show_default=True, type=click.Choice(['hourly', 'monthly']),
19+
help="Billing rate")
20+
@environment.pass_env
21+
def cli(env, name, datacenter, pod, network, billing):
22+
"""Order/create a VLAN instance."""
23+
24+
item_package = ['PUBLIC_NETWORK_VLAN']
25+
complex_type = 'SoftLayer_Container_Product_Order_Network_Vlan'
26+
extras = {'name': name}
27+
if pod:
28+
datacenter = pod.split('.')[0]
29+
mgr = SoftLayer.NetworkManager(env.client)
30+
pods = mgr.get_pods()
31+
for router in pods:
32+
if router.get('name') == pod:
33+
if network == 'public':
34+
extras['routerId'] = router.get('frontendRouterId')
35+
elif network == 'private':
36+
extras['routerId'] = router.get('backendRouterId')
37+
break
38+
if not extras.get('routerId'):
39+
raise exceptions.CLIAbort(
40+
"Unable to find pod name: {}".format(pod))
41+
if network == 'private':
42+
item_package = ['PRIVATE_NETWORK_VLAN']
43+
44+
ordering_manager = ordering.OrderingManager(env.client)
45+
result = ordering_manager.place_order(package_keyname='NETWORK_VLAN',
46+
location=datacenter,
47+
item_keynames=item_package,
48+
complex_type=complex_type,
49+
hourly=billing,
50+
extras=extras)
51+
table = formatting.KeyValueTable(['name', 'value'])
52+
table.align['name'] = 'r'
53+
table.align['value'] = 'l'
54+
table.add_row(['id', result['orderId']])
55+
table.add_row(['created', result['orderDate']])
56+
table.add_row(['name', result['orderDetails']['orderContainers'][0]['name']])
57+
58+
env.fout(table)

SoftLayer/fixtures/SoftLayer_Network_Pod.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,15 @@
1818
'frontendRouterId': 1114993,
1919
'frontendRouterName': 'fcr01a.wdc07',
2020
'name': 'wdc07.pod01'
21+
},
22+
{
23+
'backendRouterId': 1234567,
24+
'backendRouterName': 'bcr01a.wdc07',
25+
'datacenterId': 2017603,
26+
'datacenterLongName': 'Washington 7',
27+
'datacenterName': 'wdc07',
28+
'frontendRouterId': 258741369,
29+
'frontendRouterName': 'fcr01a.wdc07',
30+
'name': 'TEST00.pod2'
2131
}
2232
]

SoftLayer/fixtures/SoftLayer_Product_Order.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,22 @@
284284
}
285285
}
286286
]}
287+
288+
vlan_placeOrder = {"orderDate": "2021-06-02 15:23:47",
289+
"orderId": 123456,
290+
"orderDetails": {
291+
"orderContainers": [{
292+
"name": "test"}]},
293+
"prices": [{
294+
"id": 2018,
295+
"itemId": 1071,
296+
"categories": [{
297+
"categoryCode": "network_vlan",
298+
"id": 113,
299+
"name": "Network Vlan"}],
300+
"item": {
301+
"capacity": "0",
302+
"description": "Public Network Vlan",
303+
"id": 1071,
304+
"keyName": "PUBLIC_NETWORK_VLAN"}}
305+
]}

SoftLayer/fixtures/SoftLayer_Product_Package.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,3 +2081,66 @@
20812081
"sort": 0,
20822082
}
20832083
]}]
2084+
2085+
getItemsVLAN = [{
2086+
"description": "Private Network Vlan",
2087+
"id": 1072,
2088+
"itemTaxCategoryId": 166,
2089+
"keyName": "PRIVATE_NETWORK_VLAN",
2090+
"itemCategory": {
2091+
"categoryCode": "network_vlan",
2092+
"id": 113,
2093+
"name": "Network Vlan"},
2094+
"prices": [{
2095+
"id": 203707,
2096+
"itemId": 1072,
2097+
"laborFee": "0",
2098+
"locationGroupId": 505,
2099+
"oneTimeFee": "0",
2100+
"recurringFee": "0",
2101+
"setupFee": "0",
2102+
"sort": 10,
2103+
},
2104+
{
2105+
"id": 203727,
2106+
"itemId": 1072,
2107+
"laborFee": "0",
2108+
"locationGroupId": 545,
2109+
"oneTimeFee": "0",
2110+
"recurringFee": "0",
2111+
"setupFee": "0",
2112+
"sort": 10,
2113+
}]
2114+
}, {
2115+
"description": "Public Network Vlan",
2116+
"id": 1071,
2117+
"itemTaxCategoryId": 166,
2118+
"keyName": "PUBLIC_NETWORK_VLAN",
2119+
"units": "N/A",
2120+
"itemCategory": {
2121+
"categoryCode": "network_vlan",
2122+
"id": 113,
2123+
"name": "Network Vlan",
2124+
},
2125+
"prices": [{
2126+
"id": 203637,
2127+
"itemId": 1071,
2128+
"laborFee": "0",
2129+
"locationGroupId": 509,
2130+
"oneTimeFee": "0",
2131+
"recurringFee": "0",
2132+
"setupFee": "0",
2133+
"sort": 10,
2134+
},
2135+
{
2136+
"id": 203667,
2137+
"itemId": 1071,
2138+
"laborFee": "0",
2139+
"locationGroupId": 545,
2140+
"oneTimeFee": "0",
2141+
"recurringFee": "0",
2142+
"setupFee": "0",
2143+
"sort": 10,
2144+
}]
2145+
}
2146+
]

SoftLayer/managers/network.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,3 +778,14 @@ def cancel_item(self, identifier, cancel_immediately,
778778
reason_cancel,
779779
customer_note,
780780
id=identifier)
781+
782+
def get_pods(self, datacenter=None):
783+
"""Calls SoftLayer_Network_Pod::getAllObjects()
784+
785+
returns list of all network pods and their routers.
786+
"""
787+
_filter = None
788+
if datacenter:
789+
_filter = {"datacenterName": {"operation": datacenter}}
790+
791+
return self.client.call('SoftLayer_Network_Pod', 'getAllObjects', filter=_filter)

SoftLayer/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def build_filter_orderby(orderby):
228228
for keyword in reverse_filter:
229229
_aux_filter = {}
230230
if '=' in keyword:
231-
_aux_filter[str(keyword).split('=', maxsplit=1)[0]] = query_filter_orderby(str(keyword).split('=')[1])
231+
_aux_filter[str(keyword).split('=', maxsplit=1)[0]] = query_filter_orderby(str(keyword).split('=')[1])
232232
_filters = _aux_filter
233233
elif keyword == list(reverse_filter)[0]:
234234
_aux_filter[keyword] = query_filter_orderby('DESC')

docs/cli/vlan.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
VLANs
44
=====
55

6+
.. click:: SoftLayer.CLI.vlan.create:cli
7+
:prog: vlan create
8+
:show-nested:
9+
610
.. click:: SoftLayer.CLI.vlan.detail:cli
711
:prog: vlan detail
812
:show-nested:

tests/CLI/modules/vlan_tests.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
55
:license: MIT, see LICENSE for more details.
66
"""
7+
import json
78
from unittest import mock as mock
89

10+
from SoftLayer.fixtures import SoftLayer_Product_Order
11+
from SoftLayer.fixtures import SoftLayer_Product_Package
912
from SoftLayer import testing
1013

1114

@@ -136,6 +139,42 @@ def test_vlan_list(self):
136139
self.assert_no_fail(result)
137140
self.assert_called_with('SoftLayer_Account', 'getNetworkVlans')
138141

142+
def test_create_vlan(self):
143+
_mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
144+
_mock.return_value = SoftLayer_Product_Package.getItemsVLAN
145+
146+
order_mock = self.set_mock('SoftLayer_Product_Order', 'placeOrder')
147+
order_mock.return_value = SoftLayer_Product_Order.vlan_placeOrder
148+
149+
result = self.run_command(['vlan', 'create',
150+
'--name', 'test',
151+
'-d TEST00',
152+
'--network', 'public',
153+
'--billing', 'hourly'
154+
])
155+
156+
self.assert_no_fail(result)
157+
self.assertEqual(json.loads(result.output),
158+
{'id': 123456, 'created': '2021-06-02 15:23:47', 'name': 'test'})
159+
160+
def test_create_vlan_pod(self):
161+
_mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
162+
_mock.return_value = SoftLayer_Product_Package.getItemsVLAN
163+
164+
order_mock = self.set_mock('SoftLayer_Product_Order', 'placeOrder')
165+
order_mock.return_value = SoftLayer_Product_Order.vlan_placeOrder
166+
167+
result = self.run_command(['vlan', 'create',
168+
'--name', 'test',
169+
'-p', 'TEST00.pod2',
170+
'--network', 'public',
171+
'--billing', 'hourly'
172+
])
173+
174+
self.assert_no_fail(result)
175+
self.assertEqual(json.loads(result.output),
176+
{'id': 123456, 'created': '2021-06-02 15:23:47', 'name': 'test'})
177+
139178
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
140179
def test_vlan_cancel(self, confirm_mock):
141180
confirm_mock.return_value = True

tests/managers/network_tests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,7 @@ def test_vlan_edit(self):
624624

625625
self.network.edit(vlan_id, name, note, tags)
626626
self.assert_called_with('SoftLayer_Network_Vlan', 'editObject')
627+
628+
def test_get_all_pods(self):
629+
self.network.get_pods()
630+
self.assert_called_with('SoftLayer_Network_Pod', 'getAllObjects')

0 commit comments

Comments
 (0)