Skip to content

Commit

Permalink
feat: add methods to send api requests to OS
Browse files Browse the repository at this point in the history
  • Loading branch information
niek-mereu committed Jul 23, 2024
1 parent d5ae411 commit e83d617
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ services:
# The following configuration is based on the OpenSearch documentation:
# https://opensearch.org/docs/latest/opensearch/install/docker/
opensearch-node:
image: opensearchproject/opensearch:2.3.0
image: opensearchproject/opensearch:2.15.0
container_name: opensearch-node
environment:
cluster.name: opensearch-cluster
Expand Down Expand Up @@ -87,7 +87,7 @@ services:
- opensearch-net

opensearch-dashboards:
image: opensearchproject/opensearch-dashboards:2.3.0
image: opensearchproject/opensearch-dashboards:2.15.0
container_name: opensearch-dashboards
ports:
- 5601:5601
Expand Down
155 changes: 155 additions & 0 deletions osman/osman.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,68 @@ def upload_painless_script(self, source: dict, name: str) -> dict:
res["differences"] = diffs
logging.info("Template updated!")
return res

def update_cluster_settings(self, settings: dict) -> dict:
"""
Update cluster settings.
Parameters
----------
settings: dict
A dictionary containing the cluster settings to update. This can include
'persistent' and 'transient' settings.
Returns
-------
dict
Dictionary with the response from the OpenSearch cluster.
"""
try:
response = self.client.cluster.put_settings(body=settings)
logging.info("Cluster settings updated successfully.")
return response
except exceptions.OpenSearchException as e:
logging.error("Failed to update cluster settings: %s", e)
raise RuntimeError(f"Failed to update cluster settings: {e}") from e

def register_model_group(self, name: str, description: str, access_mode: str = "public") -> dict:
"""
Register a new model group in OpenSearch.
Parameters
----------
name : str
The name of the model group.
description : str
A description of the model group.
access_mode : str
Access mode of the model group, defaults to 'public'.
Returns
-------
dict
Dictionary containing the response from the OpenSearch server.
"""
# Construct the payload for the POST request
payload = {
"name": name,
"description": description,
"access_mode": access_mode
}

# Define the endpoint for model group registration
endpoint = "/_plugins/_ml/model_groups/_register"

try:
# Make the POST request to register the model group
response = self.client.transport.perform_request('POST', endpoint, body=json.dumps(payload))
logging.info("Model group registered successfully.")
return response
except exceptions.OpenSearchException as e:
logging.error(f"Failed to register model group: {e}")
raise RuntimeError(f"Failed to register model group: {e}") from e



def debug_painless_script(
self,
Expand Down Expand Up @@ -630,3 +692,96 @@ def debug_painless_script(
assert res["result"] == expected_result

return res

def send_post_request(self, endpoint: str, payload: dict) -> dict:
"""
Send a POST request to a specified endpoint in OpenSearch.
Parameters
----------
endpoint : str
The API endpoint to which the POST request will be sent.
payload : dict
The payload for the POST request, structured as a dictionary.
Returns
-------
dict
Dictionary containing the response from the OpenSearch server.
Raises
------
RuntimeError
If the POST request fails or OpenSearch returns an error.
"""
try:
# Make the POST request to the specified endpoint
response = self.client.transport.perform_request('POST', endpoint, body=json.dumps(payload))
logging.info(f"POST request to {endpoint} successful.")
return response
except exceptions.OpenSearchException as e:
logging.error(f"Failed to send POST request to {endpoint}: {e}")
raise RuntimeError(f"Failed to send POST request to {endpoint}: {e}") from e

def send_get_request(self, endpoint: str) -> dict:
"""
Send a GET request to a specified endpoint in OpenSearch.
Parameters
----------
endpoint : str
The API endpoint to which the GET request will be sent.
Returns
-------
dict
Dictionary containing the response from the OpenSearch server.
Raises
------
RuntimeError
If the GET request fails or OpenSearch returns an error.
"""
try:
# Make the GET request to the specified endpoint
response = self.client.transport.perform_request('GET', endpoint)
logging.info(f"GET request to {endpoint} successful.")
return response
except exceptions.OpenSearchException as e:
logging.error(f"Failed to send GET request to {endpoint}: {e}")
raise RuntimeError(f"Failed to send GET request to {endpoint}: {e}") from e

def send_put_request(self, endpoint: str, payload: dict) -> dict:
"""
Send a PUT request to a specified endpoint in OpenSearch.
Parameters
----------
endpoint : str
The API endpoint to which the PUT request will be sent.
payload : dict
The payload for the PUT request, structured as a dictionary.
Returns
-------
dict
Dictionary containing the response from the OpenSearch server.
Raises
------
RuntimeError
If the PUT request fails or OpenSearch returns an error.
"""
try:
# Serialize the payload into JSON format
json_payload = json.dumps(payload)
# Make the PUT request to the specified endpoint
response = self.client.transport.perform_request('PUT', endpoint, body=json_payload)
logging.info(f"PUT request to {endpoint} successful.")
return response
except exceptions.OpenSearchException as e:
logging.error(f"Failed to send PUT request to {endpoint}: {e}")
raise RuntimeError(f"Failed to send PUT request to {endpoint}: {e}") from e



2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ charset-normalizer==2.1.1
click==8.1.3
idna==3.4
iniconfig==1.1.1
opensearch-py==2.0.0
opensearch-py==2.15
packaging==21.3
parameterized==0.8.1
pluggy==1.0.0
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"click>=8.1",
"idna>=3.4",
"iniconfig>=1.1",
"opensearch-py>=2.0",
"opensearch-py",
"packaging>=21.3",
"parameterized>=0.8.1",
"pluggy>=1.0",
Expand Down

0 comments on commit e83d617

Please sign in to comment.