maximesson | 98f822c | 2019-12-11 16:53:33 +0100 | [diff] [blame] | 1 | import connexion |
| 2 | import fileinput |
| 3 | import json |
| 4 | import sys |
| 5 | |
| 6 | from flask import Flask, escape, request, make_response |
maximesson | 0aeab56 | 2020-01-27 18:05:04 +0100 | [diff] [blame] | 7 | from jsonschema import validate |
| 8 | from var_declaration import policy_instances, policy_types, policy_status, policy_type_per_instance |
maximesson | 98f822c | 2019-12-11 16:53:33 +0100 | [diff] [blame] | 9 | |
| 10 | app = connexion.App(__name__, specification_dir='.') |
| 11 | |
| 12 | @app.route('/policytypes/<string:policyTypeId>', methods=['PUT','DELETE']) |
| 13 | def policy_type(policyTypeId): |
| 14 | if request.method == 'PUT': |
| 15 | data = request.data.decode("utf-8") |
| 16 | data = data.replace("'", "\"") |
| 17 | data = json.loads(data) |
| 18 | policy_types[policyTypeId] = data |
| 19 | return ('The policy type was either created or updated for policy type id: ' + policyTypeId) |
| 20 | elif request.method == 'DELETE': |
| 21 | if policyTypeId in policy_types.keys(): |
| 22 | policy_types.pop(policyTypeId) |
| 23 | return make_response("policy type successfully deleted for policy type id: " + policyTypeId, 200) |
| 24 | else: |
| 25 | return make_response("No policy type defined for the specified id", 404) |
| 26 | |
| 27 | @app.route('/', methods=['GET']) |
| 28 | def test(): |
maximesson | 0aeab56 | 2020-01-27 18:05:04 +0100 | [diff] [blame] | 29 | return("Everything is fine", 200) |
maximesson | 98f822c | 2019-12-11 16:53:33 +0100 | [diff] [blame] | 30 | |
| 31 | @app.route('/deleteinstances', methods=['DELETE']) |
| 32 | def delete_instances(): |
| 33 | global policy_instances |
| 34 | global policy_status |
maximesson | 0aeab56 | 2020-01-27 18:05:04 +0100 | [diff] [blame] | 35 | global policy_type_per_instance |
maximesson | 98f822c | 2019-12-11 16:53:33 +0100 | [diff] [blame] | 36 | policy_instances.clear() |
| 37 | policy_status.clear() |
maximesson | 0aeab56 | 2020-01-27 18:05:04 +0100 | [diff] [blame] | 38 | policy_type_per_instance.clear() |
maximesson | 98f822c | 2019-12-11 16:53:33 +0100 | [diff] [blame] | 39 | return("All policy instances deleted", 200) |
| 40 | |
| 41 | @app.route('/deletetypes', methods=['DELETE']) |
| 42 | def delete_types(): |
| 43 | global policy_types |
| 44 | policy_types.clear() |
| 45 | return("All policy types deleted", 200) |
| 46 | |
| 47 | @app.route('/<string:policyId>/<string:enforceStatus>', methods=['PUT']) |
| 48 | def set_status(policyId, enforceStatus): |
| 49 | if policyId in policy_instances.keys(): |
maximesson | 0aeab56 | 2020-01-27 18:05:04 +0100 | [diff] [blame] | 50 | if policy_type_per_instance[policyId] == "UNDEFINED": |
maximesson | 98f822c | 2019-12-11 16:53:33 +0100 | [diff] [blame] | 51 | ps = {} |
| 52 | ps["policyId"] = policyId |
| 53 | ps["enforceStatus"] = enforceStatus |
maximesson | 98f822c | 2019-12-11 16:53:33 +0100 | [diff] [blame] | 54 | else: |
maximesson | 0aeab56 | 2020-01-27 18:05:04 +0100 | [diff] [blame] | 55 | policy_type_id = policy_type_per_instance[policyId] |
| 56 | status_schema = policy_types[policy_type_id]["statusSchema"] |
| 57 | ps = {} |
| 58 | ps["policyId"] = policyId |
| 59 | ps["enforceStatus"] = enforceStatus |
| 60 | try: |
| 61 | validate(instance=ps, schema=status_schema) |
| 62 | except: |
| 63 | return(set_error(None, "The json does not validate against the status schema.", 400, None, None, None, None, None)) |
| 64 | policy_status.pop(policyId) |
| 65 | policy_status[policyId] = ps |
| 66 | return("Status updated for policy: " + policyId, 200) |
maximesson | 98f822c | 2019-12-11 16:53:33 +0100 | [diff] [blame] | 67 | |
| 68 | @app.route('/<string:policyId>/<string:enforceStatus>/<string:enforceReason>', methods=['PUT']) |
| 69 | def set_status_with_reason(policyId, enforceStatus, enforceReason): |
| 70 | if policyId in policy_instances.keys(): |
maximesson | 0aeab56 | 2020-01-27 18:05:04 +0100 | [diff] [blame] | 71 | if policy_type_per_instance[policyId] == "UNDEFINED": |
| 72 | ps = {} |
| 73 | ps["policyId"] = policyId |
| 74 | ps["enforceStatus"] = enforceStatus |
| 75 | ps["enforceReason"] = enforceReason |
maximesson | 98f822c | 2019-12-11 16:53:33 +0100 | [diff] [blame] | 76 | else: |
maximesson | 0aeab56 | 2020-01-27 18:05:04 +0100 | [diff] [blame] | 77 | policy_type_id = policy_type_per_instance[policyId] |
| 78 | status_schema = policy_types[policy_type_id]["statusSchema"] |
| 79 | ps = {} |
| 80 | ps["policyId"] = policyId |
| 81 | ps["enforceStatus"] = enforceStatus |
| 82 | ps["enforceReason"] = enforceReason |
| 83 | try: |
| 84 | validate(instance=ps, schema=status_schema) |
| 85 | except: |
| 86 | return(set_error(None, "The json does not validate against the status schema.", 400, None, None, None, None, None)) |
| 87 | policy_status.pop(policyId) |
| 88 | policy_status[policyId] = ps |
| 89 | return("Status updated for policy: " + policyId, 200) |
maximesson | 98f822c | 2019-12-11 16:53:33 +0100 | [diff] [blame] | 90 | |
BjornMagnussonXA | 6f1dc48 | 2020-03-10 16:32:08 +0100 | [diff] [blame] | 91 | #Metrics function |
| 92 | |
| 93 | @app.route('/counter/<string:countername>', methods=['GET']) |
| 94 | def getCounter(countername): |
| 95 | if (countername == "num_instances"): |
| 96 | return str(len(policy_instances)),200 |
| 97 | elif (countername == "num_types"): |
| 98 | return str(len(policy_types)),200 |
| 99 | else: |
| 100 | return "Counter name: "+countername+" not found.",404 |
| 101 | |
| 102 | |
maximesson | 98f822c | 2019-12-11 16:53:33 +0100 | [diff] [blame] | 103 | port_number = 8085 |
| 104 | if len(sys.argv) >= 2: |
| 105 | if isinstance(sys.argv[1], int): |
| 106 | port_number = sys.argv[1] |
| 107 | |
| 108 | app.add_api('a1-openapi.yaml') |
| 109 | app.run(port=port_number) |
| 110 | |