Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Updated tenant-config API #52

Merged
merged 5 commits into from
Jun 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 64 additions & 20 deletions resources/functions/tenant-config/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
tenant_details_table_handler = dynamodb.Table(tenant_details_table)


def _get_tenant_config(name):
def _get_tenant_config_by_name(name):
response = tenant_details_table_handler.query(
IndexName=tenant_config_index_name,
KeyConditionExpression=Key(tenant_name_column).eq(name),
Expand All @@ -42,9 +42,23 @@ def _get_tenant_config(name):
return tenant_config.get(tenant_config_column, None)


def _get_tenant_config_for_tenant(name):
def _get_tenant_config_by_id(id):
logger.info(f"id: {id}")
response = tenant_details_table_handler.get_item(
Key={
'tenantId': id
},
)
if "Item" not in response or len(response["Item"]) < 1:
return None

tenant_config = response["Item"]
return tenant_config.get(tenant_config_column, None)


def _get_tenant_config_for_tenant_name(name):
try:
tenant_config = _get_tenant_config(name)
tenant_config = _get_tenant_config_by_name(name)
logger.info(f"tenant_config: {tenant_config}")
if tenant_config is None:
logger.error(f"No tenant details found for {name}")
Expand All @@ -58,6 +72,22 @@ def _get_tenant_config_for_tenant(name):
raise InternalServerError("Unknown error during processing!")


def _get_tenant_config_for_tenant_id(id):
try:
tenant_config = _get_tenant_config_by_id(id)
logger.info(f"tenant_config: {tenant_config}")
if tenant_config is None:
logger.error(f"No tenant details found for {id}")
raise NotFoundError(f"No tenant details found for {id}")
logger.info(
f"Tenant config found for {id} - {tenant_config}")

return tenant_config, HTTPStatus.OK.value
except botocore.exceptions.ClientError as error:
logger.error(error)
raise InternalServerError("Unknown error during processing!")


@app.get("/tenant-config/<tenant_name>")
@tracer.capture_method
def get_tenant_config_via_req_param(tenant_name):
Expand All @@ -66,27 +96,41 @@ def get_tenant_config_via_req_param(tenant_name):
logger.error(f"Tenant name not found in path!")
raise BadRequestError(f"Tenant name not found in path!")

return _get_tenant_config_for_tenant(tenant_name)
return _get_tenant_config_for_tenant_name(tenant_name)


@app.get("/tenant-config")
@tracer.capture_method
def get_tenant_config_via_header():
origin_header = app.current_event.get_header_value(name="Origin")
logger.info(f"origin_header: {origin_header}")
if origin_header is None:
logger.error(f"Origin header missing!")
raise BadRequestError(f"Origin header missing!")

hostname = origin_header.split("://")[1]
logger.info(f"hostname: {hostname}")
tenant_name = hostname.split(".")[0]
logger.info(f"tenant_name: {tenant_name}")
if tenant_name is None:
logger.error(f"Unable to parse tenant name!")
raise BadRequestError(f"Unable to parse tenant name!")

return _get_tenant_config_for_tenant(tenant_name)
def get_tenant_config_via_param_or_header():
tenant_id: str = app.current_event.get_query_string_value(
name="tenantId", default_value="")
tenant_name: str = ''
logger.info(f"tenant_id: {tenant_id}")
if not tenant_id:
logger.info(
f"No tenantId query parameter found. Looking for tenantName.")
else:
return _get_tenant_config_for_tenant_id(tenant_id)

tenant_name = app.current_event.get_query_string_value(
name="tenantName", default_value="")
if not tenant_name:
logger.info(f"No tenantName query parameter found. Looking at headers.")
origin_header = app.current_event.get_header_value(name="Origin")
logger.info(f"origin_header: {origin_header}")
if not origin_header:
logger.error(f"Origin header missing!")
raise BadRequestError(f"Origin header missing!")

hostname = origin_header.split("://")[1]
logger.info(f"hostname: {hostname}")
tenant_name = hostname.split(".")[0]
logger.info(f"tenant_name: {tenant_name}")
if not tenant_name:
logger.error(f"Unable to parse tenant name!")
raise BadRequestError(f"Unable to parse tenant name!")

return _get_tenant_config_for_tenant_name(tenant_name)


@logger.inject_lambda_context(
Expand Down
Loading