From 1b5f4b96ca4893405b56846f174a0fac338c7141 Mon Sep 17 00:00:00 2001 From: Marcel Suleiman Date: Mon, 20 Nov 2023 09:43:07 +0100 Subject: [PATCH] add device discovery task --- .../uniconfig/device_discovery.py | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 uniconfig/python/frinx_worker/uniconfig/device_discovery.py diff --git a/uniconfig/python/frinx_worker/uniconfig/device_discovery.py b/uniconfig/python/frinx_worker/uniconfig/device_discovery.py new file mode 100644 index 0000000..0e755b4 --- /dev/null +++ b/uniconfig/python/frinx_worker/uniconfig/device_discovery.py @@ -0,0 +1,59 @@ +import requests +from frinx.common.conductor_enums import TaskResultStatus +from frinx.common.frinx_rest import UNICONFIG_URL_BASE +from frinx.common.type_aliases import DictAny +from frinx.common.type_aliases import ListStr +from frinx.common.worker.service import ServiceWorkersImpl +from frinx.common.worker.service import WorkerImpl +from frinx.common.worker.task_def import TaskDefinition +from frinx.common.worker.task_def import TaskExecutionProperties +from frinx.common.worker.task_def import TaskInput +from frinx.common.worker.task_def import TaskOutput +from frinx.common.worker.task_result import TaskResult +from frinx_api.uniconfig.rest_api import Discover + +from frinx_worker.uniconfig import class_to_json + + +class DeviceDiscoveryWorkers(ServiceWorkersImpl): + class UniconfigDeviceDiscovery(WorkerImpl): + class ExecutionProperties(TaskExecutionProperties): + exclude_empty_inputs: bool = False + transform_string_to_json_valid: bool = True + + class WorkerDefinition(TaskDefinition): + name: str = 'UNICONFIG_device_discovery' + description: str = 'Verification of reachable devices in a network.' + labels: ListStr = ['BASICS', 'UNICONFIG'] + + class WorkerInput(TaskInput): + template: DictAny + + class WorkerOutput(TaskOutput): + output: DictAny + + def execute(self, worker_input: WorkerInput) -> TaskResult[WorkerOutput]: + if Discover.request is None: + raise Exception(f'Failed to create request {Discover.request}') + + response = requests.request( + url=UNICONFIG_URL_BASE + Discover.uri, + method=Discover.method, + + data=class_to_json( + Discover.request( + **worker_input.template + ), + ), + ) + + status = TaskResultStatus.COMPLETED + if response.status_code > 299: + status = TaskResultStatus.FAILED + + return TaskResult( + status=status, + output=self.WorkerOutput( + output=response.json() + ) + )