From 9839310cda595041af9ff2cada78fc031aec0e59 Mon Sep 17 00:00:00 2001 From: spisarski Date: Thu, 20 Jul 2017 15:15:55 -0600 Subject: Created new exception classes for networks. Raising NetworkSettingsError in NetworkSettings, SubnetSettingsError in SubnetSettings and PortSettingsError in PortSettings instead of Exception. JIRA: SNAPS-134 Change-Id: I724d9e115e41767a9e3596d85db2a20ee8de5f73 Signed-off-by: spisarski --- snaps/openstack/create_network.py | 40 ++++++++++++++++++++------- snaps/openstack/tests/create_network_tests.py | 25 +++++++++-------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/snaps/openstack/create_network.py b/snaps/openstack/create_network.py index ad91a52..4f27eec 100644 --- a/snaps/openstack/create_network.py +++ b/snaps/openstack/create_network.py @@ -189,7 +189,8 @@ class NetworkSettings: SubnetSettings(**subnet_config['subnet'])) if not self.name or len(self.name) < 1: - raise Exception('Name required for networks') + raise NetworkSettingsError('Name required for networks') + raise NetworkSettingsError('Name required for networks') def get_project_id(self, os_creds): """ @@ -232,7 +233,7 @@ class NetworkSettings: if project_id: out['project_id'] = project_id else: - raise Exception( + raise NetworkSettingsError( 'Could not find project ID for project named - ' + self.project_name) if self.network_type: @@ -244,6 +245,12 @@ class NetworkSettings: return {'network': out} +class NetworkSettingsError(Exception): + """ + Exception to be thrown when networks settings attributes are incorrect + """ + + class SubnetSettings: """ Class representing a subnet configuration @@ -286,9 +293,10 @@ class SubnetSettings: dhcpv6-stateless, or slaac. :param ipv6_address_mode: A valid value is dhcpv6-stateful, dhcpv6-stateless, or slaac. - :raise: Exception when config does not have or cidr values are None + :raise: SubnetSettingsError when config does not have or cidr values + are None """ - self.cidr = kwargs['cidr'] + self.cidr = kwargs.get('cidr') if kwargs.get('ip_version'): self.ip_version = kwargs['ip_version'] else: @@ -314,7 +322,7 @@ class SubnetSettings: self.ipv6_address_mode = kwargs.get('ipv6_address_mode') if not self.name or not self.cidr: - raise Exception('Name and cidr required for subnets') + raise SubnetSettingsError('Name and cidr required for subnets') def dict_for_neutron(self, os_creds, network=None): """ @@ -344,7 +352,7 @@ class SubnetSettings: if project_id: out['project_id'] = project_id else: - raise Exception( + raise SubnetSettingsError( 'Could not find project ID for project named - ' + self.project_name) if self.start and self.end: @@ -368,6 +376,12 @@ class SubnetSettings: return out +class SubnetSettingsError(Exception): + """ + Exception to be thrown when subnet settings attributes are incorrect + """ + + class PortSettings: """ Class representing a port configuration @@ -435,7 +449,7 @@ class PortSettings: self.device_id = kwargs.get('device_id') if not self.name or not self.network_name: - raise Exception( + raise PortSettingsError( 'The attributes neutron, name, and network_name are required ' 'for PortSettings') @@ -456,7 +470,7 @@ class PortSettings: self.fixed_ips.append({'ip_address': ip_addr_dict['ip'], 'subnet_id': subnet.id}) else: - raise Exception( + raise PortSettingsError( 'Invalid port configuration, subnet does not exist ' 'with name - ' + ip_addr_dict['subnet_name']) @@ -487,7 +501,7 @@ class PortSettings: self.network_name, project_id) if not self.network: - raise Exception( + raise PortSettingsError( 'Cannot locate network with name - ' + self.network_name) out['network_id'] = self.network.id @@ -500,7 +514,7 @@ class PortSettings: if project_id: out['project_id'] = project_id else: - raise Exception( + raise PortSettingsError( 'Could not find project ID for project named - ' + self.project_name) if self.mac_address: @@ -535,3 +549,9 @@ class PortSettings: self.opt_name == other.opt_name and self.device_owner == other.device_owner and self.device_id == other.device_id) + + +class PortSettingsError(Exception): + """ + Exception to be thrown when port settings attributes are incorrect + """ diff --git a/snaps/openstack/tests/create_network_tests.py b/snaps/openstack/tests/create_network_tests.py index e22f8bf..322d383 100644 --- a/snaps/openstack/tests/create_network_tests.py +++ b/snaps/openstack/tests/create_network_tests.py @@ -17,7 +17,10 @@ import uuid from snaps.openstack import create_router from snaps.openstack.create_network import (OpenStackNetwork, NetworkSettings, - SubnetSettings, PortSettings) + SubnetSettings, PortSettings, + NetworkSettingsError, + SubnetSettingsError, + PortSettingsError) from snaps.openstack.tests import openstack_tests from snaps.openstack.tests.os_source_file_test import (OSIntegrationTestCase, OSComponentTestCase) @@ -33,11 +36,11 @@ class NetworkSettingsUnitTests(unittest.TestCase): """ def test_no_params(self): - with self.assertRaises(Exception): + with self.assertRaises(NetworkSettingsError): NetworkSettings() def test_empty_config(self): - with self.assertRaises(Exception): + with self.assertRaises(NetworkSettingsError): NetworkSettings(**dict()) def test_name_only(self): @@ -102,19 +105,19 @@ class SubnetSettingsUnitTests(unittest.TestCase): """ def test_no_params(self): - with self.assertRaises(Exception): + with self.assertRaises(SubnetSettingsError): SubnetSettings() def test_empty_config(self): - with self.assertRaises(Exception): + with self.assertRaises(SubnetSettingsError): SubnetSettings(**dict()) def test_name_only(self): - with self.assertRaises(Exception): + with self.assertRaises(SubnetSettingsError): SubnetSettings(name='foo') def test_config_with_name_only(self): - with self.assertRaises(Exception): + with self.assertRaises(SubnetSettingsError): SubnetSettings(**{'name': 'foo'}) def test_name_cidr_only(self): @@ -216,19 +219,19 @@ class PortSettingsUnitTests(unittest.TestCase): """ def test_no_params(self): - with self.assertRaises(Exception): + with self.assertRaises(PortSettingsError): PortSettings() def test_empty_config(self): - with self.assertRaises(Exception): + with self.assertRaises(PortSettingsError): PortSettings(**dict()) def test_name_only(self): - with self.assertRaises(Exception): + with self.assertRaises(PortSettingsError): PortSettings(name='foo') def test_config_name_only(self): - with self.assertRaises(Exception): + with self.assertRaises(PortSettingsError): PortSettings(**{'name': 'foo'}) def test_name_netname_only(self): -- cgit 1.2.3-korg