#-----------------------------------------------------------------------------------------------------------------------
#   Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
#  with the License. A copy of the License is located at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
#  OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
#  and limitations under the License.
#-----------------------------------------------------------------------------------------------------------------------
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: CDF (AWS Connected Device Framework)

Parameters:
  Environment:
    Description:  Name of environment.  Used to name the created resources.
    Type: String
    MinLength: 1
  ArtifactsBucket:
    Description:  Name of environment.  Used to name the created resources.
    Type: String
  ArtifactsConfigPrefix:
    Description:  Key prefix within bucket where config artifacts are uploaded to.
    Type: String
    Default: 'config/'

  TemplateSnippetS3UriBase:
    Description: |
      S3 uri of directory where template snippets are stored for the account.
    Type: String
    MinLength: 1


  AuthType:
    Description: Authorization type to apply to the API gateway endpoints
    Type: String
    Default: None
    AllowedValues:
      - None
      - Private
      - Cognito
      - LambdaRequest
      - LambdaToken
      - ApiKey
      - IAM
    MinLength: 1
  ApiGatewayDefinitionTemplate:
    Description: |
      Name of the API Gateway Cloudformation definition along with the authorization method to use.  Use one of the provided
      templates to implement no auth, private, api key, lambda request, lamdba token, or Cognito auth, or modify one to meet your own
      authentization requirements.  The template must exist within the provided TemplateSnippetS3UriBase location.
    Default: 'cfn-apiGateway-noAuth.yaml'
    Type: String
    MinLength: 1
  CognitoUserPoolArn:
    Description: Cognito user pool arn.  Only required if AuthType is set to 'Cognito'.
    Type: String
    Default: 'N/A'
  AuthorizerFunctionArn:
    Description: Lambda authorizer function arn.  Only required if AuthType is set to 'LambdaRequest' or 'LambdaToken'.
    Type: String
    Default: 'N/A'
  PublicSubNetIds:
    Description: Comma delimited list of private subnetIds to deploy the Bastion into
    Type: String

  VpcId:
    Description: ID of VPC to deploy the Asset Library into
    Type: String
  CDFSecurityGroupId:
    Description: ID of an existing CDF security group to deploy the Asset Library into (required if running full mode, or private API)
    Type: String
  PrivateSubNetIds:
    Description: Comma delimited list of private subnetIds to deploy the Asset Library into
    Type: CommaDelimitedList
  
  PrivateApiGatewayVPCEndpoint:
    Description: VPC endpoint required for private api gateway support
    Type: String

  IncludeSimulationLauncher:
    Description: If true, then Simulation Launcher will be deployed.
    Type: String
    Default: 'true'
    AllowedValues:
      - 'true'
      - 'false'
    MinLength: 1
  
  IncludeSimulationManager:
    Description: If true, then Simulation Manager will be deployed.
    Type: String
    Default: 'true'
    AllowedValues:
      - 'true'
      - 'false'
    MinLength: 1

  CustomResourceLambdaArn:
    Description: Custom resource lambda arn
    Type: String
    MinLength: 1

  AssetLibraryFunctionName:
    Description: Asset library function name 
    Type: String
    MinLength: 1

  KmsKeyId:
    Description: The KMS key ID used to encrypt SSM parameters. If not provided, one will be created.
    Type: String

  JMeterRepoName:
    Description: JMeter repository URI for the simulation launcher docker image.
    Type: String

  EnableApiGatewayAccessLogs:
    Description: Enales API gateway Access Logging, defaults to false if not specified.
    Type: String
    Default: false
    AllowedValues:
      - true
      - false
    MinLength: 1
  
  SimulationManagerAppConfigOverride:
    Description: Simulation Manager Application Configuration AssetLibrary Application 
    Type: String
    Default: '{}'
  SimulationLauncherAppConfigOverride:
    Description: Simulation Launcher Application Configuration AssetLibrary Application 
    Type: String
    Default: '{}'

Conditions:
  DeploySimulationLauncher: !Equals [ !Ref IncludeSimulationLauncher, 'true' ]
  DeploySimulationManager: !Equals [ !Ref IncludeSimulationManager, 'true' ]
  UsingVPCForAuth: !Equals [ !Ref AuthType, 'Private' ]

Resources:
  
  SimulationLauncher:
    Type: AWS::CloudFormation::Stack
    Condition: DeploySimulationLauncher
    Properties:
      TemplateURL: ../packages/services/simulation-launcher/infrastructure/cfn-simulation-launcher.yaml
      Parameters:
        ApplicationConfigurationOverride: !Ref SimulationLauncherAppConfigOverride 
        Environment: !Ref Environment
        JmeterRepoName: !Ref JMeterRepoName
        VpcId: !Ref VpcId
        PublicSubNetIds: !Ref PublicSubNetIds
        BucketName: !Ref ArtifactsBucket
        CustomResourceLambdaArn: !Ref CustomResourceLambdaArn
        KmsKeyId: !Ref KmsKeyId

  SimulationManager:
    Type: AWS::CloudFormation::Stack
    Condition: DeploySimulationManager
    Properties:
      TemplateURL: ../packages/services/simulation-manager/infrastructure/cfn-simulation-manager.yml
      Parameters:
        ApplicationConfigurationOverride: !Ref SimulationManagerAppConfigOverride
        Environment: !Ref Environment
        TemplateSnippetS3UriBase: !Ref TemplateSnippetS3UriBase
        AuthType: !Ref AuthType
        ApiGatewayDefinitionTemplate: !Ref ApiGatewayDefinitionTemplate
        VpcId: !If [ UsingVPCForAuth, !Ref VpcId, 'N/A' ]
        CDFSecurityGroupId: !If [ UsingVPCForAuth, !Ref CDFSecurityGroupId, 'N/A' ]
        PrivateSubNetIds: !If [ UsingVPCForAuth, !Ref PrivateSubNetIds, 'N/A' ]
        PrivateApiGatewayVPCEndpoint: !If [ UsingVPCForAuth, !Ref PrivateApiGatewayVPCEndpoint, 'N/A' ]
        CognitoUserPoolArn: !Ref CognitoUserPoolArn
        AuthorizerFunctionArn: !Ref AuthorizerFunctionArn
        CustomResourceLambdaArn: !Ref CustomResourceLambdaArn
        BucketName: !Ref ArtifactsBucket
        SimulationLauncherSnsTopic: !GetAtt SimulationLauncher.Outputs.SnsTopic
        AssetLibraryFunctionName: !Ref AssetLibraryFunctionName
        KmsKeyId: !Ref KmsKeyId
        EnableApiGatewayAccessLogs: !Ref EnableApiGatewayAccessLogs

Outputs:
  SimulationManagerRestApiFunctionName:
    Condition: DeploySimulationManager
    Description: SimulationManager REST API lambda function name
    Value: !GetAtt  SimulationManager.Outputs.RestApiFunctionName
  SimulationManagerApiGatewayUrl:
    Condition: DeploySimulationManager
    Description: SimulationManager REST API URL
    Value: !GetAtt SimulationManager.Outputs.ApiGatewayUrl
  SimulationManagerApiGatewayHost:
    Condition: DeploySimulationManager
    Description: SimulationManager REST API host
    Value: !GetAtt SimulationManager.Outputs.ApiGatewayHost
