Skip to content
Snippets Groups Projects
config.py 3.39 KiB
Newer Older
import json

import jsonschema

CONFIG_SCHEMA = {
    "$schema": "http://json-schema.org/draft-07/schema#",

    "definitions": {
        "database_credentials": {
            "type": "object",
            "properties": {
                "hostname": {"type": "string"},
                "dbname": {"type": "string"},
                "username": {"type": "string"},
                "password": {"type": "string"}
            },
            "required": ["hostname", "dbname", "username", "password"],
            "additionalProperties": False
        },
        "timeout": {
            "type": "number",
            "maximum": 10,  # sanity
            "exclusiveMinimum": 0
        }
    },

    "type": "object",
    "properties": {
        "ops-db": {"$ref": "#/definitions/database_credentials"},
        "ssh": {
            "type": "object",
            "properties": {
                "username": {"type": "string"},
                "private-key": {"type": "string"},
                "known-hosts": {"type": "string"}
            },
            "required": ["private-key", "known-hosts"],
            "additionalProperties": False
        },
        "redis": {
            "type": "object",
            "properties": {
                "hostname": {"type": "string"},
                "port": {"type": "integer"},
                "socket_timeout": {"$ref": "#/definitions/timeout"}
            "required": ["hostname", "port"],
            "additionalProperties": False
        "sentinel": {
            "type": "object",
            "properties": {
                "hostname": {"type": "string"},
                "port": {"type": "integer"},
                "name": {"type": "string"},
Erik Reid's avatar
Erik Reid committed
                "redis_socket_timeout": {"$ref": "#/definitions/timeout"},
                "sentinel_socket_timeout": {"$ref": "#/definitions/timeout"}
            "required": ["hostname", "port", "name"],
            "additionalProperties": False
        },
        "redis-databases": {
            "type": "array",
            "minItems": 1,
            "items": {"type": "integer"}
        },
        "junosspace": {
            "api": {"type": "string"},
            "username": {"type": "string"},
            "password": {"type": "string"}
        },
        "otrs-export": {
            "username": {"type": "string"},
            "private-key": {"type": "string"},
            "destination": {"type": "string"},
            "known-hosts": {"type": "string"}
        },
        "ims": {
            "api": {"type": "string"},
            "username": {"type": "string"},
            "password": {"type": "string"}
    "oneOf": [
        {
            "required": [
                "ops-db",
                "ssh",
                "redis",
                "redis-databases",
                "otrs-export",
        },
        {
            "required": [
                "ops-db",
                "ssh",
                "sentinel",
                "redis-databases",
                "otrs-export",
    "additionalProperties": False
}


def load(f):
    """
    loads, validates and returns configuration parameters

    :param f: file-like object that produces the config file
    :return:
    """
    config = json.loads(f.read())
    jsonschema.validate(config, CONFIG_SCHEMA)
    return config