Add uuid utils

Change-Id: Iae18676b91218dc5d7f0f74aa64056811cd82776
This commit is contained in:
changzhi1990 2016-04-06 11:46:03 +08:00
parent 716083ac95
commit 1aeb503b1d
1 changed files with 30 additions and 0 deletions

View File

@ -18,6 +18,7 @@ import jsonrpclib
import six
import socket
import sys
import uuid
def safe_decode(text, incoming=None, errors='strict'):
@ -134,3 +135,32 @@ def get_ip_from_agent(node, net_type):
return 1
except Exception as e:
print "Can't get ip! Because: %s" % e
def generate_uuid():
"""Creates a random uuid string.
:returns: string
"""
return str(uuid.uuid4())
def _format_uuid_string(string):
return (string.replace('urn:', '')
.replace('uuid:', '')
.strip('{}')
.replace('-', '')
.lower())
def is_uuid_like(val):
"""Returns validation of a value as a UUID.
:param val: Value to verify
:type val: string
:returns: bool
"""
try:
return str(uuid.UUID(val)).replace('-', '') == _format_uuid_string(val)
except (TypeError, ValueError, AttributeError):
return False