def before_data_load(cls, data): """Ensure that all checks exist.""" if "checks" in data: for check_id in data["checks"]: try: CheckController.get(filters={"Check": {"id": check_id}}) except NotFoundError: raise NotFoundError("Check {} not found".format(check_id)) # Transform ID into objects data["checks"] = CheckController._list({"Check": {"id": data["checks"]}})
def list_team_checks(team_id): """List the checks of a team. .. :quickref: GET; List the checks of a team. **Example request**: .. sourcecode:: http GET /teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/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) checks = CheckController.list_team_checks(team_id) return jsonify({"checks": [format_check(c) for c in checks]}), 200
def _create_check(name, source_id, type='Threshold', parameters={}): with app.app_context(): return CheckController.create({ 'source_id': source_id, 'name': name, 'type': type, 'parameters': parameters })
def delete_source_check(team_id, source_id, check_id): """ .. :quickref: DELETE; Lorem ipsum.""" if not TeamPermission.is_manager_or_editor(team_id): abort(403) check = CheckController.delete( filters={"Check": {"id": check_id, "source_id": source_id}} ) return jsonify(format_check(check)), 200
def put_source_check(team_id, source_id, check_id): """ .. :quickref: PUT; Lorem ipsum.""" if not TeamPermission.is_manager_or_editor(team_id): abort(403) payload = get_payload() check = CheckController.update( payload, {"Check": {"id": check_id, "source_id": source_id}} ) return jsonify(format_check(check)), 200
def update_checks(cls, rule_id, checks_id): rule = cls._get(filters={"Rule": {"id": rule_id}}) checks = [] for check_id in checks_id: check = CheckController._get(filters={"Check": {"id": check_id}}) checks.append(check) rule.checks = checks db.session.commit() return cls.resource_to_dict(rule)
def post_source_check(team_id, source_id): """Add a new check. .. :quickref: POST; Add a new check. **Example request**: .. sourcecode:: http POST /v1/teams/66859c4a-3e0a-4968-a5a4-4c3b8662acb7/sources/e2c1c635-2d7c-4881-83d1-e4e7027ac7a2/checks HTTP/1.1 Host: example.com Accept: application/json { "name": "My check", "type": "Threshold", "parameters": { "metric": "foo", "threshold": 100 } } **Example response**: .. sourcecode:: http HTTP/1.1 201 CREATED { 'name': 'My check', 'parameters': { 'metric': 'foo', 'threshold': 100 }, 'type': 'Threshold' } :resheader Content-Type: application/json :status 201: the created check """ if not TeamPermission.is_manager_or_editor(team_id): abort(403) payload = get_payload() payload["source_id"] = source_id source = CheckController.create(payload) return jsonify(format_check(source)), 201
def resource_to_dict(cls, obj, blacklist=False): d = super().resource_to_dict(obj, blacklist=False) d["checks"] = [CheckController.resource_to_dict(c) for c in obj.checks] return d