Extract generate_hostid method into utils.py

This would be used in instance action API to show the hostId
to non-admin user.

This patch propose to extract the generate_hostid method
into utils.

trivial fix

Change-Id: I00d29e9fd80e6b8f7ba3bbd8e82dde9d4cb1522f
This commit is contained in:
Yikun Jiang
2018-03-22 20:16:41 +08:00
parent cbf02e050e
commit 7500462bc3
3 changed files with 30 additions and 7 deletions

View File

@@ -1380,3 +1380,21 @@ def supports_direct_io(dirpath):
pass
return hasDirectIO
def generate_hostid(host, project_id):
"""Generate an obfuscated host id representing the host.
This is a hashed value so will not actually look like a hostname, and is
hashed with data from the project_id.
:param host: The name of the compute host.
:param project_id: The UUID of the project.
:return: An obfuscated hashed host id string, return "" if host is empty
"""
if host:
data = (project_id + host).encode('utf-8')
sha_hash = hashlib.sha224(data)
return sha_hash.hexdigest()
else:
return ""