From f2ffae18709e824dcd1dbd0bc2b9431b3f9623e6 Mon Sep 17 00:00:00 2001
From: Tom Barron <tpb@dyncloud.net>
Date: Mon, 14 Nov 2016 12:47:25 -0500
Subject: [PATCH] hacking: Use uuidutils to generate UUID

Added hacking check to ensure that UUIDs are not generated
by uuid4() since we should do this using oslo_utils.uuidutils.

Based on this nova change [1].

[1] I73ee63fbd4f451d3aa5dc1a2a734d68c308b4440

Change-Id: Ic7783c29fbd838c827ccc8ee44aa757fef8e9169
---
 HACKING.rst                  |  1 +
 manila/hacking/checks.py     | 19 +++++++++++++++++++
 manila/tests/test_hacking.py | 13 +++++++++++++
 3 files changed, 33 insertions(+)

diff --git a/HACKING.rst b/HACKING.rst
index d8c634edfc..3045daf3a0 100644
--- a/HACKING.rst
+++ b/HACKING.rst
@@ -26,6 +26,7 @@ Manila Specific Commandments
 - [M336] Must use a dict comprehension instead of a dict constructor
   with a sequence of key-value pairs.
 - [M337] Ensure to not use xrange().
+- [M354] Use oslo_utils.uuidutils to generate UUID instead of uuid4().
 
 LOG Translations
 ----------------
diff --git a/manila/hacking/checks.py b/manila/hacking/checks.py
index 1778293ae2..068026a3e3 100644
--- a/manila/hacking/checks.py
+++ b/manila/hacking/checks.py
@@ -342,6 +342,24 @@ def validate_assertIsNone(logical_line):
         yield(0, msg)
 
 
+def check_uuid4(logical_line):
+    """Generating UUID
+
+    Use oslo_utils.uuidutils to generate UUID instead of uuid4().
+
+    M354
+    """
+
+    msg = ("M354: Use oslo_utils.uuidutils to generate UUID instead "
+           "of uuid4().")
+
+    if "uuid4()." in logical_line:
+        return
+
+    if "uuid4()" in logical_line:
+        yield (0, msg)
+
+
 def factory(register):
     register(validate_log_translations)
     register(check_explicit_underscore_import)
@@ -354,3 +372,4 @@ def factory(register):
     register(no_xrange)
     register(validate_assertTrue)
     register(validate_assertIsNone)
+    register(check_uuid4)
diff --git a/manila/tests/test_hacking.py b/manila/tests/test_hacking.py
index b6093d319b..bfe773c3c5 100644
--- a/manila/tests/test_hacking.py
+++ b/manila/tests/test_hacking.py
@@ -316,3 +316,16 @@ class HackingTestCase(test.TestCase):
             "assertIsNone(None)"))))
         self.assertEqual(1, len(list(checks.validate_assertIsNone(
             "assertEqual(None, %s)" % test_value))))
+
+    def test_check_uuid4(self):
+        code = """
+            fake_uuid = uuid.uuid4()
+               """
+        errors = [(1, 0, 'M354')]
+        self._assert_has_errors(code, checks.check_uuid4,
+                                expected_errors=errors)
+
+        code = """
+               hex_uuid = uuid.uuid4().hex
+               """
+        self._assert_has_no_errors(code, checks.check_uuid4)