Skip to content

Commit

Permalink
Create index atbd if not exists on opensearch client setup
Browse files Browse the repository at this point in the history
  • Loading branch information
thenav56 committed Apr 18, 2023
1 parent bbca71b commit ec3663b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
24 changes: 1 addition & 23 deletions app/api/v2/opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from app.config import OPENSEARCH_PORT, OPENSEARCH_URL
from app.logs import logger
from app.schemas.users import User
from app.search.opensearch import aws_auth
from app.users.auth import get_user

from fastapi import APIRouter, Depends
Expand All @@ -18,29 +19,6 @@
router = APIRouter()


def aws_auth():
"""Outputs an Opensearch service client. Low level client authorizes against the boto3 session and associated AWS credentials"""
logger.info("Getting AWS Auth Credentials")
credentials = boto3.Session(region_name=REGION).get_credentials()
service = "es"
awsauth = AWS4Auth(
credentials.access_key,
credentials.secret_key,
REGION,
service,
session_token=credentials.token,
)
opensearch_client = OpenSearch(
hosts=[{"host": OPENSEARCH_URL, "port": OPENSEARCH_PORT}],
http_auth=awsauth,
use_ssl=False,
verify_certs=False,
connection_class=RequestsHttpConnection,
)

return opensearch_client


@router.post("/search")
def search_opensearch(query: dict, user: User = Depends(get_user)):
"""
Expand Down
12 changes: 12 additions & 0 deletions app/search/opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from app.db.models import Atbds, AtbdVersions
from app.logs import logger
from app.schemas.opensearch import OpensearchAtbd
from app.utils import run_once

from fastapi import HTTPException

Expand All @@ -20,6 +21,16 @@
REGION = os.getenv("AWS_REGION", "us-west-2")


@run_once
def create_search_indices(opensearch_client):
"""
Create atbd index if it doesn't exists
"""
if not opensearch_client.indices.exists("atbd"):
logger.info("Creating index: atbd")
opensearch_client.indices.create("atbd")


def aws_auth():
"""
Outputs an Opensearch service client. Low level client authorizes against the boto3 session and associated AWS credentials
Expand All @@ -43,6 +54,7 @@ def aws_auth():
connection_class=RequestsHttpConnection,
)
logger.info("AWS Auth: %s", awsauth)
create_search_indices(opensearch_client)
return opensearch_client


Expand Down
20 changes: 20 additions & 0 deletions app/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Utility functions"""

from functools import wraps

from app import config


Expand All @@ -9,3 +11,21 @@ def get_task_queue():
return config.sqs.get_queue_by_name(QueueName=config.TASK_QUEUE_NAME)
else:
return config.sqs.Queue(config.TASK_QUEUE_URL)


def run_once(f):
"""
# From https://stackoverflow.com/a/4104188/3436502
Runs a function (successfully) only once.
The running can be reset by setting the `has_run` attribute to False
"""

@wraps(f)
def wrapper(*args, **kwargs):
if not getattr(wrapper, "has_run", False):
result = f(*args, **kwargs)
setattr(wrapper, "has_run", True)
return result

setattr(wrapper, "has_run", False)
return wrapper

0 comments on commit ec3663b

Please sign in to comment.