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

resource "aws_ecs_cluster" "this" {
  name = "{{ config.get('name', project_id ~ '-cluster') }}"

  setting {
    name  = "containerInsights"
    value = "{{ config.get('container_insights', 'enabled') }}"
  }

  configuration {
    execute_command_configuration {
      logging = "{{ config.get('execute_command_logging', 'OVERRIDE') }}"

      {% if config.get('execute_command_logging', 'OVERRIDE') == 'OVERRIDE' %}
      log_configuration {
        cloud_watch_log_group_name = "{{ config.get('log_group_name', '/ecs/' ~ project_id ~ '-cluster') }}"
      }
      {% endif %}
    }
  }

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

resource "aws_cloudwatch_log_group" "ecs" {
  name              = "{{ config.get('log_group_name', '/ecs/' ~ project_id ~ '-cluster') }}"
  retention_in_days = {{ config.get('log_retention_days', 90) }}
  kms_key_id        = {{ config.get('kms_key_id', 'null') }}

  tags = {
    Name      = "{{ project_id }}-ecs-logs"
    ManagedBy = "complyform"
  }
}

{% if config.get('capacity_providers', ['FARGATE']) %}
resource "aws_ecs_cluster_capacity_providers" "this" {
  cluster_name = aws_ecs_cluster.this.name

  capacity_providers = {{ config.get('capacity_providers', '["FARGATE", "FARGATE_SPOT"]') }}

  default_capacity_provider_strategy {
    base              = {{ config.get('default_base', 1) }}
    weight            = {{ config.get('default_weight', 100) }}
    capacity_provider = "{{ config.get('default_capacity_provider', 'FARGATE') }}"
  }
}
{% endif %}
