From a02f1d81f6651ddf5b9353c2e1d7fbf873a19466 Mon Sep 17 00:00:00 2001 From: Parker Berberian Date: Fri, 28 Jun 2019 09:58:54 -0400 Subject: Adds Hostname Validator Creates a single place to validate hostnames, with public fields for regex that can be copied to the frontend. We do hostname validation inconsistently all over the place, we should move to using this single validator. Change-Id: I7b71fd89843a7e5b7f9d93dcb23f4645abe71dd0 Signed-off-by: Parker Berberian --- src/resource_inventory/resource_manager.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/resource_inventory/resource_manager.py') diff --git a/src/resource_inventory/resource_manager.py b/src/resource_inventory/resource_manager.py index 652e4e3..28bed20 100644 --- a/src/resource_inventory/resource_manager.py +++ b/src/resource_inventory/resource_manager.py @@ -6,7 +6,7 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## - +import re from dashboard.exceptions import ( ResourceExistenceException, @@ -172,3 +172,13 @@ class ResourceManager: self.releaseNetworks(grb, vlan_manager, vlans) for host in hosts: self.releaseHost(host) + + +class HostNameValidator(object): + regex = r'^[A-Za-z0-9][A-Za-z0-9-]*$' + message = "Hostnames can only contain alphanumeric characters and hyphens (-). Hostnames must start with a letter" + pattern = re.compile(regex) + + @classmethod + def is_valid_hostname(cls, hostname): + return len(hostname) < 65 and cls.pattern.fullmatch(hostname) is not None -- cgit 1.2.3-korg