From 7ea3d2bd350c78c14c681fac66b52ca491df2d96 Mon Sep 17 00:00:00 2001
From: adrian-turjak <adriant@catalyst.net.nz>
Date: Wed, 24 May 2017 16:05:21 +1200
Subject: [PATCH] Fix issue with network creation and reapproval

* Once the interface is created, it shouldn't happen again
  in the event of a rerun. Storing a flag now to avoid check
  and skip if done already.

Change-Id: I71808b4e79889b6d3bd397d4fec459e7f9f07909
---
 adjutant/actions/v1/resources.py              | 25 +++++++++++--------
 adjutant/actions/v1/tests/__init__.py         | 12 +++++----
 .../actions/v1/tests/test_resource_actions.py |  4 +++
 3 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/adjutant/actions/v1/resources.py b/adjutant/actions/v1/resources.py
index 87a02d2..26d6716 100644
--- a/adjutant/actions/v1/resources.py
+++ b/adjutant/actions/v1/resources.py
@@ -149,17 +149,22 @@ class NewDefaultNetworkAction(BaseAction, ProjectMixin):
             self.add_note("Router already created for project %s" %
                           self.project_id)
 
-        try:
-            interface_body = {
-                "subnet_id": self.get_cache('subnet_id')
-            }
-            neutron.add_interface_router(self.get_cache('router_id'),
-                                         body=interface_body)
-        except Exception as e:
+        if not self.get_cache('port_id'):
+            try:
+                interface_body = {
+                    "subnet_id": self.get_cache('subnet_id')
+                }
+                interface = neutron.add_interface_router(
+                    self.get_cache('router_id'), body=interface_body)
+            except Exception as e:
+                self.add_note(
+                    "Error: '%s' while attaching interface" % e)
+                raise
+            self.set_cache('port_id', interface['port_id'])
+            self.add_note("Interface added to router for subnet")
+        else:
             self.add_note(
-                "Error: '%s' while attaching interface" % e)
-            raise
-        self.add_note("Interface added to router for subnet")
+                "Interface added to router for project %s" % self.project_id)
 
     def _pre_approve(self):
         # Note: Do we need to get this from cache? it is a required setting
diff --git a/adjutant/actions/v1/tests/__init__.py b/adjutant/actions/v1/tests/__init__.py
index 79d6d77..9e2c847 100644
--- a/adjutant/actions/v1/tests/__init__.py
+++ b/adjutant/actions/v1/tests/__init__.py
@@ -94,11 +94,13 @@ class FakeNeutronClient(object):
 
     def add_interface_router(self, router_id, body):
         global neutron_cache
-        # find project_id
-        router = neutron_cache['RegionOne']['test_project_id'][
-            'routers'][router_id]
-        router['router']['interface'] = body
-        return router
+        port_id = "port_id_%s" % neutron_cache['RegionOne']['i']
+        neutron_cache['RegionOne']['i'] += 1
+        interface = {
+            'port_id': port_id,
+            'id': router_id,
+            'subnet_id': body['subnet_id']}
+        return interface
 
     def update_quota(self, project_id, body):
         global neutron_cache
diff --git a/adjutant/actions/v1/tests/test_resource_actions.py b/adjutant/actions/v1/tests/test_resource_actions.py
index 9c3369e..1aa5d05 100644
--- a/adjutant/actions/v1/tests/test_resource_actions.py
+++ b/adjutant/actions/v1/tests/test_resource_actions.py
@@ -81,6 +81,7 @@ class ProjectSetupActionTests(TestCase):
         self.assertEquals(
             action.action.cache,
             {'network_id': 'net_id_0',
+             'port_id': 'port_id_3',
              'router_id': 'router_id_2',
              'subnet_id': 'subnet_id_1'}
         )
@@ -197,6 +198,7 @@ class ProjectSetupActionTests(TestCase):
         self.assertEquals(
             action.action.cache,
             {'network_id': 'net_id_0',
+             'port_id': 'port_id_3',
              'router_id': 'router_id_2',
              'subnet_id': 'subnet_id_1'}
         )
@@ -256,6 +258,7 @@ class ProjectSetupActionTests(TestCase):
         self.assertEquals(
             action.action.cache,
             {'network_id': 'net_id_0',
+             'port_id': 'port_id_3',
              'router_id': 'router_id_2',
              'subnet_id': 'subnet_id_1'}
         )
@@ -404,6 +407,7 @@ class ProjectSetupActionTests(TestCase):
         self.assertEquals(
             action.action.cache,
             {'network_id': 'net_id_0',
+             'port_id': 'port_id_3',
              'router_id': 'router_id_2',
              'subnet_id': 'subnet_id_1'}
         )