#!/usr/bin/env python3
#
# Validate data for kafka
#

import pscheduler


ARCHIVER_SPEC_SCHEMA = {
    "local":{},
    "versions": {
        "1":{
            "type": "object",
            "properties": {
                "schema": { "type": "integer", "enum": [ 1 ] },
                "topic": {"$ref": "#/pScheduler/String"},
                "server": {"$ref": "#/pScheduler/String"}
            },
            "additionalProperties": False,
            "required": ["topic", "server"]
        },
        "2": {
            "type": "object",
            "properties": {
                "schema": { "type": "integer", "enum": [ 2 ] },
                "topic": {"$ref": "#/pScheduler/String"},
                "server": {"$ref": "#/pScheduler/String"},
                "archiver-id": {"$ref": "#/pScheduler/String"},
                "security-protocol": {"default": "PLAINTEXT", "enum": ["PLAINTEXT", "SSL"]},
                "kafka-retries": {"$ref": "#/pScheduler/Integer"},
                "retry-policy": {"$ref": "#/pScheduler/RetryPolicy"},
            },
            "additionalProperties": True,
            "required": ["schema", "topic", "server", "security-protocol"],
            "allOf": [
                {
                    "if": {
                        "properties": {"security-protocol": { "const": "SSL" }},
                        "required": ["security-protocol"]
                    },
                    "then": {
                        "properties": {
                            "_ssl-cacert": {"$ref": "#/pScheduler/String"},
                            "_ssl-cert": {"$ref": "#/pScheduler/String"},
                            "_ssl-key": {"$ref": "#/pScheduler/String"},
                            "_ssl-password": {"$ref": "#/pScheduler/String"},
                            "ssl-checkhostname": {"$ref": "#/pScheduler/Boolean"},
                        }
                    }
                }
            ]

        }
    }
}

try:
    json = pscheduler.json_load()
except ValueError as ex:
    pscheduler.succeed_json({"valid": False, "error": str(ex)})


valid, error = pscheduler.json_validate_from_standard_template(json, ARCHIVER_SPEC_SCHEMA)


if not valid:
    pscheduler.succeed_json({"valid": False, "error": error})

pscheduler.succeed_json({"valid": True})
