Fixed bug with regards to subnet lookups.
[snaps.git] / snaps / config / router.py
index 72164f2..2a0b6a4 100644 (file)
@@ -33,7 +33,11 @@ class RouterConfig(object):
         :param admin_state_up: The administrative status of the router.
                                True = up / False = down (default True)
         :param internal_subnets: List of subnet names to which to connect this
-                                 router for Floating IP purposes
+                                 router (this way is deprecated).
+                                 *** NEW WAY ***
+                                 List of dict where the key is 'subnet' that
+                                 contains members with the following keys:
+                                 project_name, network_name, and subnet_name
         :param port_settings: List of PortConfig objects
         :return:
         """
@@ -45,6 +49,19 @@ class RouterConfig(object):
         self.enable_snat = kwargs.get('enable_snat')
         if kwargs.get('internal_subnets'):
             self.internal_subnets = kwargs['internal_subnets']
+            if isinstance(self.internal_subnets, dict):
+                if 'subnet' not in self.internal_subnets:
+                    raise RouterConfigError(
+                        'subnet is a required key to internal_subnets')
+                if 'project_name' not in self.internal_subnets['subnet']:
+                    raise RouterConfigError(
+                        'subnet.project is a required key to subnet')
+                if 'network_name' not in self.internal_subnets['subnet']:
+                    raise RouterConfigError(
+                        'network_name is a required key to subnet')
+                if 'subnet_name' not in self.internal_subnets['subnet']:
+                    raise RouterConfigError(
+                        'subnet_name is a required key to subnet')
         else:
             self.internal_subnets = list()
 
@@ -70,39 +87,42 @@ class RouterConfig(object):
         TODO - expand automated testing to exercise all parameters
         :param neutron: The neutron client to retrieve external network
                         information if necessary
-        :param os_creds: The OpenStack credentials
+        :param os_creds: The OpenStack credentials for retrieving the keystone
+                         client for looking up the project ID when the
+                         self.project_name is not None
         :return: the dictionary object
         """
         out = dict()
         ext_gw = dict()
 
-        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)
-            project_id = None
-            if project:
-                project_id = project.id
-            if project_id:
-                out['tenant_id'] = project_id
-            else:
-                raise RouterConfigError(
-                    'Could not find project ID for project named - ' +
-                    self.project_name)
-        if self.admin_state_up is not None:
-            out['admin_state_up'] = self.admin_state_up
-        if self.external_gateway:
-            ext_net = neutron_utils.get_network(
-                neutron, network_name=self.external_gateway)
-            if ext_net:
-                ext_gw['network_id'] = ext_net.id
-                out['external_gateway_info'] = ext_gw
-            else:
-                raise RouterConfigError(
-                    'Could not find the external network named - ' +
-                    self.external_gateway)
+        session = keystone_utils.keystone_session(os_creds)
+        keystone = keystone_utils.keystone_client(os_creds, session)
+        try:
+            if self.name:
+                out['name'] = self.name
+            if self.project_name:
+                project = keystone_utils.get_project(
+                    keystone=keystone, project_name=self.project_name)
+                if project:
+                        out['tenant_id'] = project.id
+                else:
+                    raise RouterConfigError(
+                        'Could not find project ID for project named - ' +
+                        self.project_name)
+            if self.admin_state_up is not None:
+                out['admin_state_up'] = self.admin_state_up
+            if self.external_gateway:
+                ext_net = neutron_utils.get_network(
+                    neutron, keystone, network_name=self.external_gateway)
+                if ext_net:
+                    ext_gw['network_id'] = ext_net.id
+                    out['external_gateway_info'] = ext_gw
+                else:
+                    raise RouterConfigError(
+                        'Could not find the external network named - ' +
+                        self.external_gateway)
+        finally:
+            keystone_utils.close_session(session)
 
         return {'router': out}