Device
AntaDevice base class¶
AntaDevice ¶
AntaDevice(
name: str,
tags: set[str] | None = None,
*,
disable_cache: bool = False
)
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
|
Hardware model of the device. |
tags |
set[str]
|
Tags for this device. |
cache |
Cache | None
|
In-memory cache from aiocache library for this device (None if cache is disabled). |
cache_locks |
dict
|
Dictionary mapping keys to asyncio locks to guarantee exclusive access to the cache if not disabled. |
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
|
Source code in anta/device.py
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 |
|
cache_statistics
property
¶
cache_statistics: dict[str, Any] | None
Return the device cache statistics for logging purposes.
__hash__ ¶
__hash__() -> int
Implement hashing for AntaDevice objects.
Source code in anta/device.py
98 99 100 |
|
__repr__ ¶
__repr__() -> str
Return a printable representation of an AntaDevice.
Source code in anta/device.py
129 130 131 132 133 134 135 136 137 138 |
|
_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
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
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
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 |
|
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
195 196 197 198 199 200 201 202 203 204 205 |
|
copy
async
¶
copy(
sources: list[Path],
destination: Path,
direction: Literal["to", "from"] = "from",
) -> None
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
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
|
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
207 208 209 210 211 212 213 214 215 216 217 218 |
|
Async EOS device class¶
AsyncEOSDevice ¶
AsyncEOSDevice(
host: str,
username: str,
password: str,
name: str | None = None,
enable_password: str | None = None,
port: int | None = None,
ssh_port: int | None = 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
)
Bases: AntaDevice
Implementation of AntaDevice for EOS using aio-eapi.
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. |
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 |
bool
|
Collect commands using privileged mode. |
False
|
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 | None
|
SSH port. |
22
|
tags |
set[str] | None
|
Tags for this device. |
None
|
timeout |
float | None
|
Timeout value in seconds for outgoing API calls. |
None
|
insecure |
bool
|
Disable SSH Host Key validation. |
False
|
proto |
Literal['http', 'https']
|
eAPI protocol. Value can be ‘http’ or ‘https’. |
'https'
|
disable_cache |
bool
|
Disable caching for all commands for this device. |
False
|
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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
|
__repr__ ¶
__repr__() -> str
Return a printable representation of an AsyncEOSDevice.
Source code in anta/device.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
_collect
async
¶
_collect(
command: AntaCommand,
*,
collection_id: str | None = None
) -> None
Collect device command output from EOS using aio-eapi.
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
|
Source code in anta/device.py
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
|
copy
async
¶
copy(
sources: list[Path],
destination: Path,
direction: Literal["to", "from"] = "from",
) -> None
Copy files to and from the device using asyncssh.scp().
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
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
|
refresh
async
¶
refresh() -> None
Update attributes of an AsyncEOSDevice instance.
This coroutine must update the following attributes of AsyncEOSDevice: - is_online: When a 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
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
|