Skip to content

Commit 0558eab

Browse files
Merge pull request #1414 from FernandoOjeda/ft/vs_create_by_router
Add slcli vs create by router data.
2 parents 8bdadbb + b2f1af6 commit 0558eab

File tree

4 files changed

+136
-6
lines changed

4 files changed

+136
-6
lines changed

SoftLayer/CLI/virt/create.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ def _parse_create_args(client, args):
9595
"private_vlan": args.get('vlan_private', None),
9696
"public_subnet": args.get('subnet_public', None),
9797
"private_subnet": args.get('subnet_private', None),
98+
"public_router": args.get('router_public', None),
99+
"private_router": args.get('router_private', None),
98100
}
99101

100102
# The primary disk is included in the flavor and the local_disk flag is not needed
@@ -192,6 +194,10 @@ def _parse_create_args(client, args):
192194
help="The ID of the public SUBNET on which you want the virtual server placed")
193195
@click.option('--subnet-private', type=click.INT,
194196
help="The ID of the private SUBNET on which you want the virtual server placed")
197+
@click.option('--router-public', type=click.INT,
198+
help="The ID of the public ROUTER on which you want the virtual server placed")
199+
@click.option('--router-private', type=click.INT,
200+
help="The ID of the private ROUTER on which you want the virtual server placed")
195201
@helpers.multi_option('--public-security-group', '-S',
196202
help=('Security group ID to associate with the public interface'))
197203
@helpers.multi_option('--private-security-group', '-s',

SoftLayer/managers/vs.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,11 @@ def _generate_create_dict(
533533
if datacenter:
534534
data["datacenter"] = {"name": datacenter}
535535

536-
if private_vlan or public_vlan or private_subnet or public_subnet:
537-
network_components = self._create_network_components(public_vlan, private_vlan,
538-
private_subnet, public_subnet)
539-
data.update(network_components)
536+
network_components = self._create_network_components(public_vlan, private_vlan,
537+
private_subnet, public_subnet,
538+
kwargs.get('private_router'),
539+
kwargs.get('public_router'))
540+
data.update(network_components)
540541

541542
if public_security_groups:
542543
secgroups = [{'securityGroup': {'id': int(sg)}}
@@ -581,8 +582,17 @@ def _generate_create_dict(
581582

582583
def _create_network_components(
583584
self, public_vlan=None, private_vlan=None,
584-
private_subnet=None, public_subnet=None):
585+
private_subnet=None, public_subnet=None,
586+
private_router=None, public_router=None):
585587
parameters = {}
588+
if any([private_router, public_router]) and any([private_vlan, public_vlan, private_subnet, public_subnet]):
589+
raise exceptions.SoftLayerError("You have to select network vlan or network vlan with a subnet or "
590+
"only router, not all options")
591+
592+
if private_router:
593+
parameters['primaryBackendNetworkComponent'] = {"router": {"id": int(private_router)}}
594+
if public_router:
595+
parameters['primaryNetworkComponent'] = {"router": {"id": int(public_router)}}
586596
if private_vlan:
587597
parameters['primaryBackendNetworkComponent'] = {"networkVlan": {"id": int(private_vlan)}}
588598
if public_vlan:
@@ -685,7 +695,21 @@ def verify_create_instance(self, **kwargs):
685695
kwargs.pop('tags', None)
686696
create_options = self._generate_create_dict(**kwargs)
687697
template = self.guest.generateOrderTemplate(create_options)
688-
if 'private_subnet' in kwargs or 'public_subnet' in kwargs:
698+
if kwargs.get('public_router') or kwargs.get('private_router'):
699+
if kwargs.get('private_vlan') or kwargs.get('public_vlan') or kwargs.get('private_subnet') \
700+
or kwargs.get('public_subnet'):
701+
raise exceptions.SoftLayerError("You have to select network vlan or network vlan with a subnet or "
702+
"only router, not all options")
703+
vsi = template['virtualGuests'][0]
704+
network_components = self._create_network_components(kwargs.get('public_vlan', None),
705+
kwargs.get('private_vlan', None),
706+
kwargs.get('private_subnet', None),
707+
kwargs.get('public_subnet', None),
708+
kwargs.get('private_router', None),
709+
kwargs.get('public_router', None))
710+
vsi.update(network_components)
711+
712+
if kwargs.get('private_subnet') or kwargs.get('public_subnet'):
689713
vsi = template['virtualGuests'][0]
690714
network_components = self._create_network_components(kwargs.get('public_vlan', None),
691715
kwargs.get('private_vlan', None),

tests/CLI/modules/vs/vs_create_tests.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,49 @@ def test_create_vlan_subnet(self, confirm_mock):
9393

9494
self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate', args=args)
9595

96+
@mock.patch('SoftLayer.CLI.formatting.confirm')
97+
def test_create_by_router(self, confirm_mock):
98+
confirm_mock.return_value = True
99+
100+
result = self.run_command(['vs', 'create',
101+
'--cpu=2',
102+
'--domain=example.com',
103+
'--hostname=host',
104+
'--os=UBUNTU_LATEST',
105+
'--memory=1',
106+
'--billing=hourly',
107+
'--datacenter=dal05',
108+
'--router-private=577940',
109+
'--router-public=1639255',
110+
'--tag=dev',
111+
'--tag=green'])
112+
113+
self.assert_no_fail(result)
114+
self.assert_called_with('SoftLayer_Product_Order', 'placeOrder')
115+
args = ({
116+
'startCpus': 2,
117+
'maxMemory': 1024,
118+
'hostname': 'host',
119+
'domain': 'example.com',
120+
'localDiskFlag': True,
121+
'hourlyBillingFlag': True,
122+
'supplementalCreateObjectOptions': {'bootMode': None},
123+
'operatingSystemReferenceCode': 'UBUNTU_LATEST',
124+
'datacenter': {'name': 'dal05'},
125+
'primaryBackendNetworkComponent': {
126+
'router': {
127+
'id': 577940
128+
}
129+
},
130+
'primaryNetworkComponent': {
131+
'router': {
132+
'id': 1639255
133+
}
134+
}
135+
},)
136+
137+
self.assert_called_with('SoftLayer_Virtual_Guest', 'generateOrderTemplate', args=args)
138+
96139
@mock.patch('SoftLayer.CLI.formatting.confirm')
97140
def test_create_with_wait_ready(self, confirm_mock):
98141
mock = self.set_mock('SoftLayer_Virtual_Guest', 'getObject')

tests/managers/vs/vs_tests.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,38 @@ def test_generate_private_vlan(self):
557557

558558
self.assertEqual(data, assert_data)
559559

560+
def test_generate_by_router_and_vlan(self):
561+
actual = self.assertRaises(
562+
exceptions.SoftLayerError,
563+
self.vs._generate_create_dict,
564+
cpus=1,
565+
memory=1,
566+
hostname='test',
567+
domain='example.com',
568+
os_code="STRING",
569+
private_router=1,
570+
private_vlan=1
571+
)
572+
573+
self.assertEqual(str(actual), "You have to select network vlan or network vlan with a subnet or only router, "
574+
"not all options")
575+
576+
def test_generate_by_router_and_subnet(self):
577+
actual = self.assertRaises(
578+
exceptions.SoftLayerError,
579+
self.vs._generate_create_dict,
580+
cpus=1,
581+
memory=1,
582+
hostname='test',
583+
domain='example.com',
584+
os_code="STRING",
585+
private_router=1,
586+
private_subnet=1
587+
)
588+
589+
self.assertEqual(str(actual), "You have to select network vlan or network vlan with a subnet or only router, "
590+
"not all options")
591+
560592
def test_generate_sec_group(self):
561593
data = self.vs._generate_create_dict(
562594
cpus=1,
@@ -596,6 +628,31 @@ def test_create_network_components_vlan_subnet_private_vlan_subnet_public(self):
596628

597629
self.assertEqual(data, assert_data)
598630

631+
def test_create_network_components_by_routers(self):
632+
data = self.vs._create_network_components(
633+
private_router=1,
634+
public_router=1
635+
)
636+
637+
assert_data = {
638+
'primaryBackendNetworkComponent': {'router': {'id': 1}},
639+
'primaryNetworkComponent': {'router': {'id': 1}},
640+
}
641+
642+
self.assertEqual(data, assert_data)
643+
644+
def test_create_network_components_by_routers_and_vlan(self):
645+
actual = self.assertRaises(
646+
exceptions.SoftLayerError,
647+
self.vs._create_network_components,
648+
private_router=1,
649+
public_router=1,
650+
private_vlan=1
651+
)
652+
653+
self.assertEqual(str(actual), "You have to select network vlan or network vlan with a subnet or only router, "
654+
"not all options")
655+
599656
def test_create_network_components_vlan_subnet_private(self):
600657
data = self.vs._create_network_components(
601658
private_vlan=1,

0 commit comments

Comments
 (0)