summaryrefslogtreecommitdiffstats
path: root/anta/cli/debug/utils.py
blob: a4bb5d9b27129634225599bb3ce7cc0aebc7259a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Copyright (c) 2023-2024 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
"""Utils functions to use with anta.cli.debug module."""

from __future__ import annotations

import functools
import logging
from typing import TYPE_CHECKING, Any, Callable

import click

from anta.cli.utils import ExitCode, inventory_options

if TYPE_CHECKING:
    from anta.inventory import AntaInventory

logger = logging.getLogger(__name__)


def debug_options(f: Callable[..., Any]) -> Callable[..., Any]:
    """Click common options required to execute a command on a specific device."""

    @inventory_options
    @click.option(
        "--ofmt",
        type=click.Choice(["json", "text"]),
        default="json",
        help="EOS eAPI format to use. can be text or json",
    )
    @click.option(
        "--version",
        "-v",
        type=click.Choice(["1", "latest"]),
        default="latest",
        help="EOS eAPI version",
    )
    @click.option("--revision", "-r", type=int, help="eAPI command revision", required=False)
    @click.option("--device", "-d", type=str, required=True, help="Device from inventory to use")
    @click.pass_context
    @functools.wraps(f)
    def wrapper(
        ctx: click.Context,
        *args: tuple[Any],
        inventory: AntaInventory,
        tags: set[str] | None,
        device: str,
        **kwargs: Any,
    ) -> Any:
        # TODO: @gmuloc - tags come from context https://github.com/arista-netdevops-community/anta/issues/584
        # pylint: disable=unused-argument
        # ruff: noqa: ARG001
        try:
            d = inventory[device]
        except KeyError as e:
            message = f"Device {device} does not exist in Inventory"
            logger.error(e, message)
            ctx.exit(ExitCode.USAGE_ERROR)
        return f(*args, device=d, **kwargs)

    return wrapper