Merge "Cleaned up extra network created in the test test_set_one_port_two_ip_two_subn...
[snaps.git] / snaps / config / network.py
index 8148c61..ffafbb6 100644 (file)
@@ -46,6 +46,7 @@ class NetworkConfig(object):
         :param segmentation_id: the id of the segmentation
                                  (this is required when network_type is 'vlan')
         :param subnets or subnet_settings: List of SubnetConfig objects.
+        :param mtu: MTU setting (optional)
         :return:
         """
 
@@ -88,6 +89,8 @@ class NetworkConfig(object):
         if not self.name or len(self.name) < 1:
             raise NetworkConfigError('Name required for networks')
 
+        self.mtu = kwargs.get('mtu')
+
     def get_project_id(self, os_creds):
         """
         Returns the project ID for a given project_name or None
@@ -144,6 +147,8 @@ class NetworkConfig(object):
             out['provider:segmentation_id'] = self.segmentation_id
         if self.external:
             out['router:external'] = self.external
+        if self.mtu:
+            out['mtu'] = self.mtu
         return {'network': out}
 
 
@@ -412,22 +417,23 @@ class PortConfig(object):
             raise PortConfigError(
                 'The attribute network_name is required')
 
-    def __get_fixed_ips(self, neutron):
+    def __get_fixed_ips(self, neutron, network):
         """
         Sets the self.fixed_ips value
         :param neutron: the Neutron client
+        :param network: the SNAPS-OO network domain object
         :return: None
         """
-
         fixed_ips = list()
         if self.ip_addrs:
 
             for ip_addr_dict in self.ip_addrs:
                 subnet = neutron_utils.get_subnet(
-                    neutron, subnet_name=ip_addr_dict['subnet_name'])
-                if subnet and 'ip' in ip_addr_dict:
-                    fixed_ips.append({'ip_address': ip_addr_dict['ip'],
-                                      'subnet_id': subnet.id})
+                    neutron, network, subnet_name=ip_addr_dict['subnet_name'])
+                if subnet:
+                    if 'ip' in ip_addr_dict:
+                        fixed_ips.append({'ip_address': ip_addr_dict['ip'],
+                                          'subnet_id': subnet.id})
                 else:
                     raise PortConfigError(
                         'Invalid port configuration, subnet does not exist '
@@ -446,22 +452,28 @@ class PortConfig(object):
         :param os_creds: the OpenStack credentials
         :return: the dictionary object
         """
-
         out = dict()
-
         session = keystone_utils.keystone_session(os_creds)
         keystone = keystone_utils.keystone_client(os_creds, session)
+
+        project_name = os_creds.project_name
+        if self.project_name:
+            project_name = project_name
         try:
             network = neutron_utils.get_network(
-                neutron, keystone, network_name=self.network_name,
-                project_name=self.project_name)
+                neutron, keystone, network_name=self.network_name)
+            if network and (not network.shared or not network.external):
+                network = neutron_utils.get_network(
+                    neutron, keystone, network_name=self.network_name,
+                    project_name=project_name)
         finally:
-            keystone_utils.close_session(session)
+            if session:
+                keystone_utils.close_session(session)
 
         if not network:
             raise PortConfigError(
                 'Cannot locate network with name - ' + self.network_name
-                + ' in project - ' + str(self.project_name))
+                + ' in project - ' + str(project_name))
 
         out['network_id'] = network.id
 
@@ -484,7 +496,7 @@ class PortConfig(object):
         if self.mac_address:
             out['mac_address'] = self.mac_address
 
-        fixed_ips = self.__get_fixed_ips(neutron)
+        fixed_ips = self.__get_fixed_ips(neutron, network)
         if fixed_ips and len(fixed_ips) > 0:
             out['fixed_ips'] = fixed_ips