This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
forked from HHS/simpler-grants-gov
-
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.
Browse files
Browse the repository at this point in the history
## Summary Fixes #9 ### Time to review: __10 mins__ ## Changes proposed Setup a search index to run locally via Docker Updated makefile to automatically initialize the index + added a script to wait for the index to start up before proceeding. Setup a very basic client for connecting to the search index (will be expanded more in subsequent PRs) Basic test / test utils to verify it is working (also will be expanded) ## Context for reviewers This is the first step in getting the search index working locally. This actually gets it running, and the client works, we just aren't doing anything meaningful with it yet besides tests. ## Additional information This doesn't yet create an index that we can use, except in the test. However, if you want to test out a search index, you can go to http://localhost:5601/app/dev_tools#/console (after running `make init`) to run some queries against the (one node) cluster. https://opensearch.org/docs/latest/getting-started/communicate/#sending-requests-in-dev-tools provides some examples of how to create + use indexes that you can follow.
- Loading branch information
Showing
12 changed files
with
315 additions
and
10 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
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,31 @@ | ||
#!/bin/bash | ||
# wait-for-local-opensearch | ||
|
||
set -e | ||
|
||
# Color formatting | ||
RED='\033[0;31m' | ||
NO_COLOR='\033[0m' | ||
|
||
MAX_WAIT_TIME=30 # seconds | ||
WAIT_TIME=0 | ||
|
||
# Curl the healthcheck endpoint of the local opensearch | ||
# until it returns a success response | ||
until curl --output /dev/null --silent http://localhost:9200/_cluster/health; | ||
do | ||
echo "waiting on OpenSearch to initialize..." | ||
sleep 3 | ||
|
||
WAIT_TIME=$(($WAIT_TIME+3)) | ||
if [ $WAIT_TIME -gt $MAX_WAIT_TIME ] | ||
then | ||
echo -e "${RED}ERROR: OpenSearch appears to not be starting up, running \"docker logs opensearch-node\" to troubleshoot.${NO_COLOR}" | ||
docker logs opensearch-node | ||
exit 1 | ||
fi | ||
done | ||
|
||
echo "OpenSearch is ready after ~${WAIT_TIME} seconds" | ||
|
||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from src.adapters.search.opensearch_client import SearchClient, get_opensearch_client | ||
from src.adapters.search.opensearch_config import get_opensearch_config | ||
|
||
__all__ = ["SearchClient", "get_opensearch_client", "get_opensearch_config"] |
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,36 @@ | ||
from typing import Any | ||
|
||
import opensearchpy | ||
|
||
from src.adapters.search.opensearch_config import OpensearchConfig, get_opensearch_config | ||
|
||
# More configuration/setup coming in: | ||
# TODO - https://github.com/navapbc/simpler-grants-gov/issues/13 | ||
|
||
# Alias the OpenSearch client so that it doesn't need to be imported everywhere | ||
# and to make it clear it's a client | ||
SearchClient = opensearchpy.OpenSearch | ||
|
||
|
||
def get_opensearch_client( | ||
opensearch_config: OpensearchConfig | None = None, | ||
) -> SearchClient: | ||
if opensearch_config is None: | ||
opensearch_config = get_opensearch_config() | ||
|
||
# See: https://opensearch.org/docs/latest/clients/python-low-level/ for more details | ||
return opensearchpy.OpenSearch(**_get_connection_parameters(opensearch_config)) | ||
|
||
|
||
def _get_connection_parameters(opensearch_config: OpensearchConfig) -> dict[str, Any]: | ||
# TODO - we'll want to add the AWS connection params here when we set that up | ||
# See: https://opensearch.org/docs/latest/clients/python-low-level/#connecting-to-amazon-opensearch-serverless | ||
|
||
return dict( | ||
hosts=[{"host": opensearch_config.host, "port": opensearch_config.port}], | ||
http_compress=True, | ||
use_ssl=opensearch_config.use_ssl, | ||
verify_certs=opensearch_config.verify_certs, | ||
ssl_assert_hostname=False, | ||
ssl_show_warn=False, | ||
) |
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,33 @@ | ||
import logging | ||
|
||
from pydantic import Field | ||
from pydantic_settings import SettingsConfigDict | ||
|
||
from src.util.env_config import PydanticBaseEnvConfig | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class OpensearchConfig(PydanticBaseEnvConfig): | ||
model_config = SettingsConfigDict(env_prefix="OPENSEARCH_") | ||
|
||
host: str # OPENSEARCH_HOST | ||
port: int # OPENSEARCH_PORT | ||
use_ssl: bool = Field(default=True) # OPENSEARCH_USE_SSL | ||
verify_certs: bool = Field(default=True) # OPENSEARCH_VERIFY_CERTS | ||
|
||
|
||
def get_opensearch_config() -> OpensearchConfig: | ||
opensearch_config = OpensearchConfig() | ||
|
||
logger.info( | ||
"Constructed opensearch configuration", | ||
extra={ | ||
"host": opensearch_config.host, | ||
"port": opensearch_config.port, | ||
"use_ssl": opensearch_config.use_ssl, | ||
"verify_certs": opensearch_config.verify_certs, | ||
}, | ||
) | ||
|
||
return opensearch_config |
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
Empty file.
Oops, something went wrong.