---
# Copyright 2018 widdix GmbH
#
# 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'
Description: 'cfn-modules: RDS PostgreSQL'
# cfn-modules:implements(ExposeName, ExposeDnsName)
Parameters:
  VpcModule:
    Description: 'Stack name of vpc module.'
    Type: String
  ClientSgModule:
    Description: 'Stack name of client-sg module.'
    Type: String
  AlertingModule:
    Description: 'Optional but recommended stack name of alerting module.'
    Type: String
    Default: ''
  HostedZoneModule:
    Description: 'Optional but recommended stack name of module implementing HostedZone.'
    Type: String
    Default: ''
  BastionModule:
    Description: 'Optional but recommended stack name of module implementing Bastion.'
    Type: String
    Default: ''
  KmsKeyModule:
    Description: 'Optional but recommended stack name of kms-key module (ignored when DBSnapshotIdentifier is set, value used from snapshot).'
    Type: String
    Default: ''
  SecretModule:
    Description: 'Optional but recommended stack name of secret module.'
    Type: String
    Default: ''
  DBSnapshotIdentifier:
    Description: 'Optional name or Amazon Resource Name (ARN) of the DB snapshot from which you want to restore (leave blank to create an empty database).'
    Type: String
    Default: ''
  DBAllocatedStorage:
    Description: 'The allocated storage size, specified in GB (ignored when DBSnapshotIdentifier is set, value used from snapshot).'
    Type: Number
    Default: 5
    MinValue: 5
    MaxValue: 16384
  DBInstanceClass:
    Description: 'The instance type of the database.'
    Type: String
    Default: 'db.t4g.micro'
  DBName:
    Description: 'Name of the database (ignored when DBSnapshotIdentifier is set, value used from snapshot).'
    Type: String
    Default: ''
  DBBackupRetentionPeriod:
    Description: 'The number of days to keep snapshots of the database.'
    Type: Number
    Default: 35
    MinValue: 0
    MaxValue: 35
  DBMasterUsername:
    Description: 'The master user name for the DB instance (ignored when DBSnapshotIdentifier is set, value used from snapshot).'
    Type: String
    Default: master
  DBMasterUserPassword:
    Description: 'The master password for the DB instance (ignored when DBSnapshotIdentifier is set, value used from snapshot; also ignored if SecretModule is set).'
    Type: String
    NoEcho: true
    Default: ''
  DBMultiAZ:
    Description: 'Specifies if the database instance is deployed to multiple Availability Zones for HA.'
    Type: String
    Default: true
    AllowedValues: [true, false]
  SubDomainNameWithDot:
    Description: 'Name that is used to create the DNS entry with trailing dot, e.g. §{SubDomainNameWithDot}§{HostedZoneName}. Leave blank for naked (or apex and bare) domain. Requires HostedZoneModule parameter!'
    Type: String
    Default: 'postgres.'
  EngineVersion:
    Description: 'PostgreSQL version (e.g., 14.12).' # aws rds describe-db-engine-versions --engine postgres --query "DBEngineVersions[].EngineVersion"
    Type: String
  EnableIAMDatabaseAuthentication:
    Description: 'Enable mapping of AWS Identity and Access Management (IAM) accounts to database accounts (https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/UsingWithRDS.IAMDBAuth.html).'
    Type: String
    AllowedValues: ['true', 'false']
    Default: 'false'
Resources:
  Instance:
    Type: 'AWS::CloudFormation::Stack'
    Properties:
      Parameters:
        VpcModule: !Ref VpcModule
        ClientSgModule: !Ref ClientSgModule
        AlertingModule: !Ref AlertingModule
        HostedZoneModule: !Ref HostedZoneModule
        BastionModule: !Ref BastionModule
        KmsKeyModule: !Ref KmsKeyModule
        SecretModule: !Ref SecretModule
        Engine: postgres
        EngineVersion: !Ref EngineVersion
        EnginePort: '5432'
        DBSnapshotIdentifier: !Ref DBSnapshotIdentifier
        DBAllocatedStorage: !Ref DBAllocatedStorage
        DBInstanceClass: !Ref DBInstanceClass
        DBName: !Ref DBName
        DBBackupRetentionPeriod: !Ref DBBackupRetentionPeriod
        DBMasterUsername: !Ref DBMasterUsername
        DBMasterUserPassword: !Ref DBMasterUserPassword
        DBMultiAZ: !Ref DBMultiAZ
        SubDomainNameWithDot: !Ref SubDomainNameWithDot
        EnableIAMDatabaseAuthentication: !Ref EnableIAMDatabaseAuthentication
      TemplateURL: './node_modules/@cfn-modules/rds-instance/module.yml'
Outputs:
  ModuleId:
    Value: 'rds-postgres'
  ModuleVersion:
    Value: '2.4.6'
  StackName:
    Value: !Ref 'AWS::StackName'
  Name:
    Description: 'The name of the database instance.'
    Value: !GetAtt 'Instance.Outputs.Name'
    Export:
      Name: !Sub '${AWS::StackName}-Name'
  DnsName:
    Description: 'The connection endpoint for the database.'
    Value: !GetAtt 'Instance.Outputs.DnsName'
    Export:
      Name: !Sub '${AWS::StackName}-DnsName'
