|
| 1 | +import requests |
| 2 | +from frinx.common.frinx_rest import UNICONFIG_HEADERS |
| 3 | +from frinx.common.frinx_rest import UNICONFIG_REQUEST_PARAMS |
| 4 | +from frinx.common.frinx_rest import UNICONFIG_URL_BASE |
| 5 | +from frinx.common.type_aliases import DictAny |
| 6 | +from frinx.common.worker.service import ServiceWorkersImpl |
| 7 | +from frinx.common.worker.task_def import TaskDefinition |
| 8 | +from frinx.common.worker.task_def import TaskExecutionProperties |
| 9 | +from frinx.common.worker.task_def import TaskInput |
| 10 | +from frinx.common.worker.task_def import TaskOutput |
| 11 | +from frinx.common.worker.task_result import TaskResult |
| 12 | +from frinx.common.worker.worker import WorkerImpl |
| 13 | + |
| 14 | +from . import class_to_json |
| 15 | +from . import handle_response |
| 16 | +from . import uniconfig_zone_to_cookie |
| 17 | + |
| 18 | + |
| 19 | +class UniconfigQueryWorkers(ServiceWorkersImpl): |
| 20 | + class UniconfigQuery(WorkerImpl): |
| 21 | + from frinx_api.uniconfig.rest_api import QueryConfig as UniconfigApi |
| 22 | + from frinx_api.uniconfig.uniconfig.query.queryconfig import Input as UniconfigQueryConfigInput |
| 23 | + |
| 24 | + |
| 25 | + class ExecutionProperties(TaskExecutionProperties): |
| 26 | + exclude_empty_inputs: bool = True |
| 27 | + transform_string_to_json_valid: bool = True |
| 28 | + |
| 29 | + class WorkerDefinition(TaskDefinition): |
| 30 | + name: str = "UNICONFIG_Query_config_RPC" |
| 31 | + description: str = "Application JSONB filtering (JSONPath with dot/bracket notation)" |
| 32 | + |
| 33 | + class WorkerInput(TaskInput, UniconfigQueryConfigInput): |
| 34 | + transaction_id: str | None = None |
| 35 | + uniconfig_server_id: str | None = None |
| 36 | + uniconfig_url_base: str = UNICONFIG_URL_BASE |
| 37 | + |
| 38 | + class WorkerOutput(TaskOutput): |
| 39 | + output: DictAny |
| 40 | + |
| 41 | + def execute(self, worker_input: WorkerInput) -> TaskResult[WorkerOutput]: |
| 42 | + if self.UniconfigApi.request is None: |
| 43 | + raise Exception(f"Failed to create request {self.UniconfigApi.request}") |
| 44 | + |
| 45 | + response = requests.request( |
| 46 | + url=worker_input.uniconfig_url_base + self.UniconfigApi.uri, |
| 47 | + method=self.UniconfigApi.method, |
| 48 | + data=class_to_json( |
| 49 | + self.UniconfigApi.request( |
| 50 | + input=self.UniconfigQueryConfigInput( |
| 51 | + jsonb_path_query=worker_input.jsonb_path_query, |
| 52 | + node_id=worker_input.node_id, |
| 53 | + topology_id=worker_input.topology_id |
| 54 | + ) |
| 55 | + ) |
| 56 | + ), |
| 57 | + cookies=uniconfig_zone_to_cookie( |
| 58 | + uniconfig_server_id=worker_input.uniconfig_server_id, transaction_id=worker_input.transaction_id |
| 59 | + ), |
| 60 | + headers=dict(UNICONFIG_HEADERS), |
| 61 | + params=UNICONFIG_REQUEST_PARAMS, |
| 62 | + ) |
| 63 | + |
| 64 | + return handle_response(response, self.WorkerOutput) |
| 65 | + |
0 commit comments