summaryrefslogtreecommitdiffstats
path: root/anta/inventory/__init__.py
blob: c5327bd442f86b022f540da1e5d23e4199ac43d2 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# 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.
"""
Inventory Module for ANTA.
"""

from __future__ import annotations

import asyncio
import logging
from ipaddress import ip_address, ip_network
from pathlib import Path
from typing import Any, Optional

from pydantic import ValidationError
from yaml import YAMLError, safe_load

from anta.device import AntaDevice, AsyncEOSDevice
from anta.inventory.exceptions import InventoryIncorrectSchema, InventoryRootKeyError
from anta.inventory.models import AntaInventoryInput
from anta.logger import anta_log_exception

logger = logging.getLogger(__name__)


class AntaInventory(dict):  # type: ignore
    # dict[str, AntaDevice] - not working in python 3.8 hence the ignore
    """
    Inventory abstraction for ANTA framework.
    """

    # Root key of inventory part of the inventory file
    INVENTORY_ROOT_KEY = "anta_inventory"
    # Supported Output format
    INVENTORY_OUTPUT_FORMAT = ["native", "json"]

    def __str__(self) -> str:
        """Human readable string representing the inventory"""
        devs = {}
        for dev in self.values():
            if (dev_type := dev.__class__.__name__) not in devs:
                devs[dev_type] = 1
            else:
                devs[dev_type] += 1
        return f"ANTA Inventory contains {' '.join([f'{n} devices ({t})' for t, n in devs.items()])}"

    @staticmethod
    def _update_disable_cache(inventory_disable_cache: bool, kwargs: dict[str, Any]) -> dict[str, Any]:
        """
        Return new dictionary, replacing kwargs with added disable_cache value from inventory_value
        if disable_cache has not been set by CLI.

        Args:
            inventory_disable_cache (bool): The value of disable_cache in the inventory
            kwargs: The kwargs to instantiate the device
        """
        updated_kwargs = kwargs.copy()
        updated_kwargs["disable_cache"] = inventory_disable_cache or kwargs.get("disable_cache")
        return updated_kwargs

    @staticmethod
    def _parse_hosts(inventory_input: AntaInventoryInput, inventory: AntaInventory, **kwargs: Any) -> None:
        """
        Parses the host section of an AntaInventoryInput and add the devices to the inventory

        Args:
            inventory_input (AntaInventoryInput): AntaInventoryInput used to parse the devices
            inventory (AntaInventory): AntaInventory to add the parsed devices to
        """
        if inventory_input.hosts is None:
            return

        for host in inventory_input.hosts:
            updated_kwargs = AntaInventory._update_disable_cache(host.disable_cache, kwargs)
            device = AsyncEOSDevice(name=host.name, host=str(host.host), port=host.port, tags=host.tags, **updated_kwargs)
            inventory.add_device(device)

    @staticmethod
    def _parse_networks(inventory_input: AntaInventoryInput, inventory: AntaInventory, **kwargs: Any) -> None:
        """
        Parses the network section of an AntaInventoryInput and add the devices to the inventory.

        Args:
            inventory_input (AntaInventoryInput): AntaInventoryInput used to parse the devices
            inventory (AntaInventory): AntaInventory to add the parsed devices to

        Raises:
            InventoryIncorrectSchema: Inventory file is not following AntaInventory Schema.
        """
        if inventory_input.networks is None:
            return

        for network in inventory_input.networks:
            try:
                updated_kwargs = AntaInventory._update_disable_cache(network.disable_cache, kwargs)
                for host_ip in ip_network(str(network.network)):
                    device = AsyncEOSDevice(host=str(host_ip), tags=network.tags, **updated_kwargs)
                    inventory.add_device(device)
            except ValueError as e:
                message = "Could not parse network {network.network} in the inventory"
                anta_log_exception(e, message, logger)
                raise InventoryIncorrectSchema(message) from e

    @staticmethod
    def _parse_ranges(inventory_input: AntaInventoryInput, inventory: AntaInventory, **kwargs: Any) -> None:
        """
        Parses the range section of an AntaInventoryInput and add the devices to the inventory.

        Args:
            inventory_input (AntaInventoryInput): AntaInventoryInput used to parse the devices
            inventory (AntaInventory): AntaInventory to add the parsed devices to

        Raises:
            InventoryIncorrectSchema: Inventory file is not following AntaInventory Schema.
        """
        if inventory_input.ranges is None:
            return

        for range_def in inventory_input.ranges:
            try:
                updated_kwargs = AntaInventory._update_disable_cache(range_def.disable_cache, kwargs)
                range_increment = ip_address(str(range_def.start))
                range_stop = ip_address(str(range_def.end))
                while range_increment <= range_stop:  # type: ignore[operator]
                    # mypy raise an issue about comparing IPv4Address and IPv6Address
                    # but this is handled by the ipaddress module natively by raising a TypeError
                    device = AsyncEOSDevice(host=str(range_increment), tags=range_def.tags, **updated_kwargs)
                    inventory.add_device(device)
                    range_increment += 1
            except ValueError as e:
                message = f"Could not parse the following range in the inventory: {range_def.start} - {range_def.end}"
                anta_log_exception(e, message, logger)
                raise InventoryIncorrectSchema(message) from e
            except TypeError as e:
                message = f"A range in the inventory has different address families for start and end: {range_def.start} - {range_def.end}"
                anta_log_exception(e, message, logger)
                raise InventoryIncorrectSchema(message) from e

    @staticmethod
    def parse(
        filename: str | Path,
        username: str,
        password: str,
        enable: bool = False,
        enable_password: Optional[str] = None,
        timeout: Optional[float] = None,
        insecure: bool = False,
        disable_cache: bool = False,
    ) -> AntaInventory:
        # pylint: disable=too-many-arguments
        """
        Create an AntaInventory instance from an inventory file.
        The inventory devices are AsyncEOSDevice instances.

        Args:
            filename (str): Path to device inventory YAML file
            username (str): Username to use to connect to devices
            password (str): Password to use to connect to devices
            enable (bool): Whether or not the commands need to be run in enable mode towards the devices
            enable_password (str, optional): Enable password to use if required
            timeout (float, optional): timeout in seconds for every API call.
            insecure (bool): Disable SSH Host Key validation
            disable_cache (bool): Disable cache globally

        Raises:
            InventoryRootKeyError: Root key of inventory is missing.
            InventoryIncorrectSchema: Inventory file is not following AntaInventory Schema.
        """

        inventory = AntaInventory()
        kwargs: dict[str, Any] = {
            "username": username,
            "password": password,
            "enable": enable,
            "enable_password": enable_password,
            "timeout": timeout,
            "insecure": insecure,
            "disable_cache": disable_cache,
        }
        if username is None:
            message = "'username' is required to create an AntaInventory"
            logger.error(message)
            raise ValueError(message)
        if password is None:
            message = "'password' is required to create an AntaInventory"
            logger.error(message)
            raise ValueError(message)

        try:
            with open(file=filename, mode="r", encoding="UTF-8") as file:
                data = safe_load(file)
        except (TypeError, YAMLError, OSError) as e:
            message = f"Unable to parse ANTA Device Inventory file '{filename}'"
            anta_log_exception(e, message, logger)
            raise

        if AntaInventory.INVENTORY_ROOT_KEY not in data:
            exc = InventoryRootKeyError(f"Inventory root key ({AntaInventory.INVENTORY_ROOT_KEY}) is not defined in your inventory")
            anta_log_exception(exc, f"Device inventory is invalid! (from {filename})", logger)
            raise exc

        try:
            inventory_input = AntaInventoryInput(**data[AntaInventory.INVENTORY_ROOT_KEY])
        except ValidationError as e:
            anta_log_exception(e, f"Device inventory is invalid! (from {filename})", logger)
            raise

        # Read data from input
        AntaInventory._parse_hosts(inventory_input, inventory, **kwargs)
        AntaInventory._parse_networks(inventory_input, inventory, **kwargs)
        AntaInventory._parse_ranges(inventory_input, inventory, **kwargs)

        return inventory

    ###########################################################################
    # Public methods
    ###########################################################################

    ###########################################################################
    # GET methods
    ###########################################################################

    def get_inventory(self, established_only: bool = False, tags: Optional[list[str]] = None) -> AntaInventory:
        """
        Returns a filtered inventory.

        Args:
            established_only: Whether or not to include only established devices. Default False.
            tags: List of tags to filter devices.

        Returns:
            AntaInventory: An inventory with filtered AntaDevice objects.
        """

        def _filter_devices(device: AntaDevice) -> bool:
            """
            Helper function to select the devices based on the input tags
            and the requirement for an established connection.
            """
            if tags is not None and all(tag not in tags for tag in device.tags):
                return False
            return bool(not established_only or device.established)

        devices: list[AntaDevice] = list(filter(_filter_devices, self.values()))
        result = AntaInventory()
        for device in devices:
            result.add_device(device)
        return result

    ###########################################################################
    # SET methods
    ###########################################################################

    def __setitem__(self, key: str, value: AntaDevice) -> None:
        if key != value.name:
            raise RuntimeError(f"The key must be the device name for device '{value.name}'. Use AntaInventory.add_device().")
        return super().__setitem__(key, value)

    def add_device(self, device: AntaDevice) -> None:
        """Add a device to final inventory.

        Args:
            device: Device object to be added
        """
        self[device.name] = device

    ###########################################################################
    # MISC methods
    ###########################################################################

    async def connect_inventory(self) -> None:
        """Run `refresh()` coroutines for all AntaDevice objects in this inventory."""
        logger.debug("Refreshing devices...")
        results = await asyncio.gather(
            *(device.refresh() for device in self.values()),
            return_exceptions=True,
        )
        for r in results:
            if isinstance(r, Exception):
                message = "Error when refreshing inventory"
                anta_log_exception(r, message, logger)