Skip to content

Commit b2f1af6

Browse files
author
Fernando Ojeda
committed
Refactor network components method.
1 parent 6d354ce commit b2f1af6

File tree

2 files changed

+25
-31
lines changed

2 files changed

+25
-31
lines changed

SoftLayer/managers/vs.py

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

536-
self.get_network_components(data, kwargs, private_subnet, private_vlan, public_subnet, public_vlan)
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)
537541

538542
if public_security_groups:
539543
secgroups = [{'securityGroup': {'id': int(sg)}}
@@ -576,35 +580,19 @@ def _generate_create_dict(
576580

577581
return data
578582

579-
def get_network_components(self, data, kwargs, private_subnet, private_vlan, public_subnet, public_vlan):
580-
"""Get the network components structure.
581-
582-
:param data: Array variable to add the network structure.
583-
:param kwargs: Vs item list.
584-
:param int private_subnet: Private subnet id.
585-
:param int private_vlan: Private vlan id.
586-
:param int public_subnet: Public subnet id.
587-
:param int public_vlan: Public vlan id.
588-
"""
589-
if kwargs.get('private_router') or kwargs.get('public_router'):
590-
if private_vlan or public_vlan or private_subnet or public_subnet:
591-
raise exceptions.SoftLayerError("You have to select network vlan or network vlan with a subnet or "
592-
"only router, not all options")
593-
network_components = self._create_network_components(public_vlan, private_vlan,
594-
private_subnet, public_subnet,
595-
kwargs.get('private_router'),
596-
kwargs.get('public_router'))
597-
data.update(network_components)
598-
if private_vlan or public_vlan or private_subnet or public_subnet:
599-
network_components = self._create_network_components(public_vlan, private_vlan,
600-
private_subnet, public_subnet)
601-
data.update(network_components)
602-
603583
def _create_network_components(
604584
self, public_vlan=None, private_vlan=None,
605585
private_subnet=None, public_subnet=None,
606586
private_router=None, public_router=None):
607587
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)}}
608596
if private_vlan:
609597
parameters['primaryBackendNetworkComponent'] = {"networkVlan": {"id": int(private_vlan)}}
610598
if public_vlan:
@@ -620,12 +608,6 @@ def _create_network_components(
620608

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

623-
if private_router:
624-
parameters['primaryBackendNetworkComponent'] = {"router": {"id": int(private_router)}}
625-
626-
if public_router:
627-
parameters['primaryNetworkComponent'] = {"router": {"id": int(public_router)}}
628-
629611
return parameters
630612

631613
@retry(logger=LOGGER)

tests/managers/vs/vs_tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,18 @@ def test_create_network_components_by_routers(self):
641641

642642
self.assertEqual(data, assert_data)
643643

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+
644656
def test_create_network_components_vlan_subnet_private(self):
645657
data = self.vs._create_network_components(
646658
private_vlan=1,

0 commit comments

Comments
 (0)