ANTA Device API
AntaDevice
¶
Bases: ABC
Abstract class representing a device in ANTA.
An implementation of this class must override the abstract coroutines _collect() and
refresh().
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Device name. |
is_online |
bool
|
True if the device IP is reachable and a port can be open. |
established |
bool
|
True if remote command execution succeeds. |
hw_model |
str | None
|
Hardware model of the device. |
tags |
set[str]
|
Tags for this device. |
cache |
AntaCache | None
|
In-memory cache for this device (None if cache is disabled). |
cache_locks |
defaultdict[str, Lock] | None
|
Dictionary mapping keys to asyncio locks to guarantee exclusive access to the cache if not disabled. Deprecated, will be removed in ANTA v2.0.0, use self.cache.locks instead. |
max_connections |
int | None
|
For informational/logging purposes only. Can be used by the runner to verify that the total potential connections of a run do not exceed the system file descriptor limit. This does not affect the actual device configuration. None if not available. |
capabilities |
AntaDeviceCapabilities
|
Class-level declaration of which optional features this device type supports. Subclasses override this to advertise their capabilities. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Device name. |
required |
tags
|
set[str] | None
|
Tags for this device. |
None
|
disable_cache
|
bool
|
Disable caching for all commands for this device. |
False
|
cache_statistics
property
¶
Return the device cache statistics for logging purposes.
max_connections
property
¶
max_connections: int | None
Maximum number of concurrent connections allowed by the device. Can be overridden by subclasses, returns None if not available.
_collect
abstractmethod
async
¶
_collect(command: AntaCommand, *, collection_id: str | None = None) -> None
Collect device command output.
This abstract coroutine can be used to implement any command collection method for a device in ANTA.
The _collect() implementation needs to populate the output attribute
of the AntaCommand object passed as argument.
If a failure occurs, the _collect() implementation is expected to catch the
exception and implement proper logging, the output attribute of the
AntaCommand object passed as argument would be None in this case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
AntaCommand
|
The command to collect. |
required |
collection_id
|
str | None
|
An identifier used to build the eAPI request ID. |
None
|
Source code in anta/device.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
collect
async
¶
collect(command: AntaCommand, *, collection_id: str | None = None) -> None
Collect the output for a specified command.
When caching is activated on both the device and the command, this method prioritizes retrieving the output from the cache. In cases where the output isn’t cached yet, it will be freshly collected and then stored in the cache for future access. The method employs asynchronous locks based on the command’s UID to guarantee exclusive access to the cache.
When caching is NOT enabled, either at the device or command level, the method directly collects the output
via the private _collect method without interacting with the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
AntaCommand
|
The command to collect. |
required |
collection_id
|
str | None
|
An identifier used to build the eAPI request ID. |
None
|
Source code in anta/device.py
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 283 284 285 286 287 | |
collect_commands
async
¶
collect_commands(commands: list[AntaCommand], *, collection_id: str | None = None) -> None
Collect multiple commands.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
commands
|
list[AntaCommand]
|
The commands to collect. |
required |
collection_id
|
str | None
|
An identifier used to build the eAPI request ID. |
None
|
Source code in anta/device.py
289 290 291 292 293 294 295 296 297 298 299 | |
copy
async
¶
Copy files to and from the device, usually through SCP.
It is not mandatory to implement this for a valid AntaDevice subclass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sources
|
list[Path]
|
List of files to copy to or from the device. |
required |
destination
|
Path
|
Local or remote destination when copying the files. Can be a folder. |
required |
direction
|
Literal['to', 'from']
|
Defines if this coroutine copies files to or from the device. |
'from'
|
Source code in anta/device.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
disconnect
async
¶
disconnect() -> None
Disconnect the device and close any open connections.
It is not mandatory to implement this for a valid AntaDevice subclass.
If disconnection logic is not needed, implement this method as pass.
Subclasses should document the concrete resources they close.
NOTE: In ANTA 2.0, this method will be made abstract and must be implemented by all subclasses.
Source code in anta/device.py
333 334 335 336 337 338 339 340 341 342 343 344 | |
refresh
abstractmethod
async
¶
refresh() -> None
Update attributes of an AntaDevice instance.
This coroutine must update the following attributes of AntaDevice:
-
is_online: When the device IP is reachable and a port can be open. -
established: When a command execution succeeds. -
hw_model: The hardware model of the device.
Source code in anta/device.py
301 302 303 304 305 306 307 308 309 310 311 312 | |
AsyncEOSDevice
¶
AsyncEOSDevice(host: str, username: str, password: str, name: str | None = None, enable_password: str | None = None, port: int | None = None, ssh_port: int = 22, tags: set[str] | None = None, timeout: float | None = None, proto: Literal['http', 'https'] = 'https', *, enable: bool = False, insecure: bool = False, disable_cache: bool = False, use_session_auth: bool = False)
Bases: AntaDevice
Implementation of AntaDevice for EOS using the asynceapi library, which is built on HTTPX.
Call disconnect() to close the eAPI httpx client. Call refresh() to re-establish
the eAPI connection; it automatically recreates _client if it has been closed.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Device name. |
is_online |
bool
|
True if the device IP is reachable and a port can be open. |
established |
bool
|
True if remote command execution succeeds. |
hw_model |
str
|
Hardware model of the device. |
tags |
set[str]
|
Tags for this device. |
enable |
bool
|
When True, commands are collected in privileged (enable) mode. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
Device FQDN or IP. |
required |
username
|
str
|
Username to connect to eAPI and SSH. |
required |
password
|
str
|
Password to connect to eAPI and SSH. |
required |
name
|
str | None
|
Device name. |
None
|
enable_password
|
str | None
|
Password used to gain privileged access on EOS. |
None
|
port
|
int | None
|
eAPI port. Defaults to 80 is proto is ‘http’ or 443 if proto is ‘https’. |
None
|
ssh_port
|
int
|
SSH port. |
22
|
tags
|
set[str] | None
|
Tags for this device. |
None
|
timeout
|
float | None
|
Global timeout value in seconds for outgoing eAPI calls. None means no timeout. |
None
|
proto
|
Literal['http', 'https']
|
eAPI protocol. Value can be ‘http’ or ‘https’. |
'https'
|
enable
|
bool
|
Collect commands using privileged mode. |
False
|
insecure
|
bool
|
Disable SSH Host Key validation. |
False
|
disable_cache
|
bool
|
Disable caching for all commands for this device. |
False
|
use_session_auth
|
bool
|
Use eAPI cookie-session authentication for this device. |
False
|
capabilities
class-attribute
instance-attribute
¶
capabilities = AntaDeviceCapabilities(supports_session_auth=True)
Features supported by this device type.
max_connections
property
¶
max_connections: int | None
Maximum number of concurrent connections allowed by the device. Returns None if not available.
use_session_auth
property
¶
use_session_auth: bool
Whether eAPI cookie-session authentication is enabled for this device.
_collect
async
¶
_collect(command: AntaCommand, *, collection_id: str | None = None) -> None
Collect device command output from EOS using asynceapi.
Supports outformat json and text as output structure.
Gain privileged access using the enable_password attribute
of the AntaDevice instance if populated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
AntaCommand
|
The command to collect. |
required |
collection_id
|
str | None
|
An identifier used to build the eAPI request ID. |
None
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the eAPI client is closed. Call |
Source code in anta/device.py
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 | |
copy
async
¶
Copy files to and from the device using asyncssh.scp().
The SSH connection is established transiently and closed safely via an async with context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sources
|
list[Path]
|
List of files to copy to or from the device. |
required |
destination
|
Path
|
Local or remote destination when copying the files. Can be a folder. |
required |
direction
|
Literal['to', 'from']
|
Defines if this coroutine copies files to or from the device. |
'from'
|
Source code in anta/device.py
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 | |
disconnect
async
¶
disconnect() -> None
Close the eAPI httpx client.
Safe to call even if the client is already closed.
Use refresh() to reconnect.
Source code in anta/device.py
699 700 701 702 703 704 705 706 707 708 709 | |
refresh
async
¶
refresh() -> None
Update attributes of an AsyncEOSDevice instance.
If the eAPI client has been closed (e.g. after a disconnect() call), it is
automatically recreated before attempting to reach the device.
Updates the following attributes:
is_online: True when the eAPI HTTP endpoint responds successfully.established: True when a command execution succeeds.hw_model: Hardware model parsed fromshow version.
Source code in anta/device.py
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 | |