Created new exception classes for networks.
[snaps.git] / snaps / openstack / create_network.py
index 8357313..4f27eec 100644 (file)
@@ -67,25 +67,22 @@ class OpenStackNetwork:
                     ' mode')
                 return
         logger.debug(
-            "Network '%s' created successfully" % self.__network['network'][
-                'id'])
+            "Network '%s' created successfully" % self.__network.id)
 
         logger.debug('Creating Subnets....')
         for subnet_setting in self.network_settings.subnet_settings:
-            sub_inst = neutron_utils.get_subnet_by_name(self.__neutron,
-                                                        subnet_setting.name)
+            sub_inst = neutron_utils.get_subnet_by_name(
+                self.__neutron, subnet_setting.name)
             if sub_inst:
                 self.__subnets.append(sub_inst)
                 logger.debug(
-                    "Subnet '%s' created successfully" % sub_inst['subnet'][
-                        'id'])
+                    "Subnet '%s' created successfully" % sub_inst.id)
             else:
                 if not cleanup:
                     self.__subnets.append(
-                        neutron_utils.create_subnet(self.__neutron,
-                                                    subnet_setting,
-                                                    self.__os_creds,
-                                                    self.__network))
+                        neutron_utils.create_subnet(
+                            self.__neutron, subnet_setting, self.__os_creds,
+                            self.__network))
 
         return self.__network
 
@@ -96,7 +93,7 @@ class OpenStackNetwork:
         for subnet in self.__subnets:
             try:
                 logger.info(
-                    'Deleting subnet with name ' + subnet['subnet']['name'])
+                    'Deleting subnet with name ' + subnet.name)
                 neutron_utils.delete_subnet(self.__neutron, subnet)
             except NotFound as e:
                 logger.warning(
@@ -192,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):
         """
@@ -235,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:
@@ -247,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
@@ -289,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:
@@ -317,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):
         """
@@ -335,7 +340,7 @@ class SubnetSettings:
         }
 
         if network:
-            out['network_id'] = network['network']['id']
+            out['network_id'] = network.id
         if self.name:
             out['name'] = self.name
         if self.project_name:
@@ -347,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:
@@ -371,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
@@ -413,6 +424,9 @@ class PortSettings:
                           For example, a virtual server.
         :return:
         """
+        if 'port' in kwargs:
+            kwargs = kwargs['port']
+
         self.network = None
 
         self.name = kwargs.get('name')
@@ -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')
 
@@ -454,10 +468,9 @@ class PortSettings:
                                                               'subnet_name'])
                 if subnet:
                     self.fixed_ips.append({'ip_address': ip_addr_dict['ip'],
-                                           'subnet_id': subnet['subnet'][
-                                               'id']})
+                                           'subnet_id': subnet.id})
                 else:
-                    raise Exception(
+                    raise PortSettingsError(
                         'Invalid port configuration, subnet does not exist '
                         'with name - ' + ip_addr_dict['subnet_name'])
 
@@ -488,10 +501,10 @@ 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['network']['id']
+        out['network_id'] = self.network.id
 
         if self.admin_state_up is not None:
             out['admin_state_up'] = self.admin_state_up
@@ -501,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:
@@ -521,3 +534,24 @@ class PortSettings:
         if self.device_id:
             out['device_id'] = self.device_id
         return {'port': out}
+
+    def __eq__(self, other):
+        return (self.name == other.name and
+                self.network_name == other.network_name and
+                self.admin_state_up == other.admin_state_up and
+                self.project_name == other.project_name and
+                self.mac_address == other.mac_address and
+                self.ip_addrs == other.ip_addrs and
+                self.fixed_ips == other.fixed_ips and
+                self.security_groups == other.security_groups and
+                self.allowed_address_pairs == other.allowed_address_pairs and
+                self.opt_value == other.opt_value and
+                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
+    """