{% set config = merged_configs.get('aws_elasticache_replication_group', {}) %}
{% set comments = compliance_comments.get('aws_elasticache_replication_group', []) %}
# ------------------------------------------------------------------------------
# ElastiCache Replication Group
# ------------------------------------------------------------------------------
{% for comment in comments %}
# {{ comment }}
{% endfor %}

resource "aws_elasticache_replication_group" "this" {
  replication_group_id = "{{ config.get('replication_group_id', project_id ~ '-cache') }}"
  description          = "{{ config.get('description', project_id ~ ' ElastiCache replication group') }}"

  engine               = "{{ config.get('engine', 'redis') }}"
  engine_version       = "{{ config.get('engine_version', '7.1') }}"
  node_type            = "{{ config.get('node_type', 'cache.t3.micro') }}"
  num_cache_clusters   = {{ config.get('num_cache_clusters', 2) }}
  port                 = {{ config.get('port', 6379) }}
  parameter_group_name = "{{ config.get('parameter_group_name', 'default.redis7') }}"

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

  # Auth
  {% if config.get('auth_token') %}
  auth_token = var.elasticache_auth_token
  {% endif %}

  # High availability
  automatic_failover_enabled = {{ config.get('automatic_failover_enabled', true) | lower }}
  multi_az_enabled           = {{ config.get('multi_az_enabled', true) | lower }}

  # Network
  subnet_group_name  = {{ config.get('subnet_group_name', 'null') }}
  security_group_ids = {{ config.get('security_group_ids', '[]') }}

  # Maintenance
  maintenance_window       = "{{ config.get('maintenance_window', 'sun:05:00-sun:06:00') }}"
  auto_minor_version_upgrade = {{ config.get('auto_minor_version_upgrade', true) | lower }}
  snapshot_retention_limit = {{ config.get('snapshot_retention_limit', 7) }}
  snapshot_window          = "{{ config.get('snapshot_window', '03:00-04:00') }}"

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