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)
def test_template_value_set_copy_variable_function_during_creation(self): p = Project("project") ct = ConfigTemplate(name="my template", template_content="not a real config", project=p) self.assertEqual(ct.update_template_variable("var 1"), "var_1") self.assertEqual(ct.update_template_variable("var 2"), "var_2") self.assertEqual(ct.update_template_variable("var 3"), "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 and verify that template variables are copied to the TemplateValueSet object tvs = TemplateValueSet(hostname="Value Set", config_template=ct) self.assertTrue(len(TemplateValue.query.all()) == 4) self.assertTrue(tvs.is_value_defined("hostname")) self.assertTrue(tvs.is_value_defined("var_1")) self.assertTrue(tvs.is_value_defined("var_2")) self.assertTrue(tvs.is_value_defined("var_3")) var_1_value = "My Value" tvs.update_variable_value("var_1", var_1_value) # call it again and verify that nothing has changed tvs.copy_variables_from_config_template() self.assertTrue(len(TemplateValue.query.all()) == 4) self.assertTrue(tvs.is_value_defined("hostname")) self.assertTrue(tvs.is_value_defined("var_1")) self.assertTrue(tvs.is_value_defined("var_2")) self.assertTrue(tvs.is_value_defined("var_3")) # test that old values are preserved self.assertEqual(tvs.get_template_value_by_name_as_string("var_1"), var_1_value) self.assertEqual(tvs.get_template_value_by_name_as_string("hostname"), tvs.hostname)