Skip to content

Commit

Permalink
feat: Updated tenant-config API (#52)
Browse files Browse the repository at this point in the history
* Added the ability to pull tenant config based on query parameters in addition to headers/path

* Fix control logic

* Uses `not` vs `is None` to catch falsy empty strings

* Really fixing logic.
  • Loading branch information
tobuck-aws authored Jun 20, 2024
1 parent 72ea312 commit 14aad83
Showing 1 changed file with 64 additions and 20 deletions.
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

0 comments on commit 14aad83

Please sign in to comment.