AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  ActivityHub API

  SAM template for ActivityHub RESTful API

Parameters:
  Stage:
    Type: String
    Default: staging
    Description: Parameter for getting the deployment stage

Mappings:
  EnvConfigs:
    staging:
      SecurityGroupIds:
        - sg-08bb42935021e024b
      SubnetIds:
        - subnet-098de5356152db3c2
        - subnet-0aea29f5add64487c
    prod:
      SecurityGroupIds:
        - sg-0118e0f1069a02381
      SubnetIds:
        - subnet-0539a75851b24df03
        - subnet-0258434fb4a0d03ef
        - subnet-007f996591222aec2
        - subnet-068050d69d264d42a

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 900
    VpcConfig:
      SecurityGroupIds: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - SecurityGroupIds
      SubnetIds: !FindInMap
        - EnvConfigs
        - Ref: Stage
        - SubnetIds

Resources:
  ActivityHubAPIFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: api/
      Handler: handler.lambda_handler
      Runtime: python3.9
      MemorySize: 1024
      Architectures:
        - x86_64
      Policies:
        - DynamoDBCrudPolicy:
            TableName: !Ref ActivityHubMessageTable
        - DynamoDBCrudPolicy:
            TableName: !Ref ActivityHubSourceTable
      Environment:
        Variables:
          ACTIVITYHUB_MESSAGE_TABLE_NAME: !Ref ActivityHubMessageTable
          ACTIVITYHUB_SOURCE_TABLE_NAME: !Ref ActivityHubSourceTable
      Events:
        WriteAnActivity:
          Type: HttpApi
          Properties:
            Path: /activity
            Method: POST
        GetAnActivity:
          Type: HttpApi
          Properties:
            Path: /activity/{id}
            Method: GET
        GetActivityListByContext:
          Type: HttpApi
          Properties:
            Path: /activities/context/{id}
            Method: GET
        QueryActivityListSince:
          Type: HttpApi
          Properties:
            Path: /activities/{target}/since/{from_ts}
            Method: GET
        QueryActivityListTimeRange:
          Type: HttpApi
          Properties:
            Path: /activities/{target}/from/{from_ts}/to/{to_ts}
            Method: GET
        GetContextListByTimeRagne:
          Type: HttpApi
          Properties:
            Path: /context/{target}/from/{from_ts}/to/{to_ts}
            Method: GET
        SearchActivityInTimeRange:
          Type: HttpApi
          Properties:
            Path: /search/{target}/from/{from_ts}/to/{to_ts}
            Method: GET

  ActivityHubSourceManagerFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: srcmngr/
      Handler: handler.lambda_handler
      Runtime: python3.9
      Architectures:
        - x86_64
      Policies:
        - DynamoDBCrudPolicy:
            TableName: !Ref ActivityHubMessageTable
        - DynamoDBCrudPolicy:
            TableName: !Ref ActivityHubSourceTable
      Environment:
        Variables:
          ACTIVITYHUB_MESSAGE_TABLE_NAME: !Ref ActivityHubMessageTable
          ACTIVITYHUB_SOURCE_TABLE_NAME: !Ref ActivityHubSourceTable

  ActivityHubManualManagerFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: api/
      Handler: manager.lambda_handler
      Runtime: python3.9
      Architectures:
        - x86_64
      Policies:
        - DynamoDBCrudPolicy:
            TableName: !Ref ActivityHubMessageTable
        - DynamoDBCrudPolicy:
            TableName: !Ref ActivityHubSourceTable
      Environment:
        Variables:
          ACTIVITYHUB_MESSAGE_TABLE_NAME: !Ref ActivityHubMessageTable
          ACTIVITYHUB_SOURCE_TABLE_NAME: !Ref ActivityHubSourceTable

  ActivityHubMessageTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: activity-hub-messages
      AttributeDefinitions:
        - AttributeName: activity_id
          AttributeType: S
        - AttributeName: calibration_time
          AttributeType: N
        - AttributeName: target_id
          AttributeType: S
        - AttributeName: context_id
          AttributeType: S
      KeySchema:
        - AttributeName: target_id
          KeyType: HASH
        - AttributeName: activity_id
          KeyType: RANGE
      GlobalSecondaryIndexes:
        - IndexName: target_id-calibration_time-index
          KeySchema:
            - AttributeName: target_id
              KeyType: HASH
            - AttributeName: calibration_time
              KeyType: RANGE
          Projection:
            ProjectionType: ALL
        - IndexName: context_id-calibration_time-index
          KeySchema: 
            - AttributeName: context_id
              KeyType: HASH
            - AttributeName: calibration_time
              KeyType: RANGE
          Projection:
            ProjectionType: ALL
        - IndexName: activity_id-index
          KeySchema:
            - AttributeName: activity_id
              KeyType: HASH
          Projection:
            ProjectionType: ALL
      BillingMode: PAY_PER_REQUEST
      ProvisionedThroughput:
        ReadCapacityUnits: 0
        WriteCapacityUnits: 0

  ActivityHubSourceTable:
    Type: AWS::Serverless::SimpleTable
    Properties:
      PrimaryKey:
        Name: name
        Type: String
      TableName: activity-hub-sources

Outputs:
  ApiEndpoint:
    Description: "The invoke URL for our HTTP API"
    Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com/activity"
  Function:
    Description: "ActivityHub API handler function ARN"
    Value: !GetAtt ActivityHubAPIFunction.Arn
