Bug 2024 71/12071/3
authorbeierlm <mark.beierl@canonical.com>
Wed, 18 May 2022 01:25:29 +0000 (21:25 -0400)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Mon, 30 May 2022 00:28:29 +0000 (02:28 +0200)
Creates a rollback for deleting files created before validation
failure

Fixes Bug 2024

Change-Id: I7c3ab4ace06acad073dac492a25b78c2d5a11427
Signed-off-by: beierlm <mark.beierl@canonical.com>
osm_nbi/descriptor_topics.py
osm_nbi/instance_topics.py
osm_nbi/nbi.py
osm_nbi/tests/test_descriptor_topics.py

index 590380a..ddec65c 100644 (file)
@@ -268,6 +268,8 @@ class DescriptorTopic(BaseTopic):
         # TODO change to Content-Disposition filename https://tools.ietf.org/html/rfc6266
         file_pkg = None
         error_text = ""
+        fs_rollback = []
+
         try:
             if content_range_text:
                 content_range = (
@@ -296,9 +298,10 @@ class DescriptorTopic(BaseTopic):
             else:
                 self.fs.file_delete(proposed_revision_path, ignore_non_exist=True)
                 self.fs.mkdir(proposed_revision_path)
+                fs_rollback.append(proposed_revision_path)
 
             storage = self.fs.get_params()
-            storage["folder"] = _id
+            storage["folder"] = proposed_revision_path
 
             file_path = (proposed_revision_path, filename)
             if self.fs.file_exists(file_path, "file"):
@@ -464,17 +467,6 @@ class DescriptorTopic(BaseTopic):
                     self.fs.file_delete(proposed_revision_path, ignore_non_exist=True)
                     raise e
 
-            # Copy the revision to the active package name by its original id
-            shutil.rmtree(self.fs.path + current_revision_path, ignore_errors=True)
-            os.rename(self.fs.path + proposed_revision_path, self.fs.path + current_revision_path)
-            self.fs.file_delete(current_revision_path, ignore_non_exist=True)
-            self.fs.mkdir(current_revision_path)
-            self.fs.reverse_sync(from_path=current_revision_path)
-            shutil.rmtree(self.fs.path + _id)
-
-            current_desc["_admin"]["storage"] = storage
-            current_desc["_admin"]["onboardingState"] = "ONBOARDED"
-            current_desc["_admin"]["operationalState"] = "ENABLED"
 
             indata = self._remove_envelop(indata)
 
@@ -482,18 +474,33 @@ class DescriptorTopic(BaseTopic):
             if kwargs:
                 self._update_input_with_kwargs(indata, kwargs)
 
+            current_desc["_admin"]["storage"] = storage
+            current_desc["_admin"]["onboardingState"] = "ONBOARDED"
+            current_desc["_admin"]["operationalState"] = "ENABLED"
+            current_desc["_admin"]["modified"] = time()
+            current_desc["_admin"]["revision"] = revision
+
             deep_update_rfc7396(current_desc, indata)
             current_desc = self.check_conflict_on_edit(
                 session, current_desc, indata, _id=_id
             )
-            current_desc["_admin"]["modified"] = time()
-            current_desc["_admin"]["revision"] = revision
+
+            # Copy the revision to the active package name by its original id
+            shutil.rmtree(self.fs.path + current_revision_path, ignore_errors=True)
+            os.rename(self.fs.path + proposed_revision_path, self.fs.path + current_revision_path)
+            self.fs.file_delete(current_revision_path, ignore_non_exist=True)
+            self.fs.mkdir(current_revision_path)
+            self.fs.reverse_sync(from_path=current_revision_path)
+
+            shutil.rmtree(self.fs.path + _id)
+
             self.db.replace(self.topic, _id, current_desc)
 
             #  Store a copy of the package as a point in time revision
             revision_desc = dict(current_desc)
             revision_desc["_id"] = _id + ":" + str(revision_desc["_admin"]["revision"])
             self.db.create(self.topic + "_revisions", revision_desc)
+            fs_rollback = []
 
             indata["_id"] = _id
             self._send_msg("edited", indata)
@@ -525,6 +532,8 @@ class DescriptorTopic(BaseTopic):
         finally:
             if file_pkg:
                 file_pkg.close()
+            for file in fs_rollback:
+                self.fs.file_delete(file, ignore_non_exist=True)
 
     def get_file(self, session, _id, path=None, accept_header=None):
         """
index aac0b0b..84aeda3 100644 (file)
@@ -557,6 +557,9 @@ class NsrTopic(BaseTopic):
             "image": [],
             "affinity-or-anti-affinity-group": [],
         }
+        if "revision" in nsd["_admin"]:
+            nsr_descriptor["revision"] = nsd["_admin"]["revision"]
+
         ns_request["nsr_id"] = nsr_id
         if ns_request and ns_request.get("config-units"):
             nsr_descriptor["config-units"] = ns_request["config-units"]
index f19f44d..29e085a 100644 (file)
@@ -172,7 +172,7 @@ query string:
         ADMIN: To act as an administrator or a different project
         PUBLIC: To get public descriptors or set a descriptor as public
         SET_PROJECT: To make a descriptor available for other project
-        
+
 Header field name      Reference       Example Descriptions
     Accept     IETF RFC 7231 [19]      application/json        Content-Types that are acceptable for the response.
     This header field shall be present if the response is expected to have a non-empty message body.
index 6b9b2c3..147f3e7 100755 (executable)
@@ -202,7 +202,7 @@ class Test_VnfdTopic(TestCase):
                 )
                 self.assertEqual(admin["usageState"], "NOT_IN_USE", "Wrong usage state")
                 storage = admin["storage"]
-                self.assertEqual(storage["folder"], did, "Wrong storage folder")
+                self.assertEqual(storage["folder"], did + ":1", "Wrong storage folder")
                 self.assertEqual(
                     storage["descriptor"], "package", "Wrong storage descriptor"
                 )
@@ -1406,7 +1406,7 @@ class Test_NsdTopic(TestCase):
                 )
                 self.assertEqual(admin["usageState"], "NOT_IN_USE", "Wrong usage state")
                 storage = admin["storage"]
-                self.assertEqual(storage["folder"], did, "Wrong storage folder")
+                self.assertEqual(storage["folder"], did + ":1", "Wrong storage folder")
                 self.assertEqual(
                     storage["descriptor"], "package", "Wrong storage descriptor"
                 )