generated from nationalarchives/da-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.
Test underlying search logic and refactor
- Loading branch information
1 parent
3196280
commit c62dd78
Showing
9 changed files
with
402 additions
and
107 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from typing import Tuple | ||
|
||
import boto3 | ||
from opensearchpy import AWSV4SignerAuth, OpenSearch | ||
|
||
from app.main.aws.parameter import ( | ||
get_aws_environment_prefix, | ||
get_parameter_store_key_value, | ||
) | ||
|
||
|
||
def get_open_search_index_from_aws_params() -> str: | ||
return get_parameter_store_key_value( | ||
get_aws_environment_prefix() + "AWS_OPEN_SEARCH_INDEX" | ||
) | ||
|
||
|
||
def generate_open_search_client_from_aws_params() -> OpenSearch: | ||
host = get_parameter_store_key_value( | ||
get_aws_environment_prefix() + "AWS_OPEN_SEARCH_HOST" | ||
) | ||
http_auth = _get_open_search_http_auth() | ||
|
||
open_search_client = OpenSearch( | ||
hosts=[{"host": host, "port": 443}], | ||
http_auth=http_auth, | ||
use_ssl=True, | ||
verify_certs=True, | ||
http_compress=True, | ||
ssl_assert_hostname=False, | ||
ssl_show_warn=True, | ||
) | ||
return open_search_client | ||
|
||
|
||
def _get_open_search_http_auth( | ||
auth_method: str = "username_password", | ||
) -> Tuple[str, str] | AWSV4SignerAuth: | ||
if auth_method == "username_password": | ||
return _get_open_search_username_password_auth() | ||
return _get_open_search_iam_auth() | ||
|
||
|
||
def _get_open_search_username_password_auth() -> Tuple[str, str]: | ||
username = get_parameter_store_key_value( | ||
get_aws_environment_prefix() + "AWS_OPEN_SEARCH_USERNAME" | ||
) | ||
password = get_parameter_store_key_value( | ||
get_aws_environment_prefix() + "AWS_OPEN_SEARCH_PASSWORD" | ||
) | ||
return (username, password) | ||
|
||
|
||
def _get_open_search_iam_auth() -> AWSV4SignerAuth: | ||
credentials = boto3.Session().get_credentials() | ||
aws_region = get_parameter_store_key_value( | ||
get_aws_environment_prefix() + "AWS_REGION" | ||
) | ||
service = "es" | ||
aws_auth = AWSV4SignerAuth(credentials, aws_region, service) | ||
return aws_auth |
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import logging | ||
from typing import Any | ||
|
||
from opensearchpy import ImproperlyConfigured | ||
from app.main.aws.open_search import ( | ||
generate_open_search_client_from_aws_params, | ||
get_open_search_index_from_aws_params, | ||
) | ||
|
||
|
||
def generate_open_search_client_and_make_poc_search(query: str) -> Any: | ||
fields = [ | ||
"legal_status", | ||
"description", | ||
"closure_type", | ||
"Internal-Sender_Identifier", | ||
"id", | ||
"Contact_Email", | ||
"Source_Organization", | ||
"Consignment_Series.keyword", | ||
"Consignment_Series", | ||
"Contact_Name", | ||
] | ||
open_search_client = generate_open_search_client_from_aws_params() | ||
|
||
try: | ||
open_search_client.ping() | ||
except ImproperlyConfigured as e: | ||
logging.error("OpenSearch client improperly configured: " + str(e)) | ||
raise e | ||
|
||
logging.info("OpenSearch client has been connected successfully") | ||
|
||
open_search_index = get_open_search_index_from_aws_params() | ||
open_search_query = { | ||
"query": { | ||
"multi_match": { | ||
"query": query, | ||
"fields": fields, | ||
"fuzziness": "AUTO", | ||
"type": "best_fields", | ||
} | ||
} | ||
} | ||
search_results = open_search_client.search( | ||
body=open_search_query, index=open_search_index | ||
) | ||
return search_results |
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,71 @@ | ||
from unittest.mock import patch | ||
from moto import mock_ssm | ||
import boto3 | ||
|
||
from app.main.aws.open_search import ( | ||
get_open_search_index_from_aws_params, | ||
generate_open_search_client_from_aws_params, | ||
) | ||
|
||
|
||
@mock_ssm | ||
def test_get_open_search_index_from_aws_params(): | ||
ssm_client = boto3.client("ssm", region_name="eu-west-2") | ||
ssm_client.put_parameter( | ||
Name="ENVIRONMENT_NAME", | ||
Value="test_env", | ||
Type="String", | ||
Overwrite=True, | ||
) | ||
ssm_client.put_parameter( | ||
Name="/test_env/AWS_OPEN_SEARCH_INDEX", | ||
Value="test_index", | ||
Type="String", | ||
Overwrite=True, | ||
) | ||
|
||
assert get_open_search_index_from_aws_params() == "test_index" | ||
|
||
|
||
@mock_ssm | ||
@patch("app.main.aws.open_search.OpenSearch") | ||
def test_generate_open_search_client_from_aws_params(mock_open_search): | ||
ssm_client = boto3.client("ssm", region_name="eu-west-2") | ||
ssm_client.put_parameter( | ||
Name="ENVIRONMENT_NAME", | ||
Value="test_env", | ||
Type="String", | ||
Overwrite=True, | ||
) | ||
ssm_client.put_parameter( | ||
Name="/test_env/AWS_OPEN_SEARCH_HOST", | ||
Value="mock_opensearch_host", | ||
Type="String", | ||
Overwrite=True, | ||
) | ||
ssm_client.put_parameter( | ||
Name="/test_env/AWS_OPEN_SEARCH_USERNAME", | ||
Value="mock_username", | ||
Type="String", | ||
Overwrite=True, | ||
) | ||
ssm_client.put_parameter( | ||
Name="/test_env/AWS_OPEN_SEARCH_PASSWORD", | ||
Value="mock_password", | ||
Type="String", | ||
Overwrite=True, | ||
) | ||
|
||
assert ( | ||
generate_open_search_client_from_aws_params() == mock_open_search.return_value | ||
) | ||
|
||
mock_open_search.assert_called_once_with( | ||
hosts=[{"host": "mock_opensearch_host", "port": 443}], | ||
http_auth=("mock_username", "mock_password"), | ||
use_ssl=True, | ||
verify_certs=True, | ||
http_compress=True, | ||
ssl_assert_hostname=False, | ||
ssl_show_warn=True, | ||
) |
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
Oops, something went wrong.