generated from MITLibraries/python-cli-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Why these changes are being introduced: Pinging the OpenSearch instance and printing information about it is the first step towards implementing real functionality in this app. How this addresses that need: * Adds an opensearch module that configures an OpenSearch client for either local or AWS hosted OpenSearch. * Updates the main cli command to take a url option, pulled from env if not provided and with a localhost default, to set the OpenSearch instance url. * Updates the main cli command to configure the OpenSearch client using the provided or default url. * Updates the ping command to get and log basic info about the OpenSearch cluster. * Adds tests for all new functionality. * Updates README with information on running and connecting to a local OpenSearch instance with Docker or connecting to an instance on AWS. * Updates mypy and isort configurations. * Updates Python version and dependencies. Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/TIMX-72
- Loading branch information
Showing
11 changed files
with
467 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
3.10.3 | ||
3.10.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
interactions: | ||
- request: | ||
body: null | ||
headers: | ||
content-type: | ||
- application/json | ||
user-agent: | ||
- opensearch-py/2.0.0 (Python 3.10.3) | ||
method: GET | ||
uri: http://localhost:9200/ | ||
response: | ||
body: | ||
string: | ||
"{\n \"name\" : \"6370d6be26b2\",\n \"cluster_name\" : \"docker-cluster\",\n | ||
\ \"cluster_uuid\" : \"j7tpRLtKTsSRlyng3RELug\",\n \"version\" : {\n \"distribution\" | ||
: \"opensearch\",\n \"number\" : \"1.3.3\",\n \"build_type\" : \"tar\",\n | ||
\ \"build_hash\" : \"6d63fc707f6010c5980ec5cb51324856caf1f03d\",\n \"build_date\" | ||
: \"2022-06-07T15:24:06.598626Z\",\n \"build_snapshot\" : false,\n \"lucene_version\" | ||
: \"8.10.1\",\n \"minimum_wire_compatibility_version\" : \"6.8.0\",\n \"minimum_index_compatibility_version\" | ||
: \"6.0.0-beta1\"\n },\n \"tagline\" : \"The OpenSearch Project: https://opensearch.org/\"\n}\n" | ||
headers: | ||
content-length: | ||
- "569" | ||
content-type: | ||
- application/json; charset=UTF-8 | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from unittest import mock | ||
|
||
from tim.opensearch import configure_opensearch_client | ||
|
||
|
||
def test_configure_opensearch_client_for_localhost(): | ||
result = configure_opensearch_client("localhost") | ||
assert str(result) == "<OpenSearch([{'host': 'localhost', 'port': '9200'}])>" | ||
|
||
|
||
@mock.patch("boto3.session.Session") | ||
def test_configure_opensearch_client_for_aws(mocked_boto3_session): # noqa | ||
result = configure_opensearch_client("fake-dev.us-east-1.es.amazonaws.com") | ||
assert ( | ||
str(result) == "<OpenSearch([{'host': 'fake-dev.us-east-1.es.amazonaws.com', " | ||
"'port': '443'}])>" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import os | ||
|
||
import boto3 | ||
from opensearchpy import AWSV4SignerAuth, OpenSearch, RequestsHttpConnection | ||
|
||
|
||
def configure_opensearch_client(url: str) -> OpenSearch: | ||
if url == "localhost": | ||
return OpenSearch( | ||
hosts=[{"host": url, "port": "9200"}], | ||
http_auth=("admin", "admin"), | ||
connection_class=RequestsHttpConnection, | ||
) | ||
|
||
credentials = boto3.Session().get_credentials() | ||
region = os.getenv("AWS_REGION", "us-east-1") | ||
auth = AWSV4SignerAuth(credentials, region) | ||
return OpenSearch( | ||
hosts=[{"host": url, "port": "443"}], | ||
http_auth=auth, | ||
use_ssl=True, | ||
verify_certs=True, | ||
connection_class=RequestsHttpConnection, | ||
) |