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 } }, "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"} }, "required": ["hostname", "port"], "additionalProperties": False }, "sentinel": { "type": "object", "properties": { "hostname": {"type": "string"}, "port": {"type": "integer"}, "name": {"type": "string"} }, "required": ["hostname", "port"], "additionalProperties": False }, "junosspace": { "api": {"type": "string"}, "username": {"type": "string"}, "password": {"type": "string"} } }, "oneOf": [ { "required": [ "ops-db", "ssh", "redis", "junosspace"] }, { "required": [ "ops-db", "ssh", "sentinel", "junosspace"] } ], "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