{% set config = merged_configs.get('aws_redshift_cluster', {}) %}
{% set comments = compliance_comments.get('aws_redshift_cluster', []) %}
# ------------------------------------------------------------------------------
# Redshift Cluster
# ------------------------------------------------------------------------------
{% for comment in comments %}
# {{ comment }}
{% endfor %}

resource "aws_redshift_cluster" "this" {
  cluster_identifier = "{{ config.get('cluster_identifier', project_id ~ '-redshift') }}"
  database_name      = "{{ config.get('database_name', 'appdb') }}"
  master_username    = "{{ config.get('master_username', 'dbadmin') }}"
  master_password    = var.redshift_master_password

  node_type       = "{{ config.get('node_type', 'ra3.xlplus') }}"
  cluster_type    = "{{ config.get('cluster_type', 'multi-node') }}"
  number_of_nodes = {{ config.get('number_of_nodes', 2) }}

  # Encryption
  encrypted  = {{ config.get('encrypted', true) | lower }}
  kms_key_id = {{ config.get('kms_key_id', 'null') }}

  # Network
  publicly_accessible  = {{ config.get('publicly_accessible', false) | lower }}
  enhanced_vpc_routing = {{ config.get('enhanced_vpc_routing', true) | lower }}
  cluster_subnet_group_name = {{ config.get('cluster_subnet_group_name', 'null') }}
  vpc_security_group_ids    = {{ config.get('vpc_security_group_ids', '[]') }}

  # Logging
  logging {
    enable               = {{ config.get('logging_enabled', true) | lower }}
    log_destination_type = "{{ config.get('log_destination_type', 'cloudwatch') }}"
    log_exports          = {{ config.get('log_exports', '["connectionlog", "userlog", "useractivitylog"]') }}
  }

  # Maintenance
  preferred_maintenance_window     = "{{ config.get('preferred_maintenance_window', 'sun:04:00-sun:05:00') }}"
  automated_snapshot_retention_period = {{ config.get('automated_snapshot_retention_period', 7) }}
  allow_version_upgrade            = {{ config.get('allow_version_upgrade', true) | lower }}

  # Deletion
  skip_final_snapshot       = {{ config.get('skip_final_snapshot', false) | lower }}
  final_snapshot_identifier = "{{ config.get('final_snapshot_identifier', project_id ~ '-redshift-final') }}"

  tags = {
    Name      = "{{ config.get('cluster_identifier', project_id ~ '-redshift') }}"
    ManagedBy = "complyform"
  }
}

variable "redshift_master_password" {
  description = "Redshift cluster master password"
  type        = string
  sensitive   = true
}
