|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Copyright (c) 2025 mundialis GmbH & Co. KG. |
| 3 | +
|
| 4 | +This program is free software: you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU General Public License as published by |
| 6 | +the Free Software Foundation, either version 3 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +
|
| 9 | +This program is distributed in the hope that it will be useful, |
| 10 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +GNU General Public License for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU General Public License |
| 15 | +along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | +
|
| 17 | +Hello World class |
| 18 | +""" |
| 19 | + |
| 20 | +__license__ = "GPLv3" |
| 21 | +__author__ = "Carmen Tawalika" |
| 22 | +__copyright__ = "Copyright 2025 mundialis GmbH & Co. KG" |
| 23 | +__maintainer__ = "mundialis GmbH & Co. KG" |
| 24 | + |
| 25 | +import json |
| 26 | + |
| 27 | +import requests |
| 28 | +from cloudevents.conversion import to_binary |
| 29 | +from cloudevents.http import CloudEvent |
| 30 | +from flask import jsonify, make_response, request |
| 31 | +from flask_restful_swagger_2 import Resource, swagger |
| 32 | + |
| 33 | +from actinia_cloudevent_plugin.apidocs import hooks |
| 34 | +from actinia_cloudevent_plugin.model.response_models import ( |
| 35 | + SimpleStatusCodeResponseModel, |
| 36 | +) |
| 37 | +from actinia_cloudevent_plugin.resources.config import EVENTRECEIVER |
| 38 | + |
| 39 | + |
| 40 | +class Hooks(Resource): |
| 41 | + """Webhook handling.""" |
| 42 | + |
| 43 | + def get(self, source_name): |
| 44 | + """Cloudevent get method: not allowed response.""" |
| 45 | + _source_name = source_name |
| 46 | + res = jsonify( |
| 47 | + SimpleStatusCodeResponseModel( |
| 48 | + status=405, |
| 49 | + message="Method Not Allowed", |
| 50 | + ), |
| 51 | + ) |
| 52 | + return make_response(res, 405) |
| 53 | + |
| 54 | + def head(self, source_name): |
| 55 | + """Cloudevent head method: return empty response.""" |
| 56 | + _source_name = source_name |
| 57 | + return make_response("", 200) |
| 58 | + |
| 59 | + @swagger.doc(hooks.describe_hooks_post_docs) |
| 60 | + def post(self, source_name) -> SimpleStatusCodeResponseModel: |
| 61 | + """Translate actinia webhook call to cloudevent. |
| 62 | +
|
| 63 | + This method is called by HTTP POST actinia-core webhook |
| 64 | + """ |
| 65 | + # only actinia as source supported so far |
| 66 | + if source_name != "actinia": |
| 67 | + return make_response( |
| 68 | + jsonify( |
| 69 | + SimpleStatusCodeResponseModel( |
| 70 | + status=400, |
| 71 | + message="Bad Request: Source name not 'actinia'", |
| 72 | + ), |
| 73 | + ), |
| 74 | + 400, |
| 75 | + ) |
| 76 | + |
| 77 | + postbody = request.get_json(force=True) |
| 78 | + |
| 79 | + if type(postbody) is dict: |
| 80 | + postbody = json.dumps(postbody) |
| 81 | + elif not isinstance(postbody, str): |
| 82 | + postbody = str(postbody) |
| 83 | + |
| 84 | + resp = json.loads(postbody) |
| 85 | + if "resource_id" not in resp: |
| 86 | + return make_response( |
| 87 | + jsonify( |
| 88 | + SimpleStatusCodeResponseModel( |
| 89 | + status=400, |
| 90 | + message="Bad Request: No resource_id found in request", |
| 91 | + ), |
| 92 | + ), |
| 93 | + 400, |
| 94 | + ) |
| 95 | + |
| 96 | + # TODO: define when to send cloudevent |
| 97 | + status = resp["status"] |
| 98 | + if status == "finished": |
| 99 | + # TODO send cloudevent |
| 100 | + pass |
| 101 | + terminate_status = ["finished", "error", "terminated"] |
| 102 | + if status in terminate_status: |
| 103 | + # TODO send cloudevent |
| 104 | + pass |
| 105 | + |
| 106 | + # TODO: move to common function from core.cloudevents |
| 107 | + url = EVENTRECEIVER.url |
| 108 | + try: |
| 109 | + attributes = { |
| 110 | + "specversion": "1.0", |
| 111 | + "source": "/actinia-cloudevent-plugin", |
| 112 | + "type": "com.mundialis.actinia.process.status", |
| 113 | + "subject": "nc_spm_08/PERMANENT", |
| 114 | + "datacontenttype": "application/json", |
| 115 | + } |
| 116 | + data = {"actinia_job": resp} |
| 117 | + event = CloudEvent(attributes, data) |
| 118 | + headers, body = to_binary(event) |
| 119 | + requests.post(url, headers=headers, data=body) |
| 120 | + except ConnectionError as e: |
| 121 | + return f"Connection ERROR when returning cloudevent: {e}" |
| 122 | + except Exception() as e: |
| 123 | + return f"ERROR when returning cloudevent: {e}" |
| 124 | + |
| 125 | + res = jsonify( |
| 126 | + SimpleStatusCodeResponseModel( |
| 127 | + status=200, |
| 128 | + message="Thank you for your update", |
| 129 | + ), |
| 130 | + ) |
| 131 | + return make_response(res, 200) |
0 commit comments