/** ************************************************************************************************
*   Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.                             *
*   SPDX-License-Identifier: Apache-2.0                                                            *
 ************************************************************************************************ */
const util = require('../../util');

module.exports = {
    S3ClearCodeVersion: {
        Type: 'Custom::S3Version',
        Properties: {
            ServiceToken: { 'Fn::GetAtt': ['CFNLambda', 'Arn'] },
            Bucket: { Ref: 'BootstrapBucket' },
            Key: { 'Fn::Sub': '${BootstrapPrefix}/lambda/s3-clean.zip' },
            BuildDate: (new Date()).toISOString(),
        },
    },
    S3CleanLambdaLogGroup: {
        Type: 'AWS::Logs::LogGroup',
        Properties: {
            LogGroupName: {
                'Fn::Join': [
                    '-',
                    [
                        { 'Fn::Sub': '/aws/lambda/${AWS::StackName}-S3CleanLambda' },
                        { 'Fn::Select': ['2', { 'Fn::Split': ['/', { Ref: 'AWS::StackId' }] }] },
                    ],
                ],
            },
            RetentionInDays: {
                'Fn::If': [
                    'LogRetentionPeriodIsNotZero',
                    { Ref: 'LogRetentionPeriod' },
                    { Ref: 'AWS::NoValue' },
                ],
            },
        },
        Metadata: {
            guard: util.cfnGuard('CLOUDWATCH_LOG_GROUP_ENCRYPTED', 'CW_LOGGROUP_RETENTION_PERIOD_CHECK'),
        },
    },
    S3Clean: {
        Type: 'AWS::Lambda::Function',
        Metadata: { guard: util.cfnGuard('LAMBDA_CONCURRENCY_CHECK', 'LAMBDA_INSIDE_VPC') },
        Properties: {
            Code: {
                S3Bucket: { Ref: 'BootstrapBucket' },
                S3Key: { 'Fn::Sub': '${BootstrapPrefix}/lambda/s3-clean.zip' },
            },
            Environment: {
                Variables: {
                    ...util.getCommonEnvironmentVariables(),
                },
            },
            Description: 'This function clears all S3 objects from the bucket of a given S3-based resource',
            Handler: 'lambda_function.handler',
            LoggingConfig: {
                LogGroup: { Ref: 'S3CleanLambdaLogGroup' },
            },
            Role: {
                'Fn::GetAtt': ['CFNLambdaRole', 'Arn'],
            },
            Runtime: process.env.npm_package_config_pythonRuntime,
            VpcConfig: {
                'Fn::If': [
                    'VPCEnabled',
                    {
                        SubnetIds: { Ref: 'VPCSubnetIdList' },
                        SecurityGroupIds: { Ref: 'VPCSecurityGroupIdList' },
                    },
                    { Ref: 'AWS::NoValue' },
                ],
            },
            TracingConfig: {
                'Fn::If': [
                    'XRAYEnabled',
                    { Mode: 'Active' },
                    { Ref: 'AWS::NoValue' },
                ],
            },
            Tags: [{
                Key: 'Type',
                Value: 'S3 Clean',
            }],
            Timeout: 300,
        },
    },
};
