From 91e8750be81f5dc68c00d3867c7fad654aa9c4cb Mon Sep 17 00:00:00 2001 From: Zhi Yan Liu Date: Mon, 17 Nov 2014 17:29:35 +0800 Subject: [PATCH] De-duplicate networks in provisioning request In some particular cases end user could provide duplicated networks in provisioning request by horizion with softlayer network environment, the change de-duplicate these duplicated network ids and make softlayer provision the server(s) with proper network(s). Signed-off-by: Zhi Yan Liu --- jumpgate/compute/drivers/sl/servers.py | 27 ++++++++++++-------- tests/jumpgate-tests/compute/test_servers.py | 18 +++++++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/jumpgate/compute/drivers/sl/servers.py b/jumpgate/compute/drivers/sl/servers.py index bd629d2..58291e9 100644 --- a/jumpgate/compute/drivers/sl/servers.py +++ b/jumpgate/compute/drivers/sl/servers.py @@ -287,18 +287,24 @@ def _handle_network(self, payload, client, networks): 4) --net-id= --net-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 @@ -307,7 +313,8 @@ 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') @@ -315,24 +322,24 @@ def _handle_network(self, payload, client, networks): 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 ' diff --git a/tests/jumpgate-tests/compute/test_servers.py b/tests/jumpgate-tests/compute/test_servers.py index 80955f2..a11900b 100644 --- a/tests/jumpgate-tests/compute/test_servers.py +++ b/tests/jumpgate-tests/compute/test_servers.py @@ -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'}])