def create_template(req):
        """
        Creates a new template.

        :param req: The received HTTP request, as created by Flask.
        :return The created template.
        :raises HTTPRequestError: If no authorization token was provided (no
        tenant was informed)
        :raises HTTPRequestError: If template attribute constraints were
        violated. This might happen if two attributes have the same name, for
        instance.
        """
        init_tenant_context(req, db)
        tpl, json_payload = parse_payload(req, template_schema)
        loaded_template = DeviceTemplate(**tpl)
        load_attrs(json_payload['attrs'], loaded_template, DeviceAttr, db)
        db.session.add(loaded_template)

        try:
            db.session.commit()
            LOGGER.debug(f" Created template in database")
        except IntegrityError as e:
            LOGGER.error(f' {e}')
            raise HTTPRequestError(
                400,
                'Template attribute constraints are violated by the request')

        results = {
            'template': template_schema.dump(loaded_template),
            'result': 'ok'
        }
        return results
Пример #2
0
    def save_templates(json_data, json_payload):
        saved_templates = []
        for template in json_data['templates']:
            loaded_template = DeviceTemplate(**template)
            for json in json_payload['templates']:
                if (json['import_id'] == template["id"]):
                    load_attrs(json['attrs'], loaded_template, DeviceAttr, db)
            db.session.add(loaded_template)
            saved_templates.append(loaded_template)

        LOGGER.info(f" Saved templates")
        return saved_templates
Пример #3
0
def auto_create_template(json_payload, new_device):
    if ('attrs' in json_payload) and (new_device.templates is None):
        device_template = DeviceTemplate(
            label="device.%s template" % new_device.id)
        db.session.add(device_template)
        LOGGER.debug(f" Adding auto-created template {device_template} into database")
        new_device.templates = [device_template]
        load_attrs(json_payload['attrs'], device_template, DeviceAttr, db)

    # TODO: perhaps it'd be best if all ids were random hex strings?
    if ('attrs' in json_payload) and (new_device.templates is not None):
        for attr in json_payload['attrs']:
            orm_template = find_template(new_device.templates, attr['template_id'])
            if orm_template is None:
                LOGGER.error(f" Unknown template {orm_template} in attr list")
                raise HTTPRequestError(400, 'Unknown template "{}" in attr list'.format(orm_template))
            create_orm_override(attr, new_device, orm_template)
Пример #4
0
def auto_create_template(json_payload, new_device):
    if ('attrs' in json_payload) and (new_device.templates is None):
        device_template = DeviceTemplate(label="device.%s template" %
                                         new_device.id)
        db.session.add(device_template)
        new_device.templates = [device_template]
        load_attrs(json_payload['attrs'], device_template, DeviceAttr, db)

    # TODO: perhaps it'd be best if all ids were random hex strings?
    if ('attrs' in json_payload) and (new_device.templates is not None):
        for attr in json_payload['attrs']:
            orm_template = find_template(new_device.templates,
                                         attr['template_id'])
            if orm_template is None:
                raise HTTPRequestError(
                    400,
                    'Unknown template "{}" in attr list'.format(orm_template))

            try:
                target = int(attr['id'])
            except ValueError:
                raise HTTPRequestError(
                    400, "Unkown attribute \"{}\" in override list".format(
                        attr['id']))

            found = False
            for orm_attr in orm_template.attrs:
                if target == orm_attr.id:
                    found = True
                    orm_override = DeviceOverride(
                        device=new_device,
                        attr=orm_attr,
                        static_value=attr['static_value'])
                    db.session.add(orm_override)
            if not found:
                raise HTTPRequestError(
                    400,
                    "Unkown attribute \"{}\" in override list".format(target))