util: move validate_name to XMLBuilder.validate_generic_name

This commit is contained in:
Cole Robinson 2019-06-07 18:02:42 -04:00
parent c2de4d7c36
commit 5ed8f2aa5f
5 changed files with 19 additions and 19 deletions

View File

@ -105,7 +105,7 @@ class Guest(XMLBuilder):
@staticmethod
def validate_name(conn, name, check_collision=True, validate=True):
if validate:
util.validate_name(_("Guest"), name)
XMLBuilder.validate_generic_name(_("Guest"), name)
if not check_collision:
return

View File

@ -194,7 +194,7 @@ class Network(XMLBuilder):
@staticmethod
def validate_name(conn, name):
util.validate_name(_("Network"), name)
XMLBuilder.validate_generic_name(_("Network"), name)
try:
conn.networkLookupByName(name)

View File

@ -285,7 +285,7 @@ class StoragePool(_StorageObject):
@staticmethod
def validate_name(conn, name):
util.validate_name(_("Storage object"), name)
XMLBuilder.validate_generic_name(_("Storage object"), name)
try:
conn.storagePoolLookupByName(name)
@ -624,7 +624,7 @@ class StorageVolume(_StorageObject):
@staticmethod
def validate_name(pool, name):
util.validate_name(_("Storage object"), name)
XMLBuilder.validate_generic_name(_("Storage object"), name)
try:
pool.storageVolLookupByName(name)

View File

@ -33,21 +33,6 @@ def libvirt_collision(collision_cb, val):
return check
def validate_name(name_type, val):
# Rather than try and match libvirt's regex, just forbid things we
# know don't work
forbid = [" "]
if not val:
raise ValueError(
_("A name must be specified for the %s") % name_type)
for c in forbid:
if c not in val:
continue
raise ValueError(
_("%s name '%s' can not contain '%s' character.") %
(name_type, val, c))
def generate_name(base, collision_cb, suffix="", lib_collision=True,
start_num=1, sep="-", force_num=False, collidelist=None):
"""

View File

@ -449,6 +449,21 @@ class XMLBuilder(object):
def register_namespace(nsname, uri):
XMLAPI.register_namespace(nsname, uri)
@staticmethod
def validate_generic_name(name_label, val):
# Rather than try and match libvirt's regex, just forbid things we
# know don't work
forbid = [" "]
if not val:
raise ValueError(
_("A name must be specified for the %s") % name_label)
for c in forbid:
if c not in val:
continue
raise ValueError(
_("%s name '%s' can not contain '%s' character.") %
(name_label, val, c))
def __init__(self, conn, parsexml=None,
parentxmlstate=None, relative_object_xpath=None):