Пример #1
0
def test_create_inventory_plugin_missing_kwarg():
    with pytest.raises(TypeError):
        _ = inventory_plugins.create_inventory_plugin(name="norris")  # type: ignore[call-arg] #pylint: disable=missing-kwoa

    with pytest.raises(TypeError):
        _ = inventory_plugins.create_inventory_plugin(  # pylint: disable=missing-kwoa
            inventory_function=dummy_generator)  # type: ignore[call-arg]
Пример #2
0
def create_inventory_plugin_from_legacy(
    inventory_plugin_name: str,
    inventory_info_dict: Dict[str, Any],
    extra_sections_count: int,
) -> InventoryPlugin:

    if inventory_info_dict.get('depends_on'):
        # TODO (mo): this affects only inv_cisco_vlans. For now, we silently
        # skip this, until we migrate inv_cisco_vlans to the new api; then
        # raise NotImplementedError("cannot auto-migrate plugins with dependencies")
        pass

    new_inventory_name = maincheckify(inventory_plugin_name)

    legacy_inventory_function = inventory_info_dict["inv_function"]
    has_parameters = _function_has_params(legacy_inventory_function)

    inventory_function = _create_inventory_function(
        legacy_inventory_function,
        has_parameters,
        extra_sections_count,
    )

    return create_inventory_plugin(
        name=new_inventory_name,
        sections=[inventory_plugin_name.split('.', 1)[0]],
        inventory_function=inventory_function,
        inventory_default_parameters={} if has_parameters else None,
        inventory_ruleset_name=inventory_plugin_name if has_parameters else None,
        module=None,
    )
Пример #3
0
def create_inventory_plugin_from_legacy(
    inventory_plugin_name: str,
    inventory_info_dict: Dict[str, Any],
    extra_sections_count: int,
) -> InventoryPlugin:

    if inventory_info_dict.get('depends_on'):
        raise NotImplementedError(
            "cannot auto-migrate plugins with dependencies")

    new_inventory_name = maincheckify(inventory_plugin_name)

    legacy_inventory_function = inventory_info_dict["inv_function"]
    has_parameters = _function_has_params(legacy_inventory_function)

    inventory_function = _create_inventory_function(
        legacy_inventory_function,
        has_parameters,
        extra_sections_count,
    )

    return create_inventory_plugin(
        name=new_inventory_name,
        sections=[inventory_plugin_name.split('.', 1)[0]],
        inventory_function=inventory_function,
        inventory_default_parameters={} if has_parameters else None,
        inventory_ruleset_name=inventory_plugin_name
        if has_parameters else None,
        module=None,
    )
def test_create_inventory_plugin_not_a_generator():
    def dummy_function(section):  # pylint: disable=unused-argument
        pass

    with pytest.raises(TypeError):
        _ = inventory_plugins.create_inventory_plugin(
            name="norris",
            inventory_function=dummy_function,
        )
def test_create_inventory_plugin_wrong_arg_name():
    def dummy_generator(noitces):  # pylint: disable=unused-argument
        return
        yield  # pylint: disable=unreachable

    with pytest.raises(TypeError):
        _ = inventory_plugins.create_inventory_plugin(
            name="norris",
            inventory_function=dummy_generator,
        )
Пример #6
0
def inventory_plugin(
    *,
    name: str,
    sections: Optional[List[str]] = None,
    inventory_function: InventoryFunction,
    inventory_default_parameters: Optional[Dict[str, Any]] = None,
    inventory_ruleset_name: Optional[str] = None,
) -> None:
    """Register an inventory plugin to checkmk.

    Args:

      name:                     The unique name of the check plugin. It must only contain the
                                characters 'A-Z', 'a-z', '0-9' and the underscore.

      sections:                 An optional list of section names that this plugin subscribes to.
                                They correspond to the 'parsed_section_name' specified in
                                :meth:`agent_section` and :meth:`snmp_section`.
                                The corresponding sections are passed to the discovery and check
                                function. The functions arguments must be called 'section_<name1>,
                                section_<name2>' ect. Defaults to a list containing as only element
                                a name equal to the name of the inventory plugin.

      inventory_function:       The inventory_function. Arguments must be 'params' (if inventory
                                parameters are defined) and 'section' (if the plugin subscribes
                                to a single section), or 'section_<name1>, section_<name2>' ect.
                                corresponding to the `sections`.
                                It is expected to be a generator of :class:`Attributes` or
                                :class:`TableRow` instances.

      inventory_default_parameters: Default parameters for the inventory function. Must match the
                                ValueSpec of the corresponding WATO ruleset, if it exists.

      inventory_ruleset_name:   The name of the inventory ruleset.

    """
    plugin = create_inventory_plugin(
        name=name,
        sections=sections,
        inventory_function=inventory_function,
        inventory_default_parameters=inventory_default_parameters,
        inventory_ruleset_name=inventory_ruleset_name,
        module=get_validated_plugin_module_name(),
    )

    if is_registered_inventory_plugin(plugin.name):
        raise ValueError("duplicate inventory plugin definition: %s" %
                         plugin.name)

    add_inventory_plugin(plugin)
def test_create_inventory_plugin_minimal():
    plugin = inventory_plugins.create_inventory_plugin(
        name="norris",
        inventory_function=dummy_generator,
    )

    assert isinstance(plugin, InventoryPlugin)
    assert plugin.name == InventoryPluginName("norris")
    assert plugin.sections == [ParsedSectionName("norris")]
    assert plugin.inventory_function.__name__ == "dummy_generator"
    assert plugin.inventory_default_parameters == {}
    assert plugin.inventory_ruleset_name is None
    assert plugin.module is None

    with pytest.raises(TypeError):
        _ = list(plugin.inventory_function(None))
Пример #8
0
def inventory_plugin(
    *,
    name: str,
    sections: Optional[List[str]] = None,
    inventory_function: InventoryFunction,
    inventory_default_parameters: Optional[Dict[str, Any]] = None,
    inventory_ruleset_name: Optional[str] = None,
) -> None:
    """Register a check plugin to checkmk.

    :param name: The name of the check plugin. It must be unique. And contain only the characters
                 A-Z, a-z, 0-9 and the underscore.
    :param sections: An optional list of section names that this plugin subscribes to. The
                     corresponding sections are passed to the inventory function. The
                     functions arguments must be called 'section_<name1>, section_<name2>' ect.
                     Default: [<name>]
    :param inventory_function: The check_function. Arguments must be 'params' (if inventory
                               parameters are defined) and 'section_<name1>,
                               section_<name2>' ect. corresponding to the `sections`.
    :param inventory_parameters: Default parameters for the inventory function. Must match the
                             ValueSpec of the corresponding WATO ruleset.
    :param inventory_ruleset_name: The name of the inventory ruleset.
    """
    plugin = create_inventory_plugin(
        name=name,
        sections=sections,
        inventory_function=inventory_function,
        inventory_default_parameters=inventory_default_parameters,
        inventory_ruleset_name=inventory_ruleset_name,
        module=get_validated_plugin_module_name(),
    )

    if is_registered_inventory_plugin(plugin.name):
        raise ValueError("duplicate inventory plugin definition: %s" %
                         plugin.name)

    add_inventory_plugin(plugin)