Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Some rearranging of files
Browse files Browse the repository at this point in the history
  • Loading branch information
chouinar committed May 17, 2024
1 parent 62ba7f1 commit 1922340
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 40 deletions.
Empty file.
4 changes: 4 additions & 0 deletions api/src/adapters/search/__init__.py
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"]
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,35 @@

import opensearchpy

from src.adapters.opensearch.opensearch_config import OpensearchConfig, get_opensearch_config
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,
) -> opensearchpy.OpenSearch:
) -> SearchClient:

Check warning on line 17 in api/src/adapters/search/opensearch_client.py

View workflow job for this annotation

GitHub Actions / API Lint, Format & Tests

Variable "src.adapters.search.opensearch_client.SearchClient" is not valid as a type [valid-type]
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]:

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}],
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,
)
)
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import logging

from pydantic import Field
from pydantic_settings import SettingsConfigDict

from src.util.env_config import PydanticBaseEnvConfig
from pydantic_settings import SettingsConfigDict

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

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:
Expand All @@ -27,7 +26,7 @@ def get_opensearch_config() -> OpensearchConfig:
"host": opensearch_config.host,
"port": opensearch_config.port,
"use_ssl": opensearch_config.use_ssl,
"verify_certs": opensearch_config.verify_certs
"verify_certs": opensearch_config.verify_certs,
},
)

Expand Down
29 changes: 29 additions & 0 deletions api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import src.adapters.db as db
import src.app as app_entry
import tests.src.db.models.factories as factories
from src.adapters import search
from src.constants.schema import Schemas
from src.db import models
from src.db.models.lookup.sync_lookup_values import sync_lookup_values
Expand Down Expand Up @@ -143,6 +144,34 @@ def test_foreign_schema(db_schema_prefix):
return f"{db_schema_prefix}{Schemas.LEGACY}"


####################
# Opensearch Fixtures
####################


@pytest.fixture(scope="session")
def search_client() -> search.SearchClient:
return search.get_opensearch_client()


@pytest.fixture(scope="session")
def opportunity_index(search_client):
# TODO - will adjust this in the future to use utils we'll build
# for setting up / aliasing indexes. For now, keep it simple

# create a random index name just to make sure it won't ever conflict
# with an actual one, similar to how we create schemas for database tests
index_name = f"test_{uuid.uuid4().int}_opportunity"

search_client.indices.create(index_name, body={})

try:
yield index_name
finally:
# Try to clean up the index at the end
search_client.indices.delete(index_name)


####################
# Test App & Client
####################
Expand Down
31 changes: 3 additions & 28 deletions api/tests/src/adapters/opensearch/test_opensearch.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import uuid

import opensearchpy
import pytest

from src.adapters.opensearch.opensearch_client import get_opensearch_client

########################################
# This is a placeholder set of tests,
# we'll evolve / change the structure
Expand All @@ -16,28 +9,10 @@
########################################


@pytest.fixture(scope="session")
def search_client() -> opensearchpy.OpenSearch:
# TODO - move this to conftest
return get_opensearch_client()


@pytest.fixture(scope="session")
def opportunity_index(search_client):
# TODO - will adjust this in the future to use utils we'll build
# for setting up / aliasing indexes. For now, keep it simple

index_name = f"test_{uuid.uuid4().int}_opportunity"

search_client.indices.create(index_name, body={})

try:
yield index_name
finally:
search_client.indices.delete(index_name)


def test_index_is_running(search_client, opportunity_index):
# Very simple test, will rewrite / remove later once we have something
# more meaningful to test.

existing_indexes = search_client.cat.indices(format="json")

found_opportunity_index = False
Expand Down

0 comments on commit 1922340

Please sign in to comment.