コード例 #1
0
ファイル: SchemaResource.py プロジェクト: Firstyear/rest389
def schema_attribute(ds, attribute_name):

    if request.method == 'OPTIONS':
        #
        # Return a list of the allowed methods
        #
        msg = {'allow': 'GET PUT DELETE OPTIONS HEAD'}
        return jsonify(msg)

    try:
        result = ds.schema.query_attributetype(attribute_name)
    except ldap.LDAPError as e:
        return jsonResponse("Error getting attribute: " % str(e), 403,
                            request.url)

    if request.method == 'HEAD':
        #
        # Return the HEAD Response
        #
        if result is None:
            return jsonResponse("", 404, request.url)
        return jsonResponse("", 200, request.url)

    if request.method == 'GET':
        if result is None:
            return jsonResponse("Failed to find attribute", 404, request.url)
        # Split the tuple
        attribute, must, may = result
        results = {'names': attribute.names,
                   'desc': attribute.desc,
                   'oid': attribute.oid,
                   'ordering': attribute.ordering,
                   'single_value': attribute.single_value,
                   'equality': attribute.equality,
                   'substr': attribute.substr,
                   'syntax': attribute.syntax,
                   'usage': attribute.usage,
                   'obsolete': attribute.obsolete,
                   'no_user_mod': attribute.no_user_mod,
                   'sup': attribute.sup,
                   'must': map(lambda x: x.names[0], must),
                   'may': map(lambda x: x.names[0], may)}
        return jsonResponse(results, 200, request.url)

    if request.method == 'PUT':
        json_req = request.get_json(force=True)
        if not json_req:
            return jsonResponse("JSON representation missing", 403,
                                request.url)
        newattr = jsonAttrToStr(json_req)

        attrObj = Schema(ds)
        try:
            attrObj.add_attribute(newattr)
        except ldap.TYPE_OR_VALUE_EXISTS:
            # idempotent - return the entry as is
            return jsonResponse(json_req, 201,
                                request.url + '/' + json_req['names'][0])

        except ldap.LDAPError as e:
            return jsonResponse('Failed to add attribute: %s' % str(e), 403,
                                request.url)

        return jsonResponse(json_req, 201,
                            request.url + '/' + json_req['names'][0])

    if request.method == 'DELETE':
        schemaObj = Schema(ds)
        try:
            delete_val = None
            name_cmp = "'%s'" % attribute_name

            entry = schemaObj.get_entry()
            attributes = entry.getValues('attributeTypes')
            for val in attributes:
                if name_cmp.lower() in val.lower():
                    delete_val = val
                    break

            if delete_val:
                ds.modify_s(DN_SCHEMA, [(ldap.MOD_DELETE, 'attributeTypes',
                            delete_val)])
                return jsonResponse('', 200, request.url)
            else:
                # Idempotent - no val, already deleted, return success
                return jsonResponse('', 200, request.url)

        except ldap.LDAPError as e:
            return jsonResponse('Failed to delete attribute: %s' % str(e), 403,
                                request.url)