Adding upstream version 1.3.0.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
6fd6eb426a
commit
dc7df702ea
337 changed files with 16571 additions and 4891 deletions
|
@ -1,4 +1,4 @@
|
|||
# Copyright (c) 2024 Arista Networks, Inc.
|
||||
# Copyright (c) 2024-2025 Arista Networks, Inc.
|
||||
# Use of this source code is governed by the Apache License 2.0
|
||||
# that can be found in the LICENSE file.
|
||||
# Initially written by Jeremy Schulman at https://github.com/jeremyschulman/aio-eapi
|
||||
|
@ -10,7 +10,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from socket import getservbyname
|
||||
from typing import TYPE_CHECKING, Any
|
||||
from typing import TYPE_CHECKING, Any, Literal, overload
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Public Imports
|
||||
|
@ -20,12 +20,13 @@ import httpx
|
|||
# -----------------------------------------------------------------------------
|
||||
# Private Imports
|
||||
# -----------------------------------------------------------------------------
|
||||
from ._constants import EapiCommandFormat
|
||||
from .aio_portcheck import port_check_url
|
||||
from .config_session import SessionConfig
|
||||
from .errors import EapiCommandError
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Sequence
|
||||
from ._types import EapiComplexCommand, EapiJsonOutput, EapiSimpleCommand, EapiTextOutput, JsonRpc
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Exports
|
||||
|
@ -121,18 +122,139 @@ class Device(httpx.AsyncClient):
|
|||
"""
|
||||
return await port_check_url(self.base_url)
|
||||
|
||||
# Single command, JSON output, no suppression
|
||||
@overload
|
||||
async def cli(
|
||||
self,
|
||||
command: str | dict[str, Any] | None = None,
|
||||
commands: Sequence[str | dict[str, Any]] | None = None,
|
||||
ofmt: str | None = None,
|
||||
version: int | str | None = "latest",
|
||||
*,
|
||||
command: EapiSimpleCommand | EapiComplexCommand,
|
||||
commands: None = None,
|
||||
ofmt: Literal["json"] = "json",
|
||||
version: int | Literal["latest"] = "latest",
|
||||
suppress_error: Literal[False] = False,
|
||||
auto_complete: bool = False,
|
||||
expand_aliases: bool = False,
|
||||
req_id: int | str | None = None,
|
||||
) -> EapiJsonOutput: ...
|
||||
|
||||
# Multiple commands, JSON output, no suppression
|
||||
@overload
|
||||
async def cli(
|
||||
self,
|
||||
*,
|
||||
command: None = None,
|
||||
commands: list[EapiSimpleCommand | EapiComplexCommand],
|
||||
ofmt: Literal["json"] = "json",
|
||||
version: int | Literal["latest"] = "latest",
|
||||
suppress_error: Literal[False] = False,
|
||||
auto_complete: bool = False,
|
||||
expand_aliases: bool = False,
|
||||
req_id: int | str | None = None,
|
||||
) -> list[EapiJsonOutput]: ...
|
||||
|
||||
# Single command, TEXT output, no suppression
|
||||
@overload
|
||||
async def cli(
|
||||
self,
|
||||
*,
|
||||
command: EapiSimpleCommand | EapiComplexCommand,
|
||||
commands: None = None,
|
||||
ofmt: Literal["text"],
|
||||
version: int | Literal["latest"] = "latest",
|
||||
suppress_error: Literal[False] = False,
|
||||
auto_complete: bool = False,
|
||||
expand_aliases: bool = False,
|
||||
req_id: int | str | None = None,
|
||||
) -> EapiTextOutput: ...
|
||||
|
||||
# Multiple commands, TEXT output, no suppression
|
||||
@overload
|
||||
async def cli(
|
||||
self,
|
||||
*,
|
||||
command: None = None,
|
||||
commands: list[EapiSimpleCommand | EapiComplexCommand],
|
||||
ofmt: Literal["text"],
|
||||
version: int | Literal["latest"] = "latest",
|
||||
suppress_error: Literal[False] = False,
|
||||
auto_complete: bool = False,
|
||||
expand_aliases: bool = False,
|
||||
req_id: int | str | None = None,
|
||||
) -> list[EapiTextOutput]: ...
|
||||
|
||||
# Single command, JSON output, with suppression
|
||||
@overload
|
||||
async def cli(
|
||||
self,
|
||||
*,
|
||||
command: EapiSimpleCommand | EapiComplexCommand,
|
||||
commands: None = None,
|
||||
ofmt: Literal["json"] = "json",
|
||||
version: int | Literal["latest"] = "latest",
|
||||
suppress_error: Literal[True],
|
||||
auto_complete: bool = False,
|
||||
expand_aliases: bool = False,
|
||||
req_id: int | str | None = None,
|
||||
) -> EapiJsonOutput | None: ...
|
||||
|
||||
# Multiple commands, JSON output, with suppression
|
||||
@overload
|
||||
async def cli(
|
||||
self,
|
||||
*,
|
||||
command: None = None,
|
||||
commands: list[EapiSimpleCommand | EapiComplexCommand],
|
||||
ofmt: Literal["json"] = "json",
|
||||
version: int | Literal["latest"] = "latest",
|
||||
suppress_error: Literal[True],
|
||||
auto_complete: bool = False,
|
||||
expand_aliases: bool = False,
|
||||
req_id: int | str | None = None,
|
||||
) -> list[EapiJsonOutput] | None: ...
|
||||
|
||||
# Single command, TEXT output, with suppression
|
||||
@overload
|
||||
async def cli(
|
||||
self,
|
||||
*,
|
||||
command: EapiSimpleCommand | EapiComplexCommand,
|
||||
commands: None = None,
|
||||
ofmt: Literal["text"],
|
||||
version: int | Literal["latest"] = "latest",
|
||||
suppress_error: Literal[True],
|
||||
auto_complete: bool = False,
|
||||
expand_aliases: bool = False,
|
||||
req_id: int | str | None = None,
|
||||
) -> EapiTextOutput | None: ...
|
||||
|
||||
# Multiple commands, TEXT output, with suppression
|
||||
@overload
|
||||
async def cli(
|
||||
self,
|
||||
*,
|
||||
command: None = None,
|
||||
commands: list[EapiSimpleCommand | EapiComplexCommand],
|
||||
ofmt: Literal["text"],
|
||||
version: int | Literal["latest"] = "latest",
|
||||
suppress_error: Literal[True],
|
||||
auto_complete: bool = False,
|
||||
expand_aliases: bool = False,
|
||||
req_id: int | str | None = None,
|
||||
) -> list[EapiTextOutput] | None: ...
|
||||
|
||||
# Actual implementation
|
||||
async def cli(
|
||||
self,
|
||||
command: EapiSimpleCommand | EapiComplexCommand | None = None,
|
||||
commands: list[EapiSimpleCommand | EapiComplexCommand] | None = None,
|
||||
ofmt: Literal["json", "text"] = "json",
|
||||
version: int | Literal["latest"] = "latest",
|
||||
*,
|
||||
suppress_error: bool = False,
|
||||
auto_complete: bool = False,
|
||||
expand_aliases: bool = False,
|
||||
req_id: int | str | None = None,
|
||||
) -> list[dict[str, Any] | str] | dict[str, Any] | str | None:
|
||||
) -> EapiJsonOutput | EapiTextOutput | list[EapiJsonOutput] | list[EapiTextOutput] | None:
|
||||
"""Execute one or more CLI commands.
|
||||
|
||||
Parameters
|
||||
|
@ -143,6 +265,7 @@ class Device(httpx.AsyncClient):
|
|||
A list of commands to execute; results in a list of output responses.
|
||||
ofmt
|
||||
Either 'json' or 'text'; indicates the output format for the CLI commands.
|
||||
eAPI defaults to 'json'.
|
||||
version
|
||||
By default the eAPI will use "version 1" for all API object models.
|
||||
This driver will, by default, always set version to "latest" so
|
||||
|
@ -158,13 +281,13 @@ class Device(httpx.AsyncClient):
|
|||
|
||||
response = dev.cli(..., suppress_error=True)
|
||||
auto_complete
|
||||
Enabled/disables the command auto-compelete feature of the EAPI. Per the
|
||||
Enabled/disables the command auto-compelete feature of the eAPI. Per the
|
||||
documentation:
|
||||
Allows users to use shorthand commands in eAPI calls. With this
|
||||
parameter included a user can send 'sh ver' via eAPI to get the
|
||||
output of 'show version'.
|
||||
expand_aliases
|
||||
Enables/disables the command use of User defined alias. Per the
|
||||
Enables/disables the command use of user-defined alias. Per the
|
||||
documentation:
|
||||
Allowed users to provide the expandAliases parameter to eAPI
|
||||
calls. This allows users to use aliased commands via the API.
|
||||
|
@ -176,15 +299,34 @@ class Device(httpx.AsyncClient):
|
|||
|
||||
Returns
|
||||
-------
|
||||
list[dict[str, Any] | str] | dict[str, Any] | str | None
|
||||
One or List of output responses, per the description above.
|
||||
dict[str, Any]
|
||||
Single command, JSON output, suppress_error=False
|
||||
list[dict[str, Any]]
|
||||
Multiple commands, JSON output, suppress_error=False
|
||||
str
|
||||
Single command, TEXT output, suppress_error=False
|
||||
list[str]
|
||||
Multiple commands, TEXT output, suppress_error=False
|
||||
dict[str, Any] | None
|
||||
Single command, JSON output, suppress_error=True
|
||||
list[dict[str, Any]] | None
|
||||
Multiple commands, JSON output, suppress_error=True
|
||||
str | None
|
||||
Single command, TEXT output, suppress_error=True
|
||||
list[str] | None
|
||||
Multiple commands, TEXT output, suppress_error=True
|
||||
"""
|
||||
if not any((command, commands)):
|
||||
msg = "Required 'command' or 'commands'"
|
||||
raise RuntimeError(msg)
|
||||
|
||||
jsonrpc = self._jsonrpc_command(
|
||||
commands=[command] if command else commands, ofmt=ofmt, version=version, auto_complete=auto_complete, expand_aliases=expand_aliases, req_id=req_id
|
||||
commands=[command] if command else commands if commands else [],
|
||||
ofmt=ofmt,
|
||||
version=version,
|
||||
auto_complete=auto_complete,
|
||||
expand_aliases=expand_aliases,
|
||||
req_id=req_id,
|
||||
)
|
||||
|
||||
try:
|
||||
|
@ -197,14 +339,14 @@ class Device(httpx.AsyncClient):
|
|||
|
||||
def _jsonrpc_command(
|
||||
self,
|
||||
commands: Sequence[str | dict[str, Any]] | None = None,
|
||||
ofmt: str | None = None,
|
||||
version: int | str | None = "latest",
|
||||
commands: list[EapiSimpleCommand | EapiComplexCommand],
|
||||
ofmt: Literal["json", "text"] = "json",
|
||||
version: int | Literal["latest"] = "latest",
|
||||
*,
|
||||
auto_complete: bool = False,
|
||||
expand_aliases: bool = False,
|
||||
req_id: int | str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
) -> JsonRpc:
|
||||
"""Create the JSON-RPC command dictionary object.
|
||||
|
||||
Parameters
|
||||
|
@ -213,6 +355,7 @@ class Device(httpx.AsyncClient):
|
|||
A list of commands to execute; results in a list of output responses.
|
||||
ofmt
|
||||
Either 'json' or 'text'; indicates the output format for the CLI commands.
|
||||
eAPI defaults to 'json'.
|
||||
version
|
||||
By default the eAPI will use "version 1" for all API object models.
|
||||
This driver will, by default, always set version to "latest" so
|
||||
|
@ -241,25 +384,20 @@ class Device(httpx.AsyncClient):
|
|||
dict containing the JSON payload to run the command.
|
||||
|
||||
"""
|
||||
cmd: dict[str, Any] = {
|
||||
return {
|
||||
"jsonrpc": "2.0",
|
||||
"method": "runCmds",
|
||||
"params": {
|
||||
"version": version,
|
||||
"cmds": commands,
|
||||
"format": ofmt or self.EAPI_DEFAULT_OFMT,
|
||||
"format": EapiCommandFormat(ofmt),
|
||||
"autoComplete": auto_complete,
|
||||
"expandAliases": expand_aliases,
|
||||
},
|
||||
"id": req_id or id(self),
|
||||
}
|
||||
if auto_complete is not None:
|
||||
cmd["params"].update({"autoComplete": auto_complete})
|
||||
|
||||
if expand_aliases is not None:
|
||||
cmd["params"].update({"expandAliases": expand_aliases})
|
||||
|
||||
return cmd
|
||||
|
||||
async def jsonrpc_exec(self, jsonrpc: dict[str, Any]) -> list[dict[str, Any] | str]:
|
||||
async def jsonrpc_exec(self, jsonrpc: JsonRpc) -> list[EapiJsonOutput] | list[EapiTextOutput]:
|
||||
"""Execute the JSON-RPC dictionary object.
|
||||
|
||||
Parameters
|
||||
|
@ -315,7 +453,7 @@ class Device(httpx.AsyncClient):
|
|||
failed_cmd = commands[err_at]
|
||||
|
||||
raise EapiCommandError(
|
||||
passed=[get_output(cmd_data[cmd_i]) for cmd_i, cmd in enumerate(commands[:err_at])],
|
||||
passed=[get_output(cmd_data[i]) for i in range(err_at)],
|
||||
failed=failed_cmd["cmd"] if isinstance(failed_cmd, dict) else failed_cmd,
|
||||
errors=cmd_data[err_at]["errors"],
|
||||
errmsg=err_msg,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue