Skip to content

Commit 3bebf3a

Browse files
Merge pull request #1149 from ATGE/issue1139
#1139 create static subnet
2 parents cdbae97 + b3f52bc commit 3bebf3a

File tree

4 files changed

+80
-23
lines changed

4 files changed

+80
-23
lines changed

SoftLayer/CLI/subnet/create.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,33 @@
1010

1111

1212
@click.command(short_help="Add a new subnet to your account")
13-
@click.argument('network', type=click.Choice(['public', 'private']))
13+
@click.argument('network', type=click.Choice(['static', 'public', 'private']))
1414
@click.argument('quantity', type=click.INT)
15-
@click.argument('vlan-id')
15+
@click.argument('endpoint-id', type=click.INT)
1616
@click.option('--ipv6', '--v6', is_flag=True, help="Order IPv6 Addresses")
1717
@click.option('--test',
1818
is_flag=True,
1919
help="Do not order the subnet; just get a quote")
2020
@environment.pass_env
21-
def cli(env, network, quantity, vlan_id, ipv6, test):
21+
def cli(env, network, quantity, endpoint_id, ipv6, test):
2222
"""Add a new subnet to your account. Valid quantities vary by type.
2323
2424
\b
25-
Type - Valid Quantities (IPv4)
26-
public - 4, 8, 16, 32
27-
private - 4, 8, 16, 32, 64
25+
IPv4
26+
static - 1, 2, 4, 8, 16, 32, 64, 128, 256
27+
public - 4, 8, 16, 32, 64, 128, 256
28+
private - 4, 8, 16, 32, 64, 128, 256
2829
2930
\b
30-
Type - Valid Quantities (IPv6)
31+
IPv6
32+
static - 64
3133
public - 64
34+
35+
\b
36+
endpoint-id
37+
static - Network_Subnet_IpAddress identifier.
38+
public - Network_Vlan identifier
39+
private - Network_Vlan identifier
3240
"""
3341

3442
mgr = SoftLayer.NetworkManager(env.client)
@@ -43,9 +51,13 @@ def cli(env, network, quantity, vlan_id, ipv6, test):
4351
version = 6
4452

4553
try:
46-
result = mgr.add_subnet(network, quantity=quantity, vlan_id=vlan_id, version=version, test_order=test)
47-
except SoftLayer.SoftLayerAPIError:
48-
raise exceptions.CLIAbort('There is no price id for {} {} ipv{}'.format(quantity, network, version))
54+
result = mgr.add_subnet(network, quantity=quantity, endpoint_id=endpoint_id, version=version, test_order=test)
55+
56+
except SoftLayer.SoftLayerAPIError as error:
57+
raise exceptions.CLIAbort('Unable to order {} {} ipv{} , error: {}'.format(quantity,
58+
network,
59+
version,
60+
error.faultString))
4961

5062
table = formatting.Table(['Item', 'cost'])
5163
table.align['Item'] = 'r'

SoftLayer/managers/network.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ def add_securitygroup_rules(self, group_id, rules):
110110
raise TypeError("The rules provided must be a list of dictionaries")
111111
return self.security_group.addRules(rules, id=group_id)
112112

113-
def add_subnet(self, subnet_type, quantity=None, vlan_id=None, version=4,
113+
def add_subnet(self, subnet_type, quantity=None, endpoint_id=None, version=4,
114114
test_order=False):
115115
"""Orders a new subnet
116116
117-
:param str subnet_type: Type of subnet to add: private, public, global
117+
:param str subnet_type: Type of subnet to add: private, public, global,static
118118
:param int quantity: Number of IPs in the subnet
119-
:param int vlan_id: VLAN id for the subnet to be placed into
119+
:param int endpoint_id: id for the subnet to be placed into
120120
:param int version: 4 for IPv4, 6 for IPv6
121121
:param bool test_order: If true, this will only verify the order.
122122
"""
@@ -126,9 +126,11 @@ def add_subnet(self, subnet_type, quantity=None, vlan_id=None, version=4,
126126
if version == 4:
127127
if subnet_type == 'global':
128128
quantity = 0
129-
category = 'global_ipv4'
129+
category = "global_ipv4"
130130
elif subnet_type == 'public':
131-
category = 'sov_sec_ip_addresses_pub'
131+
category = "sov_sec_ip_addresses_pub"
132+
elif subnet_type == 'static':
133+
category = "static_sec_ip_addresses"
132134
else:
133135
category = 'static_ipv6_addresses'
134136
if subnet_type == 'global':
@@ -137,14 +139,17 @@ def add_subnet(self, subnet_type, quantity=None, vlan_id=None, version=4,
137139
desc = 'Global'
138140
elif subnet_type == 'public':
139141
desc = 'Portable'
142+
elif subnet_type == 'static':
143+
desc = 'Static'
140144

141145
# In the API, every non-server item is contained within package ID 0.
142146
# This means that we need to get all of the items and loop through them
143147
# looking for the items we need based upon the category, quantity, and
144148
# item description.
145149
price_id = None
146150
quantity_str = str(quantity)
147-
for item in package.getItems(id=0, mask='itemCategory'):
151+
package_items = package.getItems(id=0)
152+
for item in package_items:
148153
category_code = utils.lookup(item, 'itemCategory', 'categoryCode')
149154
if all([category_code == category,
150155
item.get('capacity') == quantity_str,
@@ -161,9 +166,10 @@ def add_subnet(self, subnet_type, quantity=None, vlan_id=None, version=4,
161166
# correct order container
162167
'complexType': 'SoftLayer_Container_Product_Order_Network_Subnet',
163168
}
164-
165-
if subnet_type != 'global':
166-
order['endPointVlanId'] = vlan_id
169+
if subnet_type == 'static':
170+
order['endPointIpAddressId'] = endpoint_id
171+
elif subnet_type != 'global' and subnet_type != 'static':
172+
order['endPointVlanId'] = endpoint_id
167173

168174
if test_order:
169175
return self.client['Product_Order'].verifyOrder(order)

tests/CLI/modules/subnet_tests.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,43 @@ def test_create_subnet_no_prices_found(self):
9494
result = self.run_command(['subnet', 'create', '--v6', 'public', '32', '12346', '--test'])
9595

9696
self.assertRaises(SoftLayer.SoftLayerAPIError, verify_mock)
97-
self.assertEqual(result.exception.message, 'There is no price id for 32 public ipv6')
97+
self.assertIn('Unable to order 32 public ipv6', result.exception.message, )
98+
99+
@mock.patch('SoftLayer.CLI.formatting.confirm')
100+
def test_create_subnet_static(self, confirm_mock):
101+
confirm_mock.return_value = True
102+
103+
item_mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
104+
item_mock.return_value = SoftLayer_Product_Package.getItems
105+
106+
place_mock = self.set_mock('SoftLayer_Product_Order', 'placeOrder')
107+
place_mock.return_value = SoftLayer_Product_Order.placeOrder
108+
109+
result = self.run_command(['subnet', 'create', 'static', '2', '12346'])
110+
self.assert_no_fail(result)
111+
112+
output = [
113+
{'Item': 'Total monthly cost', 'cost': '0.00'}
114+
]
115+
116+
self.assertEqual(output, json.loads(result.output))
117+
118+
@mock.patch('SoftLayer.CLI.formatting.confirm')
119+
def test_create_subnet_static_ipv6(self, confirm_mock):
120+
confirm_mock.return_value = True
121+
122+
item_mock = self.set_mock('SoftLayer_Product_Package', 'getItems')
123+
item_mock.return_value = SoftLayer_Product_Package.getItems
124+
125+
place_mock = self.set_mock('SoftLayer_Product_Order', 'verifyOrder')
126+
place_mock.return_value = SoftLayer_Product_Order.verifyOrder
127+
128+
result = self.run_command(['subnet', 'create', '--v6', 'static', '64', '12346', '--test'])
129+
self.assert_no_fail(result)
130+
131+
output = [
132+
{'Item': 'this is a thing', 'cost': '2.00'},
133+
{'Item': 'Total monthly cost', 'cost': '2.00'}
134+
]
135+
136+
self.assertEqual(output, json.loads(result.output))

tests/managers/network_tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ def test_add_subnet_for_ipv4(self):
8181
# Test a four public address IPv4 order
8282
result = self.network.add_subnet('public',
8383
quantity=4,
84-
vlan_id=1234,
84+
endpoint_id=1234,
8585
version=4,
8686
test_order=True)
8787

8888
self.assertEqual(fixtures.SoftLayer_Product_Order.verifyOrder, result)
8989

9090
result = self.network.add_subnet('public',
9191
quantity=4,
92-
vlan_id=1234,
92+
endpoint_id=1234,
9393
version=4,
9494
test_order=False)
9595

@@ -104,7 +104,7 @@ def test_add_subnet_for_ipv6(self):
104104
# Test a public IPv6 order
105105
result = self.network.add_subnet('public',
106106
quantity=64,
107-
vlan_id=45678,
107+
endpoint_id=45678,
108108
version=6,
109109
test_order=True)
110110

0 commit comments

Comments
 (0)