def test_pathmap_schema(self): response = self.client.get("/api/v1/pathmaps/schema") self.assert_ok(response) schema = PathMap.to_schema() schema["tag"] = "VARCHAR(64)" del schema["tag_id"] self.assertEqual(response.json, schema)
def schema(): """ Returns the basic schema of :class:`.Agent` .. http:get:: /api/v1/pathmaps/schema HTTP/1.1 **Request** .. sourcecode:: http GET /api/v1/pathmaps/schema HTTP/1.1 Accept: application/json **Response** .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "id": "INTEGER", "path_linux": "VARCHAR(512)", "path_windows": "VARCHAR(512)", "path_osx": "VARCHAR(512)", "tag": "VARCHAR(64)" } :statuscode 200: no error """ out = PathMap.to_schema() del out["tag_id"] out["tag"] = "VARCHAR(%s)" % config.get("max_tag_length") return jsonify(out)
def post(self): """ A ``POST`` to this endpoint will create a new path map. A path map will list the equivalent path prefixes for all three supported families of operating systems, Linux, Windows and OS X. A path map can optionally be restricted to one tag, in which case it will only apply to agents with that tag. If a tag is specified that does not exist yet, that tag will be transparently created. .. http:post:: /api/v1/pathmaps/ HTTP/1.1 **Request** .. sourcecode:: http POST /api/v1/pathmaps/ HTTP/1.1 Accept: application/json { "path_linux": "/mnt/nfs", "path_windows": "\\domain\cifs_server", "path_osx": "/mnt/nfs", "tag": "production" } **Response** .. sourcecode:: http HTTP/1.1 201 CREATED Content-Type: application/json { "id": 1, "path_linux": "/mnt/nfs", "path_windows": "\\domain\cifs_server", "path_osx": "/mnt/nfs", "tag": "production" } :statuscode 201: a new pathmap was created :statuscode 400: there was something wrong with the request (such as invalid columns being included) """ tagname = g.json.pop("tag", None) if tagname: if not isinstance(tagname, STRING_TYPES): return jsonify(error="tag must be of type string"), BAD_REQUEST pathmap = PathMap(**g.json) if tagname: tag = Tag.query.filter_by(tag=tagname).first() if not tag: tag = Tag(tag=tagname) db.session.add(tag) pathmap.tag = tag db.session.add(pathmap) db.session.commit() out = pathmap.to_dict(unpack_relationships=False) if pathmap.tag: out["tag"] = pathmap.tag.tag logger.info("New pathmap created with values: %r", pathmap) return jsonify(out), CREATED
def post(self, pathmap_id): """ A ``POST`` to this endpoint will update an existing path map with new values. Only the values included in the request will be updated. The rest will be left unchanged. The id column cannot be changed. Including it in the request will lead to an error. .. http:post:: /api/v1/pathmaps/<int:pathmap_id> HTTP/1.1 **Request** .. sourcecode:: http POST /api/v1/pathmaps/1 HTTP/1.1 Accept: application/json { "path_linux": "/mnt/smb" } **Response** .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "id": 1, "path_linux": "/mnt/smb", "path_windows": "\\domain\cifs_server", "path_osx": "/mnt/nfs", "tag": "production" } :statuscode 200: the specified pathmap was updated :statuscode 404: the specified pathmap does not exist :statuscode 400: there was something wrong with the request (such as invalid columns being included) """ pathmap = PathMap.query.filter_by(id=pathmap_id).first() if not pathmap: return jsonify(error="No pathmap with that id"), NOT_FOUND if "id" in g.json: return (jsonify(error="ID column cannot be included in the request"), BAD_REQUEST) tagname = g.json.pop("tag", None) if tagname: tag = Tag.query.filter_by(tag=tagname).first() if not tag: tag = Tag(tag=tagname) db.session.add(tag) pathmap.tag = tag for name in PathMap.types().columns: if name in g.json: expected_type = PathMap.types().mappings[name] value = g.json.pop(name) if not isinstance(value, expected_type): return ( jsonify( error="Column `%s` is of type %r, but we " "expected %r" % (name, type(value), expected_type) ), BAD_REQUEST, ) setattr(pathmap, name, value) if g.json: return jsonify(error="Unknown columns: %r" % g.json), BAD_REQUEST db.session.add(pathmap) db.session.commit() out = pathmap.to_dict(unpack_relationships=False) if pathmap.tag: out["tag"] = pathmap.tag.tag del out["tag_id"] logger.info("Pathmap with id %s was updated, new data: %r", pathmap_id, out) return jsonify(out), OK