Exemple #1
0
 def _parse_request_items(req, allow_forbidden):
     ret = {}
     for key, val in req.GET.items():
         match = _QS_KEY_PATTERN.match(key)
         if not match:
             continue
         # `prefix` is 'resources', 'required', 'member_of', or 'in_tree'
         # `suffix` is an integer string, or None
         prefix, suffix = match.groups()
         suffix = suffix or ''
         if suffix not in ret:
             ret[suffix] = RequestGroup(use_same_provider=bool(suffix))
         request_group = ret[suffix]
         if prefix == _QS_RESOURCES:
             request_group.resources = util.normalize_resources_qs_param(
                 val)
         elif prefix == _QS_REQUIRED:
             request_group.required_traits = util.normalize_traits_qs_param(
                 val, allow_forbidden=allow_forbidden)
         elif prefix == _QS_MEMBER_OF:
             # special handling of member_of qparam since we allow multiple
             # member_of params at microversion 1.24.
             # NOTE(jaypipes): Yes, this is inefficient to do this when
             # there are multiple member_of query parameters, but we do this
             # so we can error out if someone passes an "orphaned" member_of
             # request group.
             # TODO(jaypipes): Do validation of query parameters using
             # JSONSchema
             request_group.member_of = util.normalize_member_of_qs_params(
                 req, suffix)
         elif prefix == _QS_IN_TREE:
             request_group.in_tree = util.normalize_in_tree_qs_params(val)
     return ret
def list_resource_providers(req):
    """GET a list of resource providers.

    On success return a 200 and an application/json body representing
    a collection of resource providers.
    """
    context = req.environ['placement.context']
    context.can(policies.LIST)
    want_version = req.environ[microversion.MICROVERSION_ENVIRON]

    schema = rp_schema.GET_RPS_SCHEMA_1_0
    if want_version.matches((1, 18)):
        schema = rp_schema.GET_RPS_SCHEMA_1_18
    elif want_version.matches((1, 14)):
        schema = rp_schema.GET_RPS_SCHEMA_1_14
    elif want_version.matches((1, 4)):
        schema = rp_schema.GET_RPS_SCHEMA_1_4
    elif want_version.matches((1, 3)):
        schema = rp_schema.GET_RPS_SCHEMA_1_3

    allow_forbidden = want_version.matches((1, 22))

    util.validate_query_params(req, schema)

    filters = {}
    # special handling of member_of qparam since we allow multiple member_of
    # params at microversion 1.24.
    if 'member_of' in req.GET:
        filters['member_of'], filters['forbidden_aggs'] = (
            util.normalize_member_of_qs_params(req))

    qpkeys = ('uuid', 'name', 'in_tree', 'resources', 'required')
    for attr in qpkeys:
        if attr in req.GET:
            value = req.GET[attr]
            if attr == 'resources':
                value = util.normalize_resources_qs_param(value)
            elif attr == 'required':
                value = util.normalize_traits_qs_param(
                    value, allow_forbidden=allow_forbidden)
            filters[attr] = value
    try:
        resource_providers = rp_obj.get_all_by_filters(context, filters)
    except exception.ResourceClassNotFound as exc:
        raise webob.exc.HTTPBadRequest(
            'Invalid resource class in resources parameter: %(error)s' %
            {'error': exc})
    except exception.TraitNotFound as exc:
        raise webob.exc.HTTPBadRequest(
            'Invalid trait(s) in request: %(error)s' % {'error': exc})

    response = req.response
    output, last_modified = _serialize_providers(
        req.environ, resource_providers, want_version)
    response.body = encodeutils.to_utf8(jsonutils.dumps(output))
    response.content_type = 'application/json'
    if want_version.matches((1, 15)):
        response.last_modified = last_modified
        response.cache_control = 'no-cache'
    return response