Skip to content

Commit 461cee4

Browse files
author
Fernando Ojeda
committed
Add slcli vs create by router data.
1 parent bc500bb commit 461cee4

File tree

4 files changed

+131
-2
lines changed

4 files changed

+131
-2
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: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ def _generate_create_dict(
470470
datacenter=None, os_code=None, image_id=None,
471471
dedicated=False, public_vlan=None, private_vlan=None,
472472
private_subnet=None, public_subnet=None,
473+
public_router=None, private_router=None,
473474
userdata=None, nic_speed=None, disks=None, post_uri=None,
474475
private=False, ssh_keys=None, public_security_groups=None,
475476
private_security_groups=None, boot_mode=None, transient=False, **kwargs):
@@ -533,6 +534,15 @@ def _generate_create_dict(
533534
if datacenter:
534535
data["datacenter"] = {"name": datacenter}
535536

537+
if private_router or public_router:
538+
if private_vlan or public_vlan or private_subnet or public_subnet:
539+
raise exceptions.SoftLayerError("You have to select network vlan or network vlan with a subnet or "
540+
"only router, not all options")
541+
network_components = self._create_network_components(public_vlan, private_vlan,
542+
private_subnet, public_subnet,
543+
private_router, public_router)
544+
data.update(network_components)
545+
536546
if private_vlan or public_vlan or private_subnet or public_subnet:
537547
network_components = self._create_network_components(public_vlan, private_vlan,
538548
private_subnet, public_subnet)
@@ -581,7 +591,8 @@ def _generate_create_dict(
581591

582592
def _create_network_components(
583593
self, public_vlan=None, private_vlan=None,
584-
private_subnet=None, public_subnet=None):
594+
private_subnet=None, public_subnet=None,
595+
private_router=None, public_router=None):
585596
parameters = {}
586597
if private_vlan:
587598
parameters['primaryBackendNetworkComponent'] = {"networkVlan": {"id": int(private_vlan)}}
@@ -598,6 +609,12 @@ def _create_network_components(
598609

599610
parameters['primaryBackendNetworkComponent']['networkVlan']['primarySubnet'] = {'id': int(private_subnet)}
600611

612+
if private_router:
613+
parameters['primaryBackendNetworkComponent'] = {"router": {"id": int(private_router)}}
614+
615+
if public_router:
616+
parameters['primaryNetworkComponent'] = {"router": {"id": int(public_router)}}
617+
601618
return parameters
602619

603620
@retry(logger=LOGGER)
@@ -685,14 +702,31 @@ def verify_create_instance(self, **kwargs):
685702
kwargs.pop('tags', None)
686703
create_options = self._generate_create_dict(**kwargs)
687704
template = self.guest.generateOrderTemplate(create_options)
688-
if 'private_subnet' in kwargs or 'public_subnet' in kwargs:
705+
if kwargs.get('public_router') or kwargs.get('private_router'):
706+
if kwargs.get('private_vlan') or kwargs.get('public_vlan') or kwargs.get('private_subnet') \
707+
or kwargs.get('public_subnet'):
708+
raise exceptions.SoftLayerError("You have to select network vlan or network vlan with a subnet or "
709+
"only router, not all options")
710+
vsi = template['virtualGuests'][0]
711+
network_components = self._create_network_components(kwargs.get('public_vlan', None),
712+
kwargs.get('private_vlan', None),
713+
kwargs.get('private_subnet', None),
714+
kwargs.get('public_subnet', None),
715+
kwargs.get('private_router', None),
716+
kwargs.get('public_router', None))
717+
vsi.update(network_components)
718+
719+
if kwargs.get('private_subnet') or kwargs.get('public_subnet'):
689720
vsi = template['virtualGuests'][0]
690721
network_components = self._create_network_components(kwargs.get('public_vlan', None),
691722
kwargs.get('private_vlan', None),
692723
kwargs.get('private_subnet', None),
693724
kwargs.get('public_subnet', None))
694725
vsi.update(network_components)
695726

727+
print("template")
728+
print(template)
729+
696730
return template
697731

698732
def create_instance(self, **kwargs):
@@ -1121,6 +1155,7 @@ def order_guest(self, guest_object, test=False):
11211155
if guest_object.get('placement_id'):
11221156
template['virtualGuests'][0]['placementGroupId'] = guest_object.get('placement_id')
11231157

1158+
print(template)
11241159
if test:
11251160
result = self.client.call('Product_Order', 'verifyOrder', template)
11261161
else:

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: 45 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,19 @@ 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+
599644
def test_create_network_components_vlan_subnet_private(self):
600645
data = self.vs._create_network_components(
601646
private_vlan=1,

0 commit comments

Comments
 (0)