From 258128cb6035fac3945b3c58197b1861851edd10 Mon Sep 17 00:00:00 2001 From: kalyan Date: Fri, 24 Nov 2023 17:55:32 +0530 Subject: [PATCH] add connectors Signed-off-by: kalyan --- opensearch_py_ml/ml_commons/connectors.py | 59 +++++++++++++++++++ .../ml_commons/ml_commons_client.py | 3 +- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 opensearch_py_ml/ml_commons/connectors.py diff --git a/opensearch_py_ml/ml_commons/connectors.py b/opensearch_py_ml/ml_commons/connectors.py new file mode 100644 index 000000000..18535341a --- /dev/null +++ b/opensearch_py_ml/ml_commons/connectors.py @@ -0,0 +1,59 @@ +# SPDX-License-Identifier: Apache-2.0 +# The OpenSearch Contributors require contributions made to +# this file be licensed under the Apache-2.0 license or a +# compatible open source license. +# Any modifications Copyright OpenSearch Contributors. See +# GitHub history for details. + +from opensearchpy import OpenSearch +from opensearch_py_ml.ml_commons.ml_common_utils import ML_BASE_URI + + +class Connectors: + def __init__(self, os_client: OpenSearch): + self.client = os_client + + def create_standalone_connector(self, payload: dict): + if not isinstance(payload, dict): + raise ValueError("payload needs to be a dictionary") + + return self.client.transport.perform_request( + method="POST", url=f"{ML_BASE_URI}/connectors/_create", body=payload + ) + + def create_internal_connector(self, payload: dict): + if not isinstance(payload, dict): + raise ValueError("payload needs to be a dictionary") + + return self.client.transport.perform_request( + method="POST", url=f"{ML_BASE_URI}/models/_register", body=payload + ) + + def list_connectors(self): + search_query = {"query": {"match_all": {}}} + return self.search_connectors(search_query) + + def search_connectors(self, search_query: dict): + if not isinstance(search_query, dict): + raise ValueError("search_query needs to be a dictionary") + + return self.client.transport.perform_request( + method="POST", url=f"{ML_BASE_URI}/connectors/_search", body=search_query + ) + + + def get_connector(self, connector_id: str): + if not isinstance(connector_id, str): + raise ValueError("connector_id needs to be a string") + + return self.client.transport.perform_request( + method="GET", url=f"{ML_BASE_URI}/connectors/{connector_id}" + ) + + def delete_connector(self, connector_id: str): + if not isinstance(connector_id, str): + raise ValueError("connector_id needs to be a string") + + return self.client.transport.perform_request( + method="DELETE", url=f"{ML_BASE_URI}/connectors/{connector_id}" + ) diff --git a/opensearch_py_ml/ml_commons/ml_commons_client.py b/opensearch_py_ml/ml_commons/ml_commons_client.py index 72e2e158b..7548933cd 100644 --- a/opensearch_py_ml/ml_commons/ml_commons_client.py +++ b/opensearch_py_ml/ml_commons/ml_commons_client.py @@ -24,7 +24,7 @@ from opensearch_py_ml.ml_commons.model_access_control import ModelAccessControl from opensearch_py_ml.ml_commons.model_execute import ModelExecute from opensearch_py_ml.ml_commons.model_uploader import ModelUploader - +from opensearch_py_ml.ml_commons.connectors import Connectors class MLCommonClient: """ @@ -37,6 +37,7 @@ def __init__(self, os_client: OpenSearch): self._model_uploader = ModelUploader(os_client) self._model_execute = ModelExecute(os_client) self.model_access_control = ModelAccessControl(os_client) + self.connectors = Connectors(os_client) def execute(self, algorithm_name: str, input_json: dict) -> dict: """