Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

De-duplicate networks in provisioning request #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions jumpgate/compute/drivers/sl/servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,18 +287,24 @@ def _handle_network(self, payload, client, networks):
4) --net-id=<private id> --net-id=<public id>
"""

if len(networks) > 2:
# De-duplicate requested networks
unique_nets = []
unique_nets = [unique_nets.append(n) or n
for n in networks
if n not in unique_nets]

if len(unique_nets) > 2:
raise Exception('Too many net-id arguments')

# support cases of the string 'public' or 'private'
if networks[0]['uuid'] == 'public':
if len(networks) > 1:
if unique_nets[0]['uuid'] == 'public':
if len(unique_nets) > 1:
raise Exception('Too many net-id arguments. '
'Please indicate only "public" or "private"')
payload['private'] = False
return
elif networks[0]['uuid'] == 'private':
if len(networks) > 1:
elif unique_nets[0]['uuid'] == 'private':
if len(unique_nets) > 1:
raise Exception('Too many net-id arguments. '
'Please indicate only "public" or "private"')
payload['private'] = True
Expand All @@ -307,32 +313,33 @@ def _handle_network(self, payload, client, networks):
private_network_only = True
try:
_filter = {
'networkVlans': {'id': {'operation': int(networks[0]['uuid'])}}
'networkVlans': {'id': {'operation':
int(unique_nets[0]['uuid'])}}
}
except Exception:
raise ValueError('Invalid id format')

priv_id_valid = (
client['Account'].getPrivateNetworkVlans(filter=_filter))
if priv_id_valid:
payload['private_vlan'] = int(networks[0]['uuid'])
payload['private_vlan'] = int(unique_nets[0]['uuid'])
else:
raise Exception('Private vlan must be specified first '
'or is invalid')

# if there is another net-id, then it should be a public network
if len(networks) == 2:
if len(unique_nets) == 2:
try:
_filter = {
'networkVlans': {'id': {'operation':
int(networks[1]['uuid'])}}
int(unique_nets[1]['uuid'])}}
}
except Exception:
raise ValueError('Invalid id format')
pub_id_valid = (
client['Account'].getPublicNetworkVlans(filter=_filter))
if pub_id_valid:
payload['public_vlan'] = int(networks[1]['uuid'])
payload['public_vlan'] = int(unique_nets[1]['uuid'])
private_network_only = False
else:
raise Exception('Public vlan must be specified second '
Expand Down
18 changes: 18 additions & 0 deletions tests/jumpgate-tests/compute/test_servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,24 @@ def test_handle_network_valid_ids(self):
self.assertEqual(self.payload['public_vlan'], 489588)
self.assertEqual(self.payload['private'], False)

def test_handle_network_valid_duplicated_public_private_ids(self):
self.instance._handle_network(self.payload, self.client,
[{'uuid': 489586}, {'uuid': 489588},
{'uuid': 489588}, {'uuid': 489586}])
self.assertEqual(self.payload['public_vlan'], 489588)
self.assertEqual(self.payload['private_vlan'], 489586)
self.assertEqual(self.payload['private'], False)

def test_handle_network_valid_duplicated_ids(self):
self.client['Account'].getPrivateNetworkVlans.return_value = [489586]
self.client['Account'].getPublicNetworkVlans.return_value = [489588]
self.instance._handle_network(self.payload, self.client,
[{'uuid': 489586}, {'uuid': 489588},
{'uuid': 489588}, {'uuid': 489586}])
self.assertEqual(self.payload['private_vlan'], 489586)
self.assertEqual(self.payload['public_vlan'], 489588)
self.assertEqual(self.payload['private'], False)

def test_handle_network_valid_public(self):
self.instance._handle_network(self.payload, self.client,
[{'uuid': 'public'}])
Expand Down