Skip to content
Snippets Groups Projects
Select Git revision
  • baa43872ea7cbeca07c595f5e3a18e615905a8ae
  • develop default
  • master protected
  • feature/frontend-tests
  • 0.110
  • 0.109
  • 0.108
  • 0.107
  • 0.106
  • 0.105
  • 0.104
  • 0.103
  • 0.102
  • 0.101
  • 0.100
  • 0.99
  • 0.98
  • 0.97
  • 0.96
  • 0.95
  • 0.94
  • 0.93
  • 0.92
  • 0.91
24 results

helpers.py

Blame
  • config.py 9.41 KiB
    import json
    import jsonschema
    
    CONFIG_SCHEMA = {
        '$schema': 'https://json-schema.org/draft-07/schema#',
    
        'definitions': {
            'timeout': {
                'type': 'number',
                'maximum': 60,  # sanity
                'exclusiveMinimum': 0
            },
            'ssh-credentials': {
                'type': 'object',
                'properties': {
                    'username': {'type': 'string'},
                    'private-key': {'type': 'string'},
                    'known-hosts': {'type': 'string'}
                },
                'required': ['private-key', 'known-hosts'],
                'additionalProperties': False
            },
            'nokia-ssh-credentials': {
                'type': 'object',
                'properties': {
                    'username': {'type': 'string'},
                    'password': {'type': 'string'},
                    'hostkey_verify': {'type': 'boolean'},
                    'key_filename': {'type': 'string'}
                },
                'one-of': [
                    {
                        'required': ['username', 'password']
                    },
                    {
                        'required': ['username', 'key_filename']
                    },
                    {
                        'required': ['ssh_config']
                    }
                ],
                'additionalProperties': False
            },
            'ims': {
                'type': 'object',
                'properties': {
                    'api': {'type': 'string'},
                    'username': {'type': 'string'},
                    'password': {'type': 'string'},
                    'verify-ssl': {'type': 'boolean'},
                },
                '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'},
                    'password': {'type': 'string'},
                    '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'},
                    'password': {'type': 'string'},
                    '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
            },
            'oid': {
                'type': 'string',
                'pattern': r'^(\d+\.)*\d+$'
            },
            'gws-direct-counters': {
                'type': 'object',
                'properties': {
                    'discards_in': {'$ref': '#/definitions/oid'},
                    'discards_out': {'$ref': '#/definitions/oid'},
                    'errors_in': {'$ref': '#/definitions/oid'},
                    'errors_out': {'$ref': '#/definitions/oid'},
                    'traffic_in': {'$ref': '#/definitions/oid'},
                    'traffic_out': {'$ref': '#/definitions/oid'},
                },
                'additionalProperties': False
            },
            'gws-direct-interface': {
                'type': 'object',
                'properties': {
                    'info': {'type': 'string'},
                    'tag': {'type': 'string'},
                    'counters': {'$ref': '#/definitions/gws-direct-counters'}
                },
                'required': ['tag', 'counters'],
                'additionalProperties': False
            },
            'gws-direct-host-v2': {
                'type': 'object',
                'properties': {
                    'hostname': {'type': 'string'},
                    'community': {'type': 'string'},
                    'interfaces': {
                        'type': 'array',
                        'items': {'$ref': '#/definitions/gws-direct-interface'},
                        'minItems': 1
                    }
                },
                'required': ['hostname', 'community', 'interfaces'],
                'additionalProperties': False
            },
            'snmp-v3-cred': {
                'type': 'object',
                'properties': {
                    'protocol': {'enum': ['MD5', 'DES']},
                    'password': {'type': 'string'}
                },
                'required': ['protocol', 'password'],
                'additionalProperties': False
            },
            'gws-direct-host-v3': {
                'type': 'object',
                'properties': {
                    'hostname': {'type': 'string'},
                    'sec-name': {'type': 'string'},
                    'auth': {'$ref': '#/definitions/snmp-v3-cred'},
                    'priv': {'$ref': '#/definitions/snmp-v3-cred'},
                    'interfaces': {
                        'type': 'array',
                        'items': {'$ref': '#/definitions/gws-direct-interface'},
                        'minItems': 1
                    }
                },
                'required': ['hostname', 'sec-name', 'interfaces'],
                'additionalProperties': False
            },
            'gws-direct-nren-isp': {
                'type': 'object',
                'properties': {
                    'nren': {'type': 'string'},
                    'isp': {
                        'type': 'string',
                        'enum': ['Cogent', 'Telia', 'CenturyLink', 'GTT']
                    },
                    'hosts': {
                        'type': 'array',
                        'items': {
                            'oneOf': [
                                {'$ref': '#/definitions/gws-direct-host-v2'},
                                {'$ref': '#/definitions/gws-direct-host-v3'},
                            ]
                        },
                        'minItems': 1
                    }
                },
                'required': ['nren', 'isp', 'hosts'],
                'additionalProperties': False
            },
            'gws-direct': {
                'type': 'array',
                'items': {'$ref': '#/definitions/gws-direct-nren-isp'}
            },
            'nren-asn-map': {
                'type': 'array',
                'items': {
                    'type': 'object',
                    'properties': {
                      'nren': {'type': 'string'},
                      'asn': {'type': 'integer'}
                    },
                    'required': ['nren', 'asn'],
                    'additionalProperties': False
                },
            }
        },
    
        'type': 'object',
        'properties': {
            'ssh': {'$ref': '#/definitions/ssh-credentials'},
            'nokia-ssh': {'$ref': '#/definitions/nokia-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'},
            'lab-routers': {
                'type': 'array',
                'items': {'type': 'string'}
            },
            'unmanaged-interfaces': {
                'type': 'array',
                'items': {'$ref': '#/definitions/interface-address'}
            },
            'gws-direct': {'$ref': '#/definitions/gws-direct'},
            'nren-asn-map': {'$ref': '#/definitions/nren-asn-map'},
            'nokia-community-inventory-provider': {'type': 'string'},
            'nokia-community-dashboard': {'type': 'string'},
            'nokia-community-brian': {'type': 'string'},
    
        },
        'oneOf': [
            {
                'required': [
                    'ssh',
                    'nokia-ssh',
                    'redis',
                    'redis-databases',
                    'ims',
                    'managed-routers',
                    'gws-direct',
                    'nren-asn-map']
            },
            {
                'required': [
                    'ssh',
                    'nokia-ssh',
                    'sentinel',
                    'redis-databases',
                    'ims',
                    'managed-routers',
                    'gws-direct',
                    'nren-asn-map']
            }
        ],
        '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