def test_template_value_set_name_constraint(self): p = Project("project") ct1 = ConfigTemplate("Template A", project=p) ct2 = ConfigTemplate("Template B", project=p) tvs1 = TemplateValueSet("values 1", ct1) tvs2 = TemplateValueSet("values 2", ct1) tvs3 = TemplateValueSet("values 3", ct2) tvs4 = TemplateValueSet("values 4", ct2) db.session.add_all([p, ct1, ct2, tvs1, tvs2, tvs3, tvs4]) db.session.commit() self.assertTrue(len(TemplateValueSet.query.all()) == 4) # test integrity check with self.assertRaises(IntegrityError): failed_tvs = TemplateValueSet("values 2", ct1) db.session.rollback() failed_tvs = TemplateValueSet("values 2") failed_tvs.config_template = ct1 db.session.add(failed_tvs) with self.assertRaises(IntegrityError): db.session.commit() db.session.rollback() failed_tvs.config_template = ct2 db.session.add(failed_tvs) db.session.commit() self.assertTrue(len(TemplateValueSet.query.all()) == 5)
def test_template_value_set_copy_variable_function_afterwards(self): p = Project("project") ct = ConfigTemplate(name="my template", template_content="not a real config", project=p) ct.update_template_variable("var 1") ct.update_template_variable("var 2") ct.update_template_variable("var 3") db.session.add_all([p, ct]) db.session.commit() # verify database content self.assertTrue(len(ConfigTemplate.query.all()) == 1) self.assertTrue(len(TemplateVariable.query.all()) == 3 + 1) # create a new template value set without link to a config template tvs = TemplateValueSet(hostname="Value Set") # define it afterwards (you need to trigger the copy process manually) tvs.config_template = ct tvs.copy_variables_from_config_template( ) # this method will automatically create a 'hostname' variable value self.assertTrue(len(TemplateValue.query.all()) == 4) self.assertTrue(tvs.is_value_defined("var_1")) self.assertTrue(tvs.is_value_defined("var_2")) self.assertTrue(tvs.is_value_defined("var_3")) self.assertTrue(tvs.is_value_defined("hostname"))
def add_template_value_set(config_template_id): """add a new Template Value Set :param config_template_id: :return: """ parent_config_template = ConfigTemplate.query.filter(ConfigTemplate.id == config_template_id).first_or_404() form = TemplateValueSetForm(request.form) if form.validate_on_submit(): try: template_value_set = TemplateValueSet(hostname="", config_template=parent_config_template) template_value_set.hostname = form.hostname.data template_value_set.config_template = parent_config_template template_value_set.copy_variables_from_config_template() db.session.add(template_value_set) db.session.commit() flash("Template Value Set successful created", "success") return redirect(url_for( "edit_template_value_set", template_value_set_id=template_value_set.id, config_template_id=parent_config_template.id )) except IntegrityError as ex: if "UNIQUE constraint failed" in str(ex): msg = "Template Value Set hostname already in use, please use another one" else: msg = "Template Value Set was not created (unknown error)" flash(msg, "error") logger.error(msg, exc_info=True) db.session.rollback() except Exception: msg = "Template Value Set was not created (unknown error)" logger.error(msg, exc_info=True) flash(msg, "error") db.session.rollback() return render_template( "template_value_set/add_template_value_set.html", config_template=parent_config_template, project=parent_config_template.project, form=form )
def add_template_value_set(config_template_id): """add a new Template Value Set :param config_template_id: :return: """ parent_config_template = ConfigTemplate.query.filter( ConfigTemplate.id == config_template_id).first_or_404() form = TemplateValueSetForm(request.form) if form.validate_on_submit(): try: template_value_set = TemplateValueSet( hostname="", config_template=parent_config_template) template_value_set.hostname = form.hostname.data template_value_set.config_template = parent_config_template template_value_set.copy_variables_from_config_template() db.session.add(template_value_set) db.session.commit() flash("Template Value Set successful created", "success") return redirect( url_for("edit_template_value_set", template_value_set_id=template_value_set.id, config_template_id=parent_config_template.id)) except IntegrityError as ex: if "UNIQUE constraint failed" in str(ex): msg = "Template Value Set hostname already in use, please use another one" else: msg = "Template Value Set was not created (unknown error)" flash(msg, "error") logger.error(msg, exc_info=True) db.session.rollback() except Exception: msg = "Template Value Set was not created (unknown error)" logger.error(msg, exc_info=True) flash(msg, "error") db.session.rollback() return render_template("template_value_set/add_template_value_set.html", config_template=parent_config_template, project=parent_config_template.project, form=form)