Skip to content

Commit

Permalink
enable external list support
Browse files Browse the repository at this point in the history
  • Loading branch information
devksingh4 committed Apr 11, 2024
1 parent 8f13150 commit ce1178c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
16 changes: 14 additions & 2 deletions cloudformation/lambda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,19 @@ Conditions:
IsProd: !Equals [!Ref Env, 'prod']

Resources:
# API Lambda

MembershipApiExternalQueryListsTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: "Retain"
Properties:
BillingMode: 'PAY_PER_REQUEST'
TableName: infra-membership-api-external-lists
DeletionProtectionEnabled: true
AttributeDefinitions:
- AttributeName: netid_list
AttributeType: S
KeySchema:
- AttributeName: netid_list
KeyType: HASH
MembershipApiCacheTable:
Type: AWS::DynamoDB::Table
Properties:
Expand Down Expand Up @@ -157,6 +168,7 @@ Resources:
Effect: Allow
Resource:
- !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${MembershipApiCacheTableName}
- !GetAtt MembershipApiExternalQueryListsTable.Arn
PolicyName: lambda-dn

MembershipApiLambdaLogGroup:
Expand Down
28 changes: 28 additions & 0 deletions code/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
dynamo = boto3.resource('dynamodb', region_name=os.environ.get("AWS_REGION", "us-east-1"))

TABLE_NAME='infra-membership-api-cache'
EXTERNAL_LIST_TABLE_NAME = 'infra-membership-api-external-lists'
AAD_SECRET_ID='infra-membership-api-aad-secret'
TOKEN_VALIDITY_SECONDS = 3590 # it's really 3600 but we save them slightly shorter

table = dynamo.Table(TABLE_NAME)
list_table = dynamo.Table(EXTERNAL_LIST_TABLE_NAME)

def healthzHandler(context, queryParams):
return {
Expand Down Expand Up @@ -49,6 +51,31 @@ def getPaidMembership(context, queryParams) -> dict:
"isPaidMember": paid
})
}
def getExternalMembership(context, queryParams) -> dict:
netid = queryParams['netId'].lower()
check_list = queryParams['list'].lower()
member = False
try:
list_table.get_item(
Key={
'key': f"{netid}_{check_list}"
}
)
member = True
except KeyError:
member = False
return {
'statusCode': 200,
'headers': {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": True
},
'body': json.dumps({
"netId": netid,
"list": check_list,
"isPaidMember": member
})
}
def getUI(context, queryParam) -> dict:
with open("index.html", "r") as file:
return {
Expand All @@ -63,6 +90,7 @@ def getUI(context, queryParam) -> dict:
"GET": {
"/api/v1/healthz": healthzHandler,
"/api/v1/checkMembership": getPaidMembership,
"/api/v1/checkExternalMembership": getExternalMembership,
"/": getUI,
}
}
Expand Down

0 comments on commit ce1178c

Please sign in to comment.