def apply_config(config): for nsdata in config: ns = ConfigNamespace.get(nsdata['namespacePrefix']) # Update existing namespace if ns: ns.namespace_prefix = nsdata['namespacePrefix'] ns.name = nsdata['name'] ns.sort_order = nsdata['sortOrder'] else: ns = ConfigNamespace() ns.namespace_prefix = nsdata['namespacePrefix'] ns.name = nsdata['name'] ns.sort_order = nsdata['sortOrder'] db.session.add(ns) for itmdata in nsdata['configItems']: itm = ConfigItem.get(ns.namespace_prefix, itmdata['key']) if itm: itm.value = itmdata['value'] itm.type = itmdata['type'] itm.description = itmdata['description'] else: itm = ConfigItem() itm.namespace_prefix = ns.namespace_prefix itm.key = itmdata['key'] itm.value = itmdata['value'] itm.description = itmdata['description'] db.session.add(itm) db.session.commit()
def _register_default_option(nsobj, opt): """ Register default ConfigOption value if it doesn't exist. If does exist, update the description if needed """ item = ConfigItem.get(nsobj.namespace_prefix, opt.name) if not item: logger.info('Adding {} ({}) = {} to {}'.format(opt.name, opt.type, opt.default_value, nsobj.namespace_prefix)) item = ConfigItem() item.namespace_prefix = nsobj.namespace_prefix item.key = opt.name item.value = opt.default_value item.type = opt.type item.description = opt.description nsobj.config_items.append(item) else: if item.description != opt.description: logger.info('Updating description of {} / {}'.format( item.namespace_prefix, item.key)) item.description = opt.description db.session.add(item)
def post(self): self.reqparse.add_argument('config', type=str, required=True) args = self.reqparse.parse_args() try: config = json.loads(args['config'], cls=InquisitorJSONDecoder) for nsdata in config: ns = ConfigNamespace.get(nsdata['namespacePrefix']) # Update existing namespace if ns: ns.namespace_prefix = nsdata['namespacePrefix'] ns.name = nsdata['name'] ns.sort_order = nsdata['sortOrder'] else: ns = ConfigNamespace() ns.namespace_prefix = nsdata['namespacePrefix'] ns.name = nsdata['name'] ns.sort_order = nsdata['sortOrder'] db.session.add(ns) for itmdata in nsdata['configItems']: itm = ConfigItem.get(ns.namespace_prefix, itmdata['key']) if itm: itm.value = itmdata['value'] itm.type = itmdata['type'] itm.description = itmdata['description'] else: itm = ConfigItem() itm.namespace_prefix = ns.namespace_prefix itm.key = itmdata['key'] itm.value = itmdata['value'] itm.description = itmdata['description'] db.session.add(itm) db.session.commit() auditlog(event='config.import', actor=session['user'].username, data=config) return self.make_response('Configuration imported') except Exception as ex: self.log.exception('Failed importing configuration data') return self.make_response( 'Error importing configuration data: {}'.format(ex), HTTP.SERVER_ERROR)
def set(self, namespace, key, value, description=None): """Set (create/update) a configuration item Args: namespace (`str`): Namespace for the item key (`str`): Key of the item value (`Any`): Value of the type, must by one of `DBCString`, `DBCFloat`, `DBCInt`, `DBCArray`, `DBCJSON` or `bool` description (`str`): Description of the configuration item Returns: `None` """ if isinstance(value, DBCChoice): vtype = 'choice' elif isinstance(value, DBCString): vtype = 'string' elif isinstance(value, DBCFloat): vtype = 'float' elif isinstance(value, DBCInt): vtype = 'int' elif isinstance(value, DBCArray): vtype = 'array' elif isinstance(value, DBCJSON): vtype = 'json' elif isinstance(value, bool): vtype = 'bool' else: raise ValueError('Invalid config item type: {}'.format( type(value))) if namespace in self.__data and key in self.__data[namespace]: itm = db.ConfigItem.find_one( ConfigItem.namespace_prefix == namespace, ConfigItem.key == key) if not itm: raise KeyError(key) itm.value = value itm.type = vtype if description: itm.description = description else: itm = ConfigItem() itm.key = key itm.value = value itm.type = vtype itm.description = description itm.namespace_prefix = namespace db.session.add(itm) db.session.commit() if namespace in self.__data: self.__data[namespace][key] = value else: self.__data[namespace] = {key: value}