Exemple #1
0
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()
Exemple #2
0
    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)