From e315af40c61e9783ee1099427e86935e1bdb6c28 Mon Sep 17 00:00:00 2001 From: Lachlan Miskin Date: Mon, 24 Jul 2023 14:01:36 +1000 Subject: [PATCH] Update to pydantic v2.1, including removing some un-used models and correcting some field types. --- netpalm/backend/core/calls/scriptrunner/script.py | 2 +- netpalm/backend/core/models/models.py | 10 +++++----- netpalm/backend/core/models/napalm.py | 4 ++-- netpalm/backend/core/models/ncclient.py | 9 +++++---- netpalm/backend/core/models/netmiko.py | 6 +++--- netpalm/backend/core/models/puresnmp.py | 2 +- netpalm/backend/core/models/restconf.py | 2 +- netpalm/backend/core/models/service.py | 12 +++--------- netpalm/backend/core/models/task.py | 10 +++++----- .../extensibles/custom_scripts/hello_world_model.py | 2 +- netpalm/requirements.txt | 2 +- netpalm/routers/service.py | 2 +- 12 files changed, 29 insertions(+), 34 deletions(-) diff --git a/netpalm/backend/core/calls/scriptrunner/script.py b/netpalm/backend/core/calls/scriptrunner/script.py index ef4f8475..18d2e754 100644 --- a/netpalm/backend/core/calls/scriptrunner/script.py +++ b/netpalm/backend/core/calls/scriptrunner/script.py @@ -93,7 +93,7 @@ def script_kiddy(**kwargs): try: log.debug(f"script_kiddy: attempting to run script {script_path_full_name}") - if not model_is_defined or model_mode is "legacy": + if not model_is_defined or model_mode == "legacy": result = runscrp(kwargs=arg) else: data_to_send = model_to_validate(**kwarg) diff --git a/netpalm/backend/core/models/models.py b/netpalm/backend/core/models/models.py index 58f06cc3..d5b2436f 100644 --- a/netpalm/backend/core/models/models.py +++ b/netpalm/backend/core/models/models.py @@ -67,7 +67,7 @@ class SetConfig(BaseModel): ttl: Optional[int] = None class Config: - schema_extra = { + json_schema_extra = { "example": { "library": "napalm", @@ -109,7 +109,7 @@ class CacheConfig(BaseModel): poison: Optional[bool] = False class Config: - schema_extra = { + json_schema_extra = { 'example': { 'enabled': True, 'ttl': 300, @@ -126,7 +126,7 @@ class Script(BaseModel): ttl: Optional[int] = None class Config: - schema_extra = { + json_schema_extra = { "example": { "script": "hello_world", "args": { @@ -144,7 +144,7 @@ class ScriptCustom(BaseModel): ttl: Optional[int] = None class Config: - schema_extra = { + json_schema_extra = { "example": { "script": "hello_world", "queue_strategy": "fifo" @@ -163,7 +163,7 @@ class GetConfig(BaseModel): ttl: Optional[int] = None class Config: - schema_extra = { + json_schema_extra = { "example": { "library": "netmiko", "connection_args": { diff --git a/netpalm/backend/core/models/napalm.py b/netpalm/backend/core/models/napalm.py index 8c2140cd..2461efc2 100644 --- a/netpalm/backend/core/models/napalm.py +++ b/netpalm/backend/core/models/napalm.py @@ -45,7 +45,7 @@ class NapalmGetConfig(BaseModel): cache: Optional[CacheConfig] = {} class Config: - schema_extra = { + json_schema_extra = { "example": { "library": "napalm", "connection_args": { @@ -72,7 +72,7 @@ class NapalmSetConfig(BaseModel): post_checks: Optional[List[GenericPrePostCheck]] = None class Config: - schema_extra = { + json_schema_extra = { "example": { "library": "napalm", "connection_args": { diff --git a/netpalm/backend/core/models/ncclient.py b/netpalm/backend/core/models/ncclient.py index eb698435..d79db54c 100644 --- a/netpalm/backend/core/models/ncclient.py +++ b/netpalm/backend/core/models/ncclient.py @@ -29,7 +29,7 @@ class NcclientGetRpcArgs(BaseModel): capabilities: Optional[bool] = False -class NcclientDeviceDrivers(str): +class NcclientDeviceDrivers(str, Enum): default = "default" hpcomware = "hpcomware" h3c = "h3c" @@ -46,6 +46,7 @@ class NcclientDeviceDrivers(str): class NcclientDeviceParams(BaseModel): name: NcclientDeviceDrivers + class NcclientManagerParams(BaseModel): timeout: int @@ -74,7 +75,7 @@ class NcclientSetConfig(BaseModel): ttl: Optional[int] = None class Config: - schema_extra = { + json_schema_extra = { "example": { "library": "ncclient", "connection_args": { @@ -104,7 +105,7 @@ class NcclientGetConfig(BaseModel): ttl: Optional[int] = None class Config: - schema_extra = { + json_schema_extra = { "example": { "library": "ncclient", "connection_args": { @@ -139,7 +140,7 @@ class NcclientGet(BaseModel): ttl: Optional[int] = None class Config: - schema_extra = { + json_schema_extra = { "example": { "library": "ncclient", "connection_args": { diff --git a/netpalm/backend/core/models/netmiko.py b/netpalm/backend/core/models/netmiko.py index 079c3590..c86bf0cd 100644 --- a/netpalm/backend/core/models/netmiko.py +++ b/netpalm/backend/core/models/netmiko.py @@ -58,7 +58,7 @@ class NetmikoConnectionArgs(BaseModel): serial_settings: Optional[str] = None fast_cli: Optional[bool] = False session_log: Optional[str] = None - session_log_record_writes = False + session_log_record_writes: Optional[bool] = False session_log_file_mode: Optional[str] = "write" allow_auto_change: Optional[bool] = False encoding: Optional[str] = "ascii" @@ -78,7 +78,7 @@ class NetmikoGetConfig(BaseModel): enable_mode: Optional[bool] = False class Config: - schema_extra = { + json_schema_extra = { "example": { "library": "netmiko", "connection_args": { @@ -114,7 +114,7 @@ class NetmikoSetConfig(BaseModel): ttl: Optional[int] = None class Config: - schema_extra = { + json_schema_extra = { "example": { "library": "netmiko", "connection_args": { diff --git a/netpalm/backend/core/models/puresnmp.py b/netpalm/backend/core/models/puresnmp.py index 02841172..4379ea41 100644 --- a/netpalm/backend/core/models/puresnmp.py +++ b/netpalm/backend/core/models/puresnmp.py @@ -37,7 +37,7 @@ class PureSNMPGetConfig(BaseModel): ttl: Optional[int] = None class Config: - schema_extra = { + json_schema_extra = { "example": { "library": "puresnmp", "connection_args": { diff --git a/netpalm/backend/core/models/restconf.py b/netpalm/backend/core/models/restconf.py index 447029bf..4a128d50 100644 --- a/netpalm/backend/core/models/restconf.py +++ b/netpalm/backend/core/models/restconf.py @@ -41,7 +41,7 @@ class Restconf(BaseModel): ttl: Optional[int] = None class Config: - schema_extra = { + json_schema_extra = { "example": { "library": "restconf", "connection_args": { diff --git a/netpalm/backend/core/models/service.py b/netpalm/backend/core/models/service.py index e2d3fa29..19466e95 100644 --- a/netpalm/backend/core/models/service.py +++ b/netpalm/backend/core/models/service.py @@ -42,7 +42,7 @@ class ServiceModel(BaseModel): ttl: Optional[int] = None class Config: - schema_extra = { + json_schema_extra = { "example": { "operation": "retrieve", "args": { @@ -52,24 +52,18 @@ class Config: } } + # now redundant class ServiceModelMethods(BaseModel): operation: ServiceLifecycle path: Optional[str] = None payload: dict + # now redundant class ServiceModelSupportedMethods(BaseModel): supported_methods: List[ServiceModelMethods] = None -# now redundant -class ServiceModelTemplate(BaseModel): - __root__: List[ServiceModelSupportedMethods] - class ServiceInventorySchema(BaseModel): service_meta: dict - - -class ServiceInventoryResponse(BaseModel): - __root__: List[ServiceInventorySchema] \ No newline at end of file diff --git a/netpalm/backend/core/models/task.py b/netpalm/backend/core/models/task.py index e4f8819e..ef37b0f7 100644 --- a/netpalm/backend/core/models/task.py +++ b/netpalm/backend/core/models/task.py @@ -22,8 +22,8 @@ class TaskMetaData(BaseModel): enqueued_at: Optional[str] started_at: Optional[str] ended_at: Optional[str] - enqueued_elapsed_seconds: Optional[str] - total_elapsed_seconds: Optional[str] + enqueued_elapsed_seconds: Optional[int] + total_elapsed_seconds: Optional[int] assigned_worker: Optional[str] class TaskError(BaseModel): @@ -56,7 +56,7 @@ class Response(BaseModel): data: TaskResponse class Config: - schema_extra = { + json_schema_extra = { "example": { "status": "success", "data": { @@ -99,7 +99,7 @@ class ServiceResponse(BaseModel): data: ServiceTaskResponse class Config: - schema_extra = { + json_schema_extra = { "example": { "status": "success", "data": { @@ -133,7 +133,7 @@ class ResponseBasic(BaseModel): class WorkerResponse(BaseModel): hostname: Optional[Any] = None - pid: str + pid: int name: Optional[Any] = None last_heartbeat: Optional[Any] = None birth_date: Optional[Any] = None diff --git a/netpalm/backend/plugins/extensibles/custom_scripts/hello_world_model.py b/netpalm/backend/plugins/extensibles/custom_scripts/hello_world_model.py index bb40d797..9029edff 100644 --- a/netpalm/backend/plugins/extensibles/custom_scripts/hello_world_model.py +++ b/netpalm/backend/plugins/extensibles/custom_scripts/hello_world_model.py @@ -25,7 +25,7 @@ class hello_world_model(BaseModel): class Config: # add an example payload under the "example" dict - schema_extra = { + json_schema_extra = { "example": { "script": "hello_world", "args": { diff --git a/netpalm/requirements.txt b/netpalm/requirements.txt index 8f67b95c..d1229bab 100644 --- a/netpalm/requirements.txt +++ b/netpalm/requirements.txt @@ -18,5 +18,5 @@ filelock jsonpath_ng apscheduler==3.6.3 puresnmp==1.9.1 -pydantic +pydantic>=2.1 names_generator==0.1.0 \ No newline at end of file diff --git a/netpalm/routers/service.py b/netpalm/routers/service.py index 08cf79da..af95f6ae 100644 --- a/netpalm/routers/service.py +++ b/netpalm/routers/service.py @@ -12,7 +12,7 @@ from netpalm.backend.core.confload.confload import config # load models -from netpalm.backend.core.models.service import ServiceModel, ServiceInventoryResponse +from netpalm.backend.core.models.service import ServiceModel from netpalm.backend.core.models.task import ServiceResponse, Response, ResponseBasic from netpalm.backend.core.manager import ntplm