Added all common modules in conductor directory
[optf/has.git] / conductor / conductor / common / models / __init__.py
1 #
2 # -------------------------------------------------------------------------
3 #   Copyright (c) 2015-2017 AT&T Intellectual Property
4 #
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 #
17 # -------------------------------------------------------------------------
18 #
19
20 from uuid import UUID
21
22 import six
23
24
25 def validate_uuid4(uuid_string):
26     """Validate that a UUID string is in fact a valid uuid4.
27
28     Happily, the uuid module does the actual checking for us.
29     It is vital that the 'version' kwarg be passed
30     to the UUID() call, otherwise any 32-character
31     hex string is considered valid.
32     """
33
34     if not isinstance(uuid_string, six.string_types):
35         return False
36
37     try:
38         val = UUID(uuid_string.translate(None, '-'), version=4)
39     except ValueError:
40         # If it's a value error, then the string
41         # is not a valid hex code for a UUID.
42         return False
43
44     # If the uuid_string is a valid hex code,
45     # but an invalid uuid4, the UUID.__init__ will convert it to a
46     # valid uuid4. This is bad for validation purposes.
47     return val.hex == uuid_string