# 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.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 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: CLoudformation custom resource to deploy Lex

Resources:  
  LexBotCustomResource:
    Type: 'AWS::Serverless::Function'
    Properties:
      FunctionName: LexBotCustomResource
      Handler: index.handler
      Runtime: nodejs12.x
      CodeUri: ./custom-resources/lex-bot
      Description: 'custom cloudformation resource used to deploy Lexbots'
      MemorySize: 256
      Timeout: 300
      Policies:
        - Statement: 
          - Sid: Lex
            Effect: "Allow"
            Action: 
            - lex:DeleteIntent
            - lex:CreateIntent
            - lex:PutIntent
            - lex:DeleteBot
            - lex:GetIntent
            - lex:PutBot
            Resource: "*"
  LexIntentCustomResource:
    Type: 'AWS::Serverless::Function'
    Properties:
      FunctionName: LexIntentCustomResource
      Handler: index.handler
      Runtime: nodejs12.x
      CodeUri: ./custom-resources/lex-intent

      Description: 'custom cloudformation resource used to deploy Lexbots'
      MemorySize: 256
      Timeout: 300
      Policies:
        - Statement: 
          - Sid: Lex
            Effect: "Allow"
            Action: 
            - lex:DeleteIntent
            - lex:CreateIntent
            - lex:GetSlot
            - lex:PutIntent
            Resource: "*"
  LexSlotCustomResource:
    Type: 'AWS::Serverless::Function'
    Properties:
      FunctionName: LexSlotCustomResource
      Handler: index.handler
      Runtime: nodejs12.x
      CodeUri: ./custom-resources/lex-slot
      Description: 'custom cloudformation resource used to deploy Lexbots'
      MemorySize: 256
      Timeout: 300
      Policies:
        - Statement: 
          - Sid: Lex
            Effect: "Allow"
            Action: 
            - lex:DeleteSlotType
            - lex:CreateSlotType
            - lex:PutSlotType
            Resource: "*"

  LexAliasCustomResource:
    Type: 'AWS::Serverless::Function'
    Properties:
      FunctionName: LexAliasCustomResource
      Handler: index.handler
      Runtime: nodejs12.x
      CodeUri: ./custom-resources/lex-alias
      Description: 'custom cloudformation resource used to deploy Lex aliases'
      MemorySize: 256
      Timeout: 300
      Policies:
        - Statement: 
          - Sid: Lex
            Effect: "Allow"
            Action: 
            - lex:GetBotAliases
            - lex:GetBotAlias
            - lex:PutBotAlias
            - lex:DeleteBotAlias
            Resource: "*"
            

  LexConnectPermissionCustomResource:
    Type: 'AWS::Serverless::Function'
    Properties:
      FunctionName: LexConnectPermissionCustomResource
      Handler: index.handler
      Runtime: nodejs12.x
      CodeUri: ./custom-resources/create-connect-lex-permission
      Description: 'custom cloudformation resource used to associate a Lex with a Connect instance'
      MemorySize: 256
      Timeout: 300
      Policies:
        - Statement: 
          - Sid: Connect
            Effect: "Allow"
            Action: 
            - lex:GetBots
            - lex:GetBot
            - lex:CreateResourcePolicy
            - lex:DeleteResourcePolicy
            - lex:UpdateResourcePolicy
            - lex:DescribeBotAlias
            - lex:ListBotAliases
            - lex:ListBots
            - connect:AssociateBot
            - connect:DisassociateBot
            - connect:ListBots
            - connect:AssociateLexBot
            - connect:DisassociateLexBot
            - connect:ListLexBots
            Resource: 
            - !Sub "arn:aws:connect:*:${AWS::AccountId}:instance/*"
            - !Sub "arn:aws:lambda:*:${AWS::AccountId}:function:*"
            
Outputs:
  CFNIntentLex:
    Description: Custom resource to deploy intents
    Value:
      Fn::GetAtt:
      - LexIntentCustomResource
      - Arn
    Export:
      Name: LexIntentCustomResource
  CFNBotLex:
    Description: Custom resource to deploy Amazon Lex Bots
    Value:
      Fn::GetAtt:
      - LexBotCustomResource
      - Arn
    Export:
      Name: LexBotCustomResource
  CFNSlotLex:
    Description: Custom resource to deploy slots
    Value:
      Fn::GetAtt:
      - LexSlotCustomResource
      - Arn
    Export:
      Name: LexSlotCustomResource
  CFNBotLexAlias:
    Description: Custom resource to deploy Lex bot aliases
    Value:
      Fn::GetAtt:
      - LexAliasCustomResource
      - Arn
    Export:
      Name: LexBotAliasCustomResource
  CFNLexConnectPermission:
    Description: Custom resource to associate a lex bot to a connect instance
    Value:
      Fn::GetAtt:
      - LexConnectPermissionCustomResource
      - Arn
    Export:
      Name: LexConnectPermissionCustomResource