diff options
| author | Brian Lamar <brian.lamar@rackspace.com> | 2011-11-09 18:16:24 -0500 |
|---|---|---|
| committer | Brian Lamar <brian.lamar@rackspace.com> | 2011-11-15 17:36:02 -0500 |
| commit | bcfff3dd530994d3ae22f945f429df3afc954cb0 (patch) | |
| tree | 5746bf0af3e79d01bd9f2996668a404a212e1d67 /nova/utils.py | |
| parent | dd5c3f7badf24e21eb1b311639bcbc55512bc117 (diff) | |
Follow hostname RFCs
Updated hostname sanitization method to more closely follow RFC-952
and RFC-1123. Also moved it to nova.utils, where it seems to fit
better.
Fixes bug 885374
(Patch Set 1) Updated hostname sanitization with more efficient and
[opinion] more readable implementation.
Change-Id: I60d7ee89867c05950bec1fd53b072a1c6247ebea
Diffstat (limited to 'nova/utils.py')
| -rw-r--r-- | nova/utils.py | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/nova/utils.py b/nova/utils.py index 7723837c9..ec5de7aab 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -1061,3 +1061,16 @@ def total_seconds(td): else: return ((td.days * 86400 + td.seconds) * 10 ** 6 + td.microseconds) / 10.0 ** 6 + + +def sanitize_hostname(hostname): + """Return a hostname which conforms to RFC-952 and RFC-1123 specs.""" + if isinstance(hostname, unicode): + hostname = hostname.encode('latin-1', 'ignore') + + hostname = re.sub('[ _]', '-', hostname) + hostname = re.sub('[^\w.-]+', '', hostname) + hostname = hostname.lower() + hostname = hostname.strip('.-') + + return hostname |
