Skip to content

Commit a17430e

Browse files
Merge pull request #2178 from allmightyspiff/issues2176
Reverted 'globalip assign' syntax
2 parents 526b81d + ec89f09 commit a17430e

File tree

8 files changed

+87
-32
lines changed

8 files changed

+87
-32
lines changed

SoftLayer/CLI/dns/zone_delete.py

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def cli(env, zone):
1717
"""Delete zone.
1818
1919
Example::
20+
2021
slcli dns zone-delete ibm.com
2122
This command deletes a zone that is named ibm.com
2223
"""

SoftLayer/CLI/formatting.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ def confirm(prompt_str, default=False):
254254
def no_going_back(confirmation):
255255
"""Show a confirmation to a user.
256256
257-
:param confirmation str: the string the user has to enter in order to
258-
confirm their action.
257+
:param confirmation str: the string the user has to enter in order to confirm their action.
259258
"""
260259
if not confirmation:
261260
confirmation = 'yes'

SoftLayer/CLI/globalip/assign.py

+35-11
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,51 @@
55

66
import SoftLayer
77
from SoftLayer.CLI import environment
8+
from SoftLayer.CLI import helpers
89

9-
target_types = {'vlan': 'SoftLayer_Network_Vlan',
10-
'ip': 'SoftLayer_Network_Subnet_IpAddress',
11-
'hardware': 'SoftLayer_Hardware_Server',
12-
'vsi': 'SoftLayer_Virtual_Guest'}
10+
11+
# pylint: disable=unused-argument
12+
def targetipcallback(ctx, param, value):
13+
"""This is here to allow for using --target-id in some cases. Takes the first value and returns it"""
14+
if value:
15+
return value[0]
16+
return value
1317

1418

1519
@click.command(cls=SoftLayer.CLI.command.SLCommand, epilog="More information about types and identifiers "
1620
"on https://sldn.softlayer.com/reference/services/SoftLayer_Network_Subnet/route/")
17-
@click.argument('identifier')
21+
@click.argument('globalip')
22+
@click.argument('targetip', nargs=-1, callback=targetipcallback)
1823
@click.option('--target', type=click.Choice(['vlan', 'ip', 'hardware', 'vsi']),
1924
help='choose the type. vlan, ip, hardware, vsi')
20-
@click.option('--target-id', help='The identifier for the destination resource to route this subnet to. ')
25+
@click.option('--target-id', help='The identifier for the destination resource to route this subnet to.')
2126
@environment.pass_env
22-
def cli(env, identifier, target, target_id):
23-
"""Assigns the subnet to a target.
27+
def cli(env, globalip, targetip, target, target_id):
28+
"""Assigns the GLOBALIP to TARGETIP.
2429
30+
GLOBALIP should be either the Global IP address, or the SoftLayer_Network_Subnet_IpAddress_Global id
31+
See `slcli globalip list`
32+
TARGETIP should be either the target IP address, or the SoftLayer_Network_Subnet_IpAddress id
33+
See `slcli subnet list`
2534
Example::
35+
2636
slcli globalip assign 12345678 9.111.123.456
27-
This command assigns IP address with ID 12345678 to a target device whose IP address is 9.111.123.456
28-
"""
37+
This command assigns Global IP address with ID 12345678 to a target device whose IP address is 9.111.123.456
2938
39+
slcli globalip assign 123.4.5.6 6.5.4.123
40+
Global IPs can be specified by their IP address
41+
"""
3042
mgr = SoftLayer.NetworkManager(env.client)
31-
mgr.route(identifier, target_types.get(target), target_id)
43+
# Find SoftLayer_Network_Subnet_IpAddress_Global::id
44+
global_ip_id = helpers.resolve_id(mgr.resolve_global_ip_ids, globalip, name='Global IP')
45+
46+
# Find Global IPs SoftLayer_Network_Subnet::id
47+
mask = "mask[id,ipAddress[subnetId]]"
48+
subnet = env.client.call('SoftLayer_Network_Subnet_IpAddress_Global', 'getObject', id=global_ip_id, mask=mask)
49+
subnet_id = subnet.get('ipAddress', {}).get('subnetId')
50+
51+
# For backwards compatibility
52+
if target_id:
53+
targetip = target_id
54+
55+
mgr.route(subnet_id, 'SoftLayer_Network_Subnet_IpAddress', targetip)

SoftLayer/CLI/globalip/cancel.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ def cli(env, identifier, force):
1818
"""Cancel global IP.
1919
2020
Example::
21+
2122
slcli globalip cancel 12345
2223
"""
2324

2425
mgr = SoftLayer.NetworkManager(env.client)
25-
global_ip_id = helpers.resolve_id(mgr.resolve_global_ip_ids, identifier,
26-
name='global ip')
26+
global_ip_id = helpers.resolve_id(mgr.resolve_global_ip_ids, identifier, name='global ip')
2727

2828
if not force:
2929
if not (env.skip_confirmations or

SoftLayer/CLI/globalip/unassign.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,21 @@
1212
@click.argument('identifier')
1313
@environment.pass_env
1414
def cli(env, identifier):
15-
"""Unassigns a global IP from a target."""
15+
"""Unroutes IDENTIFIER
16+
17+
IDENTIFIER should be either the Global IP address, or the SoftLayer_Network_Subnet_IpAddress_Global id
18+
Example::
19+
20+
slcli globalip unassign 123456
21+
22+
slcli globalip unassign 123.43.22.11
23+
"""
1624

1725
mgr = SoftLayer.NetworkManager(env.client)
18-
global_ip_id = helpers.resolve_id(mgr.resolve_global_ip_ids, identifier,
19-
name='global ip')
20-
mgr.unassign_global_ip(global_ip_id)
26+
global_ip_id = helpers.resolve_id(mgr.resolve_global_ip_ids, identifier, name='global ip')
27+
28+
# Find Global IPs SoftLayer_Network_Subnet::id
29+
mask = "mask[id,ipAddress[subnetId]]"
30+
subnet = env.client.call('SoftLayer_Network_Subnet_IpAddress_Global', 'getObject', id=global_ip_id, mask=mask)
31+
subnet_id = subnet.get('ipAddress', {}).get('subnetId')
32+
mgr.clear_route(subnet_id)
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
route = True
22
unroute = True
3-
getObject = {'id': 1234, 'billingItem': {'id': 1234}}
3+
getObject = {'id': 1234, 'billingItem': {'id': 1234}, 'ipAddress': {'subnetId': 9988}}

SoftLayer/managers/network.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -841,12 +841,12 @@ def get_closed_pods(self):
841841
def route(self, subnet_id, type_serv, target):
842842
"""Assigns a subnet to a specified target.
843843
844-
:param int subnet_id: The ID of the global IP being assigned
844+
https://sldn.softlayer.com/reference/services/SoftLayer_Network_Subnet/route/
845+
:param int subnet_id: The ID of the SoftLayer_Network_Subnet_IpAddress being routed
845846
:param string type_serv: The type service to assign
846847
:param string target: The instance to assign
847848
"""
848-
return self.client.call('SoftLayer_Network_Subnet', 'route',
849-
type_serv, target, id=subnet_id, )
849+
return self.client.call('SoftLayer_Network_Subnet', 'route', type_serv, target, id=subnet_id, )
850850

851851
def get_datacenter(self, _filter=None, datacenter=None):
852852
"""Calls SoftLayer_Location::getDatacenters()

tests/CLI/modules/globalip_tests.py

+28-9
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,9 @@
1212
import json
1313

1414

15-
class DnsTests(testing.TestCase):
15+
class GlobalIpTests(testing.TestCase):
1616

17-
def test_ip_assign(self):
18-
result = self.run_command(['globalip', 'assign', '1'])
19-
20-
self.assert_no_fail(result)
21-
self.assertEqual(result.output, "")
22-
23-
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
17+
@mock.patch('SoftLayer.CLI.formatting.confirm')
2418
def test_ip_cancel(self, no_going_back_mock):
2519
# Test using --really flag
2620
result = self.run_command(['--really', 'globalip', 'cancel', '1'])
@@ -39,7 +33,7 @@ def test_ip_cancel(self, no_going_back_mock):
3933
no_going_back_mock.return_value = False
4034
result = self.run_command(['globalip', 'cancel', '1'])
4135

42-
self.assertEqual(result.exit_code, 0)
36+
self.assertEqual(result.exit_code, 2)
4337

4438
def test_ip_list(self):
4539
result = self.run_command(['globalip', 'list', '--ip-version=v4'])
@@ -84,6 +78,31 @@ def test_ip_unassign(self):
8478
result = self.run_command(['globalip', 'unassign', '1'])
8579
self.assert_no_fail(result)
8680
self.assertEqual(result.output, "")
81+
self.assert_called_with('SoftLayer_Network_Subnet', 'clearRoute', identifier=9988)
82+
83+
def test_ip_assign(self):
84+
result = self.run_command(['globalip', 'assign', '1', '999'])
85+
self.assert_no_fail(result)
86+
self.assertEqual(result.output, "")
87+
service = 'SoftLayer_Network_Subnet_IpAddress'
88+
self.assert_called_with('SoftLayer_Network_Subnet', 'route', identifier=9988, args=(service, '999'))
89+
90+
def test_ip_assign_target(self):
91+
result = self.run_command(['globalip', 'assign', '1', '--target-id=8123'])
92+
self.assert_no_fail(result)
93+
self.assertEqual(result.output, "")
94+
service = 'SoftLayer_Network_Subnet_IpAddress'
95+
self.assert_called_with('SoftLayer_Network_Subnet', 'route', identifier=9988, args=(service, '8123'))
96+
97+
def test_ip_assign_ip(self):
98+
mock_api = self.set_mock('SoftLayer_Account', 'getGlobalIpRecords')
99+
mock_api.return_value = [{"id": 112233}]
100+
result = self.run_command(['globalip', 'assign', '192.168.1.1', '1.2.3.4'])
101+
self.assert_no_fail(result)
102+
self.assertEqual(result.output, "")
103+
service = 'SoftLayer_Network_Subnet_IpAddress'
104+
self.assert_called_with(f"{service}_Global", "getObject", identifier=112233)
105+
self.assert_called_with('SoftLayer_Network_Subnet', 'route', identifier=9988, args=(service, '1.2.3.4'))
87106

88107
def test_ip_cancel_force(self):
89108
result = self.run_command(['globalip', 'cancel', '1', '--force'])

0 commit comments

Comments
 (0)