def delete_source(team_id, source_id): """Delete a source. .. :quickref: DELETE; Delete a source. **Example request**: .. sourcecode:: http DELETE /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/sources/e2c1c635-2d7c-4881-83d1-e4e7027ac7a2 HTTP/1.1 Host: example.com Accept: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK {} :resheader Content-Type: application/json :status 200: the source has been deleted """ if not TeamPermission.is_manager(team_id): abort(403) SourceController.delete( filters={"Source": { "id": source_id, "team_id": team_id }}) return jsonify({}), 200
def _create_source(name, team_id, plugin='Fake', conf={}): with app.app_context(): return SourceController.create({ 'team_id': team_id, 'name': name, 'plugin': plugin, 'configuration': conf })
def ensure_check(cls, obj): """Ensure check-source compatibility and validate configuration""" source = SourceController.get( filters={"Source": { "id": obj.source_id }}) plugin = BaseSource.load_source(source["plugin"], {}) plugin.validate_check_parameters(obj.type, obj.parameters)
def get_source(team_id, source_id): """Get a source from a team. .. :quickref: GET; Get a source from a team. **Example request**: .. sourcecode:: http GET /teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/sources/672d82d7-1970-48cd-a690-20f5daf303cf HTTP/1.1 Host: example.com Accept: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK { "checks": [{ "created_at": "2018-05-17T11:52:25Z", "id": "86ee5bdb-7088-400c-8654-a162f18b0710", "name": "Server Ping", "parameters": { "metric": "depc.tutorial.ping", "threshold": 20 }, "source_id": "672d82d7-1970-48cd-a690-20f5daf303cf", "type": "Threshold", "updated_at": "2018-11-12T17:41:11Z" }], "configuration": {}, "createdAt": "2018-05-16T11:38:46Z", "id": "672d82d7-1970-48cd-a690-20f5daf303cf", "name": "My source", "plugin": "Fake", "updatedAt": "2019-01-15T13:33:22Z" } :resheader Content-Type: application/json :status 200: the list of sources """ if not TeamPermission.is_user(team_id): abort(403) source = SourceController.get( filters={"Source": { "id": source_id, "team_id": team_id }}) return ( jsonify( format_source(source, config=TeamPermission.is_manager(team_id))), 200, )
def before_data_load(cls, data): """Ensure that the source and checks exist.""" if "source_id" in data: try: source = SourceController.get( filters={"Source": { "id": data["source_id"] }}) except NotFoundError: raise NotFoundError("Source {} not found".format( data["source_id"])) plugin = BaseSource.load_source(source["plugin"], {}) plugin.validate_check_parameters(data["type"], data["parameters"])
def put_source(team_id, source_id): """Edit an existing source. .. :quickref: PUT; Edit an existing source. **Example request**: .. sourcecode:: http PUT /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/sources/e2c1c635-2d7c-4881-83d1-e4e7027ac7a2 HTTP/1.1 Host: example.com Accept: application/json { "name": "My edited source" } **Example response**: .. sourcecode:: http HTTP/1.1 200 OK { "checks": [], "configuration": {}, "createdAt": "2019-01-21T14:08:22Z", "id": "e2c1c635-2d7c-4881-83d1-e4e7027ac7a2", "name": "My edited source", "plugin": "Fake", "updatedAt": "2019-01-21T14:21:24Z" } :resheader Content-Type: application/json :status 200: the edited source """ if not TeamPermission.is_manager(team_id): abort(403) payload = get_payload() source = SourceController.update( payload, {"Source": { "id": source_id, "team_id": team_id }}) return jsonify(format_source(source, True)), 200
def post_source(team_id): """Add a new source. .. :quickref: POST; Add a new source. **Example request**: .. sourcecode:: http POST /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/sources HTTP/1.1 Host: example.com Accept: application/json { "name": "My source", "plugin": "Fake", "configuration": {} } **Example response**: .. sourcecode:: http HTTP/1.1 201 CREATED { "checks": [], "configuration": {}, "createdAt": "2019-01-21T14:08:22Z", "id": "e2c1c635-2d7c-4881-83d1-e4e7027ac7a2", "name": "My source", "plugin": "Fake", "updatedAt": "2019-01-21T14:08:22Z" } :resheader Content-Type: application/json :status 201: the created source """ if not TeamPermission.is_manager(team_id): abort(403) payload = get_payload() payload["team_id"] = team_id source = SourceController.create(payload) return jsonify(format_source(source, True)), 201
def list_source_checks(team_id, source_id): """List the checks of a source. .. :quickref: GET; List the checks of a source. **Example request**: .. sourcecode:: http GET /teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/sources/e2c1c635-2d7c-4881-83d1-e4e7027ac7a2/checks HTTP/1.1 Host: example.com Accept: application/json **Example response**: .. sourcecode:: http HTTP/1.1 200 OK { 'checks': [{ 'name': 'My check', 'parameters': {'metric': 'foo', 'threshold': 100}, 'type': 'Threshold' }] } :resheader Content-Type: application/json :status 200: the list of checks """ if not TeamPermission.is_user(team_id): abort(403) team = SourceController.get( filters={"Source": { "id": source_id, "team_id": team_id }}) return jsonify({"checks": [format_check(c) for c in team["checks"]]}), 200
def ensure_check(cls, obj): name = obj.name # Name surrounded by quotes are prohibited if name.startswith(('"', "'")) or name.endswith(('"', "'")): raise IntegrityError("The check name cannot begin or end with a quote") # Ensure that the check does not exist in another source checks = cls._list(filters={"Check": {"name": name}}) source = SourceController._get(filters={"Source": {"id": obj.source_id}}) for check in checks: if check.id != obj.id and check.source.team_id == source.team_id: raise AlreadyExistError( "The check {name} already exists.", {"name": name} ) # Ensure the type field if ":" in obj.parameters["threshold"] and obj.type != "Interval": raise IntegrityError( "Threshold {} must be flagged as interval".format( obj.parameters["threshold"] ) )
def before_data_load(cls, data): if "source_id" in data: try: SourceController.get(filters={"Source": {"id": data["source_id"]}}) except NotFoundError: raise NotFoundError("Source {} not found".format(data["source_id"]))