def test_is_valid_logical_name(self): valid = ( "spam", "spAm", "SPAM", "spam-eggs", "spam.eggs", "spam_eggs", "spam~eggs", "9spam", "spam7", "~spam", ".spam", ".~-_", "~", "br34kf4st", "s", "s" * 63, "s" * 255, ) invalid = (" ", "spam eggs", "$pam", "egg$", "spam#eggs", " eggs", "spam ", "", None, "spam%20") for hostname in valid: result = utils.is_valid_logical_name(hostname) # Need to ensure a binary response for success. assertTrue # is too generous, and would pass this test if, for # instance, a regex Match object were returned. self.assertIs(result, True, "%s is unexpectedly invalid" % hostname) for hostname in invalid: result = utils.is_valid_logical_name(hostname) # Need to ensure a binary response for # success. assertFalse is too generous and would pass this # test if None were returned. self.assertIs(result, False, "%s is unexpectedly valid" % hostname)
def test_is_valid_logical_name(self): valid = ('spam', 'spAm', 'SPAM', 'spam-eggs', 'spam.eggs', 'spam_eggs', 'spam~eggs', '9spam', 'spam7', '~spam', '.spam', '.~-_', '~', 'br34kf4st', 's', 's' * 63, 's' * 255) invalid = (' ', 'spam eggs', '$pam', 'egg$', 'spam#eggs', ' eggs', 'spam ', '', None, 'spam%20') for hostname in valid: result = utils.is_valid_logical_name(hostname) # Need to ensure a binary response for success. assertTrue # is too generous, and would pass this test if, for # instance, a regex Match object were returned. self.assertIs(result, True, "%s is unexpectedly invalid" % hostname) for hostname in invalid: result = utils.is_valid_logical_name(hostname) # Need to ensure a binary response for # success. assertFalse is too generous and would pass this # test if None were returned. self.assertIs(result, False, "%s is unexpectedly valid" % hostname)
def name(name, value): """Validate that the value is a logical name :param name: Name of the argument :param value: A logical name string value :returns: The value, or None if value is None :raises: InvalidParameterValue if the value is not a valid logical name """ if value is None: return if not utils.is_valid_logical_name(value): raise exception.InvalidParameterValue( _('Expected name for %s: %s') % (name, value)) return value
def test_is_valid_logical_name(self): valid = ( 'spam', 'spAm', 'SPAM', 'spam-eggs', 'spam.eggs', 'spam_eggs', 'spam~eggs', '9spam', 'spam7', '~spam', '.spam', '.~-_', '~', 'br34kf4st', 's', 's' * 63, 's' * 255) invalid = ( ' ', 'spam eggs', '$pam', 'egg$', 'spam#eggs', ' eggs', 'spam ', '', None, 'spam%20') for hostname in valid: result = utils.is_valid_logical_name(hostname) # Need to ensure a binary response for success. assertTrue # is too generous, and would pass this test if, for # instance, a regex Match object were returned. self.assertIs(result, True, "%s is unexpectedly invalid" % hostname) for hostname in invalid: result = utils.is_valid_logical_name(hostname) # Need to ensure a binary response for # success. assertFalse is too generous and would pass this # test if None were returned. self.assertIs(result, False, "%s is unexpectedly valid" % hostname)
def uuid_or_name(name, value): """Validate that the value is a UUID or logical name :param name: Name of the argument :param value: A UUID or logical name string value :returns: The value, or None if value is None :raises: InvalidParameterValue if the value is not a valid UUID or logical name """ if value is None: return if (not utils.is_valid_logical_name(value) and not uuidutils.is_uuid_like(value)): raise exception.InvalidParameterValue( _('Expected UUID or name for %s: %s') % (name, value)) return value
def get(cls, context, allocation_ident): """Find an allocation by its ID, UUID or name. :param allocation_ident: The ID, UUID or name of an allocation. :param context: Security context :returns: An :class:`Allocation` object. :raises: InvalidIdentity """ if strutils.is_int_like(allocation_ident): return cls.get_by_id(context, allocation_ident) elif uuidutils.is_uuid_like(allocation_ident): return cls.get_by_uuid(context, allocation_ident) elif utils.is_valid_logical_name(allocation_ident): return cls.get_by_name(context, allocation_ident) else: raise exception.InvalidIdentity(identity=allocation_ident)
def get(cls, context, portgroup_ident): """Find a portgroup based on its id, uuid, name or address. :param portgroup_ident: The id, uuid, name or address of a portgroup. :param context: Security context :returns: A :class:`Portgroup` object. :raises: InvalidIdentity """ if strutils.is_int_like(portgroup_ident): return cls.get_by_id(context, portgroup_ident) elif uuidutils.is_uuid_like(portgroup_ident): return cls.get_by_uuid(context, portgroup_ident) elif netutils.is_valid_mac(portgroup_ident): return cls.get_by_address(context, portgroup_ident) elif utils.is_valid_logical_name(portgroup_ident): return cls.get_by_name(context, portgroup_ident) else: raise exception.InvalidIdentity(identity=portgroup_ident)
def get(cls, context, portgroup_ident): """Find a portgroup based on its id, uuid, name or address. :param portgroup_ident: The id, uuid, name or address of a portgroup. :param context: Security context :returns: A :class:`Portgroup` object. :raises: InvalidIdentity """ if strutils.is_int_like(portgroup_ident): return cls.get_by_id(context, portgroup_ident) elif uuidutils.is_uuid_like(portgroup_ident): return cls.get_by_uuid(context, portgroup_ident) elif utils.is_valid_mac(portgroup_ident): return cls.get_by_address(context, portgroup_ident) elif utils.is_valid_logical_name(portgroup_ident): return cls.get_by_name(context, portgroup_ident) else: raise exception.InvalidIdentity(identity=portgroup_ident)
def get_rpc_portgroup(portgroup_ident): """Get the RPC portgroup from the portgroup UUID or logical name. :param portgroup_ident: the UUID or logical name of a portgroup. :returns: The RPC portgroup. :raises: InvalidUuidOrName if the name or uuid provided is not valid. :raises: PortgroupNotFound if the portgroup is not found. """ # Check to see if the portgroup_ident is a valid UUID. If it is, treat it # as a UUID. if uuidutils.is_uuid_like(portgroup_ident): return objects.Portgroup.get_by_uuid(pecan.request.context, portgroup_ident) # We can refer to portgroups by their name if utils.is_valid_logical_name(portgroup_ident): return objects.Portgroup.get_by_name(pecan.request.context, portgroup_ident) raise exception.InvalidUuidOrName(name=portgroup_ident)
def get_rpc_allocation(allocation_ident): """Get the RPC allocation from the allocation UUID or logical name. :param allocation_ident: the UUID or logical name of an allocation. :returns: The RPC allocation. :raises: InvalidUuidOrName if the name or uuid provided is not valid. :raises: AllocationNotFound if the allocation is not found. """ # Check to see if the allocation_ident is a valid UUID. If it is, treat it # as a UUID. if uuidutils.is_uuid_like(allocation_ident): return objects.Allocation.get_by_uuid(pecan.request.context, allocation_ident) # We can refer to allocations by their name if utils.is_valid_logical_name(allocation_ident): return objects.Allocation.get_by_name(pecan.request.context, allocation_ident) raise exception.InvalidUuidOrName(name=allocation_ident)
def get_rpc_deploy_template(template_ident): """Get the RPC deploy template from the UUID or logical name. :param template_ident: the UUID or logical name of a deploy template. :returns: The RPC deploy template. :raises: InvalidUuidOrName if the name or uuid provided is not valid. :raises: DeployTemplateNotFound if the deploy template is not found. """ # Check to see if the template_ident is a valid UUID. If it is, treat it # as a UUID. if uuidutils.is_uuid_like(template_ident): return objects.DeployTemplate.get_by_uuid(api.request.context, template_ident) # We can refer to templates by their name if utils.is_valid_logical_name(template_ident): return objects.DeployTemplate.get_by_name(api.request.context, template_ident) raise exception.InvalidUuidOrName(name=template_ident)
def get_rpc_deploy_template(template_ident): """Get the RPC deploy template from the UUID or logical name. :param template_ident: the UUID or logical name of a deploy template. :returns: The RPC deploy template. :raises: InvalidUuidOrName if the name or uuid provided is not valid. :raises: DeployTemplateNotFound if the deploy template is not found. """ # Check to see if the template_ident is a valid UUID. If it is, treat it # as a UUID. if uuidutils.is_uuid_like(template_ident): return objects.DeployTemplate.get_by_uuid(pecan.request.context, template_ident) # We can refer to templates by their name if utils.is_valid_logical_name(template_ident): return objects.DeployTemplate.get_by_name(pecan.request.context, template_ident) raise exception.InvalidUuidOrName(name=template_ident)
def get(cls, context, port_id): """Find a port. Find a port based on its id or uuid or name or MAC address and return a Port object. :param context: Security context :param port_id: the id *or* uuid *or* name *or* MAC address of a port. :returns: a :class:`Port` object. :raises: InvalidIdentity """ if strutils.is_int_like(port_id): return cls.get_by_id(context, port_id) elif uuidutils.is_uuid_like(port_id): return cls.get_by_uuid(context, port_id) elif netutils.is_valid_mac(port_id): return cls.get_by_address(context, port_id) elif utils.is_valid_logical_name(port_id): return cls.get_by_name(context, port_id) else: raise exception.InvalidIdentity(identity=port_id)
def is_valid_logical_name(name): """Determine if the provided name is a valid hostname.""" if pecan.request.version.minor < versions.MINOR_10_UNRESTRICTED_NODE_NAME: return utils.is_hostname_safe(name) else: return utils.is_valid_logical_name(name)
def is_valid_logical_name(name): """Determine if the provided name is a valid hostname.""" if pecan.request.version.minor < 10: return utils.is_hostname_safe(name) else: return utils.is_valid_logical_name(name)