Migrate Ruleset with Tuple for Version check

How do I migrate a ruleset where one of the parameters is a Tuple() with two TextAscii() fields for comparing with a version number?

{"version": ('2.3.0', '2.2.0')}

I tried with this specification:

def _valuespec_test_string_levels():
    return Dictionary(
        title=Title("TEST String Levels"),
        elements={
            "version": DictElement(
                parameter_form=SimpleLevels(
                    title=Title("Version"),
                    form_spec_template=String(),
                    level_direction=LevelDirection.LOWER,
                    prefill_fixed_levels=InputHint(("2.3.0p31", "2.3.0b1")),
                )
            )
        }
    )

rule_spec_test_string_levels = CheckParameters(
    name="test_string_levels",
    topic=Topic.APPLICATIONS,
    parameter_form=_valuespec_test_string_levels,
    title=Title("TEST String Levels"),
    condition=HostCondition(),
)

But I get the error message:

Internal error: cannot access local variable 'prefill_value' where it is not associated with a value

This is a test with a completely new ruleset specification. The next step would be to write a migration function, but first the spec must work.

The old code looked like this:

def _parameter_valuespec_test_string_levels():
    return Dictionary(
        title = _("TEST String Levels"),
        elements = [
            ('version', Tuple(
                 title = _("Version"),
                 elements = [
                     TextAscii(title = _("Warning below"), default_value = "2.3.0p31"),
                     TextAscii(title = _("Critical below"), default_value = "2.3.0b1"),
                ]
            ),
        ],
    )

Today i migrated a rule with a similar problem.

Dict element with an tuple as value.

Definition

def _parameter_valuespec_hyperv_vm_integration():
    return Dictionary(
        elements={
            "match_services": DictElement(
                parameter_form=List(
                    title=Title("Special States"),
                    migrate=_migrate_tuple,
                    element_template=Dictionary(
                        elements={
                            "service_name": DictElement(
                                required=True,
                                parameter_form=String(
                                    title=Title("Service name"),
                                    custom_validate=(LengthInRange(min_value=1),),
                                ),
                            ),
                            "state": DictElement(
                                required=True,
                                parameter_form=String(
                                    title=Title("State name"),
                                    custom_validate=(LengthInRange(min_value=1),),
                                ),
                            ),
                        }
                    ),
                ),
            ),
        }
    )

Result

{"match_services": [
    {"service_name": "testname123", "state": "running"}, 
    {"service_name": "testname456", "state": "stopped"},
]}

It’s more complex than the result before but it was ok in my case.

Link to the complete code - also the migrate function was not so easy.

1 Like