示例#1
0
def check_string_length(value, name=None, min_len=0, max_len=None):
    """Check the length of specified string

    :param value: the value of the string
    :param name: the name of the string
    :param min_len: the minimum length of the string
    :param max_len: the maximum length of the string

    """
    if not isinstance(value, six.string_types):
        if name is None:
            msg = _("The input is not a string or unicode")
        else:
            msg = _("%s is not a string or unicode") % name
        raise t_exceptions.InvalidInput(message=msg)

    if name is None:
        name = value

    if len(value) < min_len:
        msg = _("%(name)s has a minimum character requirement of "
                "%(min_length)s.") % {
                    'name': name,
                    'min_length': min_len
                }
        raise t_exceptions.InvalidInput(message=msg)

    if max_len and len(value) > max_len:
        msg = _("%(name)s has more than %(max_length)s "
                "characters.") % {
                    'name': name,
                    'max_length': max_len
                }
        raise t_exceptions.InvalidInput(message=msg)
示例#2
0
 def prepare_bottom_external_subnet_by_bottom_name(self, context, subnet,
                                                   region_name, b_net_name,
                                                   top_subnet_id):
     t_ctx = t_context.get_context_from_neutron_context(context)
     pod = db_api.get_pod_by_name(t_ctx, region_name)
     b_client = self._get_client(region_name)
     bottom_network = b_client.list_networks(t_ctx, [{
         'key': 'name',
         'comparator': 'eq',
         'value': b_net_name
     }])
     if not bottom_network:
         raise t_exceptions.InvalidInput(
             reason='bottom network not found for %(b_net_name)s' %
             {'b_net_name': b_net_name})
     body = {
         'subnet': {
             'name': top_subnet_id,
             'network_id': bottom_network[0]['id'],
             'tenant_id': subnet['tenant_id']
         }
     }
     attrs = ('ip_version', 'cidr', 'gateway_ip', 'allocation_pools',
              'enable_dhcp')
     for attr in attrs:
         if validators.is_attr_set(subnet.get(attr)):
             body['subnet'][attr] = subnet[attr]
     self.prepare_bottom_element(t_ctx, subnet['tenant_id'], pod,
                                 {'id': top_subnet_id},
                                 t_constants.RT_SUBNET, body)
示例#3
0
def find_pod_by_az_or_region(context, az_or_region):
    pods = find_pods_by_az_or_region(context, az_or_region)

    # if pods is None, returning None value directly.
    if pods is None:
        return None
    # if no pod is matched, then we will raise an exception
    if len(pods) < 1:
        raise exceptions.PodNotFound(az_or_region)
    # if the pods list only contain one pod, then this pod will be
    # returned back
    if len(pods) == 1:
        return pods[0]
    # if the pods list contains more than one pod, then we will raise an
    # exception
    if len(pods) > 1:
        raise exceptions.InvalidInput(
            reason='Multiple pods with the same az_name are found')