Merge "Cleaned up extra network created in the test test_set_one_port_two_ip_two_subn...
[snaps.git] / snaps / config / network.py
index 39a4254..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
@@ -98,11 +101,15 @@ class NetworkConfig(object):
             return self.project_id
         else:
             if self.project_name:
-                keystone = keystone_utils.keystone_client(os_creds)
-                project = keystone_utils.get_project(
-                    keystone=keystone, project_name=self.project_name)
-                if project:
-                    return project.id
+                session = keystone_utils.keystone_session(os_creds)
+                keystone = keystone_utils.keystone_client(os_creds, session)
+                try:
+                    project = keystone_utils.get_project(
+                        keystone=keystone, project_name=self.project_name)
+                    if project:
+                        return project.id
+                finally:
+                    keystone_utils.close_session(session)
 
         return None
 
@@ -140,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}
 
 
@@ -175,14 +184,17 @@ class SubnetConfig(object):
                              through authorization policies (optional)
         :param start: The start address for the allocation pools (optional)
         :param end: The end address for the allocation pools (optional)
-        :param gateway_ip: The gateway IP address (optional)
+        :param gateway_ip: The gateway IP address (optional). When not
+                           configured, the IP address will be automatically
+                           assigned; when 'none', no gateway address will be
+                           assigned, else the value must be valid
         :param enable_dhcp: Set to true if DHCP is enabled and false if DHCP is
                             disabled (optional)
         :param dns_nameservers: A list of DNS name servers for the subnet.
                                 Specify each name server as an IP address
                                 and separate multiple entries with a space.
                                 For example [8.8.8.7 8.8.8.8]
-                                (default '8.8.8.8')
+                                (default [])
         :param host_routes: A list of host route dictionaries for the subnet.
                             For example:
                                 "host_routes":[
@@ -221,10 +233,7 @@ class SubnetConfig(object):
         if 'dns_nameservers' in kwargs:
             self.dns_nameservers = kwargs.get('dns_nameservers')
         else:
-            if self.ip_version == 4:
-                self.dns_nameservers = ['8.8.8.8']
-            else:
-                self.dns_nameservers = list()
+            self.dns_nameservers = list()
 
         self.host_routes = kwargs.get('host_routes')
         self.destination = kwargs.get('destination')
@@ -255,9 +264,13 @@ class SubnetConfig(object):
         if self.name:
             out['name'] = self.name
         if self.project_name:
-            keystone = keystone_utils.keystone_client(os_creds)
-            project = keystone_utils.get_project(
-                keystone=keystone, project_name=self.project_name)
+            session = keystone_utils.keystone_session(os_creds)
+            keystone = keystone_utils.keystone_client(os_creds, session)
+            try:
+                project = keystone_utils.get_project(
+                    keystone=keystone, project_name=self.project_name)
+            finally:
+                keystone_utils.close_session(session)
             project_id = None
             if project:
                 project_id = project.id
@@ -270,7 +283,10 @@ class SubnetConfig(object):
         if self.start and self.end:
             out['allocation_pools'] = [{'start': self.start, 'end': self.end}]
         if self.gateway_ip:
-            out['gateway_ip'] = self.gateway_ip
+            if self.gateway_ip == 'none':
+                out['gateway_ip'] = None
+            else:
+                out['gateway_ip'] = self.gateway_ip
         if self.enable_dhcp is not None:
             out['enable_dhcp'] = self.enable_dhcp
         if self.dns_nameservers and len(self.dns_nameservers) > 0:
@@ -401,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 '
@@ -435,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_id = None
+        project_name = os_creds.project_name
         if self.project_name:
-            keystone = keystone_utils.keystone_client(os_creds)
-            project = keystone_utils.get_project(
-                keystone=keystone, project_name=self.project_name)
-            if project:
-                project_id = project.id
+            project_name = project_name
+        try:
+            network = neutron_utils.get_network(
+                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:
+            if session:
+                keystone_utils.close_session(session)
 
-        network = neutron_utils.get_network(
-            neutron, network_name=self.network_name, project_id=project_id)
         if not network:
             raise PortConfigError(
-                'Cannot locate network with name - ' + self.network_name)
+                'Cannot locate network with name - ' + self.network_name
+                + ' in project - ' + str(project_name))
 
         out['network_id'] = network.id
 
@@ -459,6 +482,11 @@ class PortConfig(object):
         if self.name:
             out['name'] = self.name
         if self.project_name:
+            project = keystone_utils.get_project(
+                keystone=keystone, project_name=self.project_name)
+            project_id = None
+            if project:
+                project_id = project.id
             if project_id:
                 out['tenant_id'] = project_id
             else:
@@ -468,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
 
@@ -476,7 +504,8 @@ class PortConfig(object):
             sec_grp_ids = list()
             for sec_grp_name in self.security_groups:
                 sec_grp = neutron_utils.get_security_group(
-                    neutron, sec_grp_name=sec_grp_name)
+                    neutron, keystone, sec_grp_name=sec_grp_name,
+                    project_name=self.project_name)
                 if sec_grp:
                     sec_grp_ids.append(sec_grp.id)
             out['security_groups'] = sec_grp_ids