def test_validate_uuid(self): invalid_uuids = [ None, 123, '123', 't5069610-744b-42a7-8bd8-ceac1a229cd4', 'e5069610-744bb-42a7-8bd8-ceac1a229cd4' ] for uuid in invalid_uuids: msg = validators.validate_uuid(uuid) error = "'%s' is not a valid UUID" % uuid self.assertEqual(error, msg) msg = validators.validate_uuid('00000000-ffff-ffff-ffff-000000000000') self.assertIsNone(msg)
def test_validate_uuid(self): invalid_uuids = [None, 123, '123', 't5069610-744b-42a7-8bd8-ceac1a229cd4', 'e5069610-744bb-42a7-8bd8-ceac1a229cd4'] for uuid in invalid_uuids: msg = validators.validate_uuid(uuid) error = "'%s' is not a valid UUID" % uuid self.assertEqual(error, msg) msg = validators.validate_uuid('00000000-ffff-ffff-ffff-000000000000') self.assertIsNone(msg)
def create_project_net_partition_mapping(self, context, project_net_partition_mapping): session = context.session p2n = project_net_partition_mapping['project_net_partition_mapping'] project = p2n['project'] net_partition_id = p2n['net_partition_id'] err = validators.validate_uuid(project) if err: raise nuage_exc.NuageBadRequest(resource='net_partition', msg=err) # Validate netpartition netpart = nuagedb.get_net_partition_by_id(session, net_partition_id) if not netpart: msg = _('Net partition {} is not a valid netpartition ' 'ID.').format(net_partition_id) raise nuage_exc.NuageBadRequest( resource='project_net_partition_mapping', msg=msg) with session.begin(subtransactions=True): existing_mapping = nuagedb.get_project_net_partition_mapping( session, project) if existing_mapping: session.delete(existing_mapping) mapping = nuagedb.add_project_net_partition_mapping( session, net_partition_id, project) return self._make_project_net_partition_mapping(context, mapping)
def _validate_insert_remove_rule_request(self, rule_info): """Validate rule_info dict Check that all mandatory fields are present, otherwise raise proper exception. """ if not rule_info or 'firewall_rule_id' not in rule_info: raise f_exc.FirewallRuleInfoMissing() # Validator doesn't return anything if the check passes if validators.validate_uuid(rule_info['firewall_rule_id']): raise f_exc.FirewallRuleNotFound( firewall_rule_id=rule_info['firewall_rule_id'])
def _validate_subnets(self, context, subnet_ids): """Ensure UUIDs OK and subnets exist.""" for subnet_id in subnet_ids: msg = validators.validate_uuid(subnet_id) if msg: raise vpnaas.InvalidEndpointInEndpointGroup( group_type=constants.SUBNET_ENDPOINT, endpoint=subnet_id, why=_('Invalid UUID')) try: self.core_plugin.get_subnet(context, subnet_id) except nexception.SubnetNotFound: raise vpnaas.NonExistingSubnetInEndpointGroup(subnet=subnet_id)
def _validate_subnets(self, context, subnet_ids): """Ensure UUIDs OK and subnets exist.""" for subnet_id in subnet_ids: msg = validators.validate_uuid(subnet_id) if msg: raise vpnaas.InvalidEndpointInEndpointGroup( group_type=constants.SUBNET_ENDPOINT, endpoint=subnet_id, why=_('Invalid UUID')) try: self.core_plugin.get_subnet(context, subnet_id) except nexception.SubnetNotFound: raise vpnaas.NonExistingSubnetInEndpointGroup( subnet=subnet_id)
def validate_subports(data, valid_values=None): if not isinstance(data, list): msg = _("Invalid data format for subports: '%s' is not a list") % data LOG.debug(msg) return msg subport_ids = set() segmentations = collections.defaultdict(set) for subport in data: if not isinstance(subport, dict): msg = _("Invalid data format for subport: " "'%s' is not a dict") % subport LOG.debug(msg) return msg # Expect a non duplicated and valid port_id for the subport if 'port_id' not in subport: msg = _("A valid port UUID must be specified") LOG.debug(msg) return msg elif validators.validate_uuid(subport["port_id"]): msg = _("Invalid UUID for subport: '%s'") % subport["port_id"] return msg elif subport["port_id"] in subport_ids: msg = _("Non unique UUID for subport: '%s'") % subport["port_id"] return msg subport_ids.add(subport["port_id"]) # Validate that both segmentation id and segmentation type are # specified, and that the client does not duplicate segmentation # ids segmentation_id = subport.get("segmentation_id") segmentation_type = subport.get("segmentation_type") if (not segmentation_id or not segmentation_type) and len(subport) > 1: msg = _("Invalid subport details '%s': missing segmentation " "information. Must specify both segmentation_id and " "segmentation_type") % subport LOG.debug(msg) return msg if segmentation_id in segmentations.get(segmentation_type, []): msg = _("Segmentation ID '%(seg_id)s' for '%(subport)s' is not " "unique") % { "seg_id": segmentation_id, "subport": subport["port_id"] } LOG.debug(msg) return msg if segmentation_id: segmentations[segmentation_type].add(segmentation_id)
def validate_subports(data, valid_values=None): if not isinstance(data, list): msg = _("Invalid data format for subports: '%s' is not a list") % data LOG.debug(msg) return msg subport_ids = set() segmentations = collections.defaultdict(set) for subport in data: if not isinstance(subport, dict): msg = _("Invalid data format for subport: " "'%s' is not a dict") % subport LOG.debug(msg) return msg # Expect a non duplicated and valid port_id for the subport if 'port_id' not in subport: msg = _("A valid port UUID must be specified") LOG.debug(msg) return msg elif validators.validate_uuid(subport["port_id"]): msg = _("Invalid UUID for subport: '%s'") % subport["port_id"] return msg elif subport["port_id"] in subport_ids: msg = _("Non unique UUID for subport: '%s'") % subport["port_id"] return msg subport_ids.add(subport["port_id"]) # Validate that both segmentation id and segmentation type are # specified, and that the client does not duplicate segmentation # ids segmentation_id = subport.get("segmentation_id") segmentation_type = subport.get("segmentation_type") if (not segmentation_id or not segmentation_type) and len(subport) > 1: msg = _("Invalid subport details '%s': missing segmentation " "information. Must specify both segmentation_id and " "segmentation_type") % subport LOG.debug(msg) return msg if segmentation_id in segmentations.get(segmentation_type, []): msg = _("Segmentation ID '%(seg_id)s' for '%(subport)s' is not " "unique") % {"seg_id": segmentation_id, "subport": subport["port_id"]} LOG.debug(msg) return msg if segmentation_id: segmentations[segmentation_type].add(segmentation_id)
def delete_project_net_partition_mapping(self, context, project_id): session = context.session err = validators.validate_uuid(project_id) if err: raise nuage_exc.NuageBadRequest( resource='project_net_partition_mapping', msg=err) with session.begin(subtransactions=True): existing_mapping = nuagedb.get_project_net_partition_mapping( session, project_id) if existing_mapping: session.delete(existing_mapping) else: msg = _('Project {} does not currently ' 'have a default net-partition associated.').format( project_id) raise nuage_exc.NuageBadRequest( resource='project_net_partition_mapping', msg=msg)