Skip to content
Snippets Groups Projects
config.py 4.89 KiB
import json
import jsonschema

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

    "definitions": {
        "timeout": {
            "type": "number",
            "maximum": 60,  # sanity
            "exclusiveMinimum": 0
        },
        "database-credentials": {
            "type": "object",
            "properties": {
                "hostname": {"type": "string"},
                "dbname": {"type": "string"},
                "username": {"type": "string"},
                "password": {"type": "string"}
            },
            "required": ["hostname", "dbname", "username", "password"],
            "additionalProperties": False
        },
        "ssh-credentials": {
            "type": "object",
            "properties": {
                "username": {"type": "string"},
                "private-key": {"type": "string"},
                "known-hosts": {"type": "string"}
            },
            "required": ["private-key", "known-hosts"],
            "additionalProperties": False
        },
        "ims": {
            "type": "object",
            "properties": {
                "api": {"type": "string"},
                "username": {"type": "string"},
                "password": {"type": "string"}
            },
            "required": ["api", "username", "password"],
            "additionalProperties": False
        },
        "otrs-export": {
            "type": "object",
            "properties": {
                "username": {"type": "string"},
                "private-key": {"type": "string"},
                "known-hosts": {"type": "string"},
                "destination": {"type": "string"}
            },
            "required": [
                "username",
                "private-key",
                "known-hosts",
                "destination"
            ],
            "additionalProperties": False
        },
        "redis-credentials": {
            "type": "object",
            "properties": {
                "hostname": {"type": "string"},
                "port": {"type": "integer"},
                "celery-db-index": {"type": "integer"},
                "socket_timeout": {"$ref": "#/definitions/timeout"}
            },
            "required": ["hostname", "port"],
            "additionalProperties": False
        },
        "redis-sentinel-config": {
            "type": "object",
            "properties": {
                "hostname": {"type": "string"},
                "port": {"type": "integer"},
                "celery-db-index": {"type": "integer"},
                "name": {"type": "string"},
                "redis_socket_timeout": {"$ref": "#/definitions/timeout"},
                "sentinel_socket_timeout": {"$ref": "#/definitions/timeout"}
            },
            "required": ["hostname", "port", "name"],
            "additionalProperties": False
        },
        "interface-address": {
            "type": "object",
            "properties": {
                "address": {"type": "string"},
                "network": {"type": "string"},
                "interface": {"type": "string"},
                "router": {"type": "string"}
            },
            "required": ["address", "network", "interface", "router"],
            "additionalProperties": False
        }
    },

    "type": "object",
    "properties": {
        "ops-db": {"$ref": "#/definitions/database-credentials"},
        "ssh": {"$ref": "#/definitions/ssh-credentials"},
        "redis": {"$ref": "#/definitions/redis-credentials"},
        "sentinel": {"$ref": "#/definitions/redis-sentinel-config"},
        "ims": {"$ref": "#/definitions/ims"},
        "otrs-export": {"$ref": "#/definitions/otrs-export"},
        "redis-databases": {
            "type": "array",
            "minItems": 1,
            "items": {"type": "integer"}
        },
        "managed-routers": {"type": "string"},
        "unmanaged-interfaces": {
            "type": "array",
            "items": {"$ref": "#/definitions/interface-address"}
        }
    },
    "oneOf": [
        {
            "required": [
                "ops-db",
                "ssh",
                "redis",
                "redis-databases",
                "otrs-export",
                "ims",
                "managed-routers"]
        },
        {
            "required": [
                "ops-db",
                "ssh",
                "sentinel",
                "redis-databases",
                "otrs-export",
                "ims",
                "managed-routers"]
        }
    ],
    "additionalProperties": False
}


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

    Input is validated against this jsonschema:

    .. asjson:: inventory_provider.config.CONFIG_SCHEMA

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