Пример #1
0
def create_inventory_plugin(
    *,
    name: str,
    sections: Optional[List[str]] = None,
    inventory_function: Callable,
    inventory_default_parameters: Optional[Dict] = None,
    inventory_ruleset_name: Optional[str] = None,
    module: Optional[str] = None,
) -> InventoryPlugin:
    """Return an InventoryPlugin object after validating and converting the arguments one by one

    For a detailed description of the parameters please refer to the exposed function in the
    'register' namespace of the API.
    """
    plugin_name = InventoryPluginName(name)

    subscribed_sections = create_subscribed_sections(sections, plugin_name)

    validate_function_arguments(
        type_label="inventory",
        function=inventory_function,
        has_item=False,
        default_params=inventory_default_parameters,
        sections=subscribed_sections,
    )

    # validate check arguments
    validate_default_parameters(
        "inventory",
        inventory_ruleset_name,
        inventory_default_parameters,
    )

    return InventoryPlugin(
        name=plugin_name,
        sections=subscribed_sections,
        inventory_function=_filter_inventory(inventory_function),
        inventory_default_parameters=inventory_default_parameters or {},
        inventory_ruleset_name=(RuleSetName(inventory_ruleset_name)
                                if inventory_ruleset_name else None),
        module=module,
    )
Пример #2
0
def create_check_plugin(
    *,
    name: str,
    sections: Optional[List[str]] = None,
    service_name: str,
    discovery_function: Callable,
    discovery_default_parameters: Optional[Dict] = None,
    discovery_ruleset_name: Optional[str] = None,
    discovery_ruleset_type: RuleSetType = "merged",
    check_function: Callable,
    check_default_parameters: Optional[Dict] = None,
    check_ruleset_name: Optional[str] = None,
    cluster_check_function: Optional[Callable] = None,
    module: Optional[str] = None,
    validate_item: bool = True,
    validate_kwargs: bool = True,
) -> CheckPlugin:
    """Return an CheckPlugin object after validating and converting the arguments one by one

    For a detailed description of the parameters please refer to the exposed function in the
    'register' namespace of the API.
    """
    plugin_name = CheckPluginName(name)

    subscribed_sections = create_subscribed_sections(sections, plugin_name)

    requires_item = _requires_item(service_name)

    if validate_kwargs:
        _validate_kwargs(
            plugin_name=plugin_name,
            subscribed_sections=subscribed_sections,
            service_name=service_name,
            requires_item=requires_item,
            discovery_function=discovery_function,
            discovery_default_parameters=discovery_default_parameters,
            discovery_ruleset_name=discovery_ruleset_name,
            discovery_ruleset_type=discovery_ruleset_type,
            check_function=check_function,
            check_default_parameters=check_default_parameters,
            check_ruleset_name=check_ruleset_name,
            cluster_check_function=cluster_check_function,
        )

    disco_func = _filter_discovery(discovery_function, requires_item,
                                   validate_item)
    disco_ruleset_name = RuleSetName(
        discovery_ruleset_name) if discovery_ruleset_name else None

    cluster_check_function = (unfit_for_clustering_wrapper(check_function)
                              if cluster_check_function is None else
                              _filter_check(cluster_check_function))

    return CheckPlugin(
        name=plugin_name,
        sections=subscribed_sections,
        service_name=service_name,
        discovery_function=disco_func,
        discovery_default_parameters=discovery_default_parameters,
        discovery_ruleset_name=disco_ruleset_name,
        discovery_ruleset_type=discovery_ruleset_type,
        check_function=_filter_check(check_function),
        check_default_parameters=check_default_parameters,
        check_ruleset_name=RuleSetName(check_ruleset_name)
        if check_ruleset_name else None,
        cluster_check_function=cluster_check_function,
        module=module,
    )
Пример #3
0
def create_check_plugin(
    *,
    name: str,
    sections: Optional[List[str]] = None,
    service_name: str,
    discovery_function: Callable,
    discovery_default_parameters: Optional[Dict] = None,
    discovery_ruleset_name: Optional[str] = None,
    discovery_ruleset_type: DiscoveryRuleSetType = "merged",
    check_function: Callable,
    check_default_parameters: Optional[Dict] = None,
    check_ruleset_name: Optional[str] = None,
    cluster_check_function: Optional[Callable] = None,
    module: Optional[str] = None,
    validate_item: bool = True,
) -> CheckPlugin:
    """Return an CheckPlugin object after validating and converting the arguments one by one

    For a detailed description of the parameters please refer to the exposed function in the
    'register' namespace of the API.
    """
    plugin_name = CheckPluginName(name)

    subscribed_sections = create_subscribed_sections(sections, plugin_name)

    _validate_service_name(plugin_name, service_name)
    requires_item = _requires_item(service_name)

    # validate discovery arguments
    validate_default_parameters(
        "discovery",
        discovery_ruleset_name,
        discovery_default_parameters,
    )
    _validate_discovery_ruleset(
        discovery_ruleset_name,
        discovery_default_parameters,
    )
    _validate_discovery_ruleset_type(discovery_ruleset_type, )
    validate_function_arguments(
        type_label="discovery",
        function=discovery_function,
        has_item=False,
        has_params=discovery_ruleset_name is not None,
        sections=subscribed_sections,
    )
    disco_func = _filter_discovery(discovery_function, requires_item,
                                   validate_item)
    disco_params = discovery_default_parameters or {}
    disco_ruleset_name = RuleSetName(
        discovery_ruleset_name) if discovery_ruleset_name else None

    # validate check arguments
    validate_default_parameters(
        "check",
        check_ruleset_name,
        check_default_parameters,
    )
    _validate_check_ruleset(
        check_ruleset_name,
        check_default_parameters,
    )
    validate_function_arguments(
        type_label="check",
        function=check_function,
        has_item=requires_item,
        has_params=check_default_parameters is not None,
        sections=subscribed_sections,
    )

    if cluster_check_function is None:
        cluster_check_function = unfit_for_clustering_wrapper(check_function)
    else:
        validate_function_arguments(
            type_label="cluster_check",
            function=cluster_check_function,
            has_item=requires_item,
            has_params=check_ruleset_name is not None,
            sections=subscribed_sections,
        )
        cluster_check_function = _filter_check(cluster_check_function)

    return CheckPlugin(
        name=plugin_name,
        sections=subscribed_sections,
        service_name=service_name,
        discovery_function=disco_func,
        discovery_default_parameters=disco_params,
        discovery_ruleset_name=disco_ruleset_name,
        discovery_ruleset_type=discovery_ruleset_type,
        check_function=_filter_check(check_function),
        check_default_parameters=check_default_parameters or {},
        check_ruleset_name=RuleSetName(check_ruleset_name)
        if check_ruleset_name else None,
        cluster_check_function=cluster_check_function,
        module=module,
    )