
module "kafka_jdbc_source_snowflake_dim_release_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=6.5.0"

  providers = {
    aws.dns = aws.networking
  }

  environment                           = var.environment
  service_name                          = local.service_name
  non_ecr_image                         = "086679231553.dkr.ecr.${var.aws_region}.amazonaws.com/${var.connector_type}:latest"
  application_family                    = var.application_family
  aws_region                            = var.aws_region
  service_platform_version              = "1.4.0"
  desired_task_count                    = 1
  minimum_capacity                      = 1
  maximum_capacity                      = 2
  task_cpu                              = 1024
  task_memory                           = 3072
  container_port                        = 8083
  health_check_grace_period_seconds     = 180
  container_start_period_seconds        = 180
  blocking_waf_enabled                  = true
  health_check_path                     = "/connectors/${var.connector_name}"
  web_service_health_check_command      = "[ \"$(curl -s -f http://localhost:8083/connectors/${var.connector_name}/tasks/0/status | jq -r .state)\" == 'RUNNING' ] || exit 1"
  fargate_spot_capacity_provider_weight = 100
  task_placement_failure_alert_enabled  = true
  propagate_tags                        = true
  vpc_id                                = module.vpc_info.vpc_id
  https_listener_certificate_id         = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets   = module.vpc_info.default_private_subnet_ids
  datadog_custom_image    = "086679231553.dkr.ecr.${var.aws_region}.amazonaws.com/orchard-datadog-agent-jmx:latest"
  # datadog agent crashes due to OutOfMemoryError with JMX. So increase it from 256
  datadog_task_memory = 512
  datadog_agent_environment_variables = [
    {
      JMX_PORT = "9095"
    },
    {
      DD_ENV = var.environment
    },
    {
      SERVICE_NAME = local.service_name
    },
    {
      CONNECTOR_TYPE = "default_source"
    },
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      SERVICE_NAME = local.service_name
    },
    {
      CONNECT_GROUP_UNIQUE_IDENTIFIER = "${var.environment}-${local.service_name}"
    },
    {
      CONNECT_BOOTSTRAP_SERVERS = var.connect_bootstrap_servers
    },
    {
      QUERY = replace(templatefile("${path.module}/dim_release_query.sql", {}), "\n", " ")
    },
    {
      QUOTE_SQL_IDENTIFIERS = "never"
    },
    {
      MODE = "timestamp"
    },
    {
      TIMESTAMP_COLUMN_NAME = "CDC_TIMESTAMP"
    },
    {
      TIMESTAMP_INITIAL = 1767229200 # 2026-01-01 01:00:00.000 since this is a new CDC table. For old records we will backfill using db-deploy,
    },
    {
      TOPIC_PREFIX = "cdc.snowflakeFacts.dim_release"
    },
    {
      EXTRACT_NESTED_OBJECT = "true"
    },
    {
      FIELD_NAME_FOR_KEY = "ID"
    },
    {
      FIELD_NAME_FOR_VALUE = "RECORD"
    },
    {
      POLL_INTERVAL_MS = "300000" # 5 mins
    },
    {
      BATCH_MAX_ROWS = "1000"
    },
    {
      QUERY_RETRY_ATTEMPTS = "5"
    },
    {
      SNOWFLAKE_CONFIGURATION = "true"
    },
    {
      SNOWFLAKE_SUBDOMAIN = "delphi.us-east-1"
    },
    {
      SNOWFLAKE_WAREHOUSE = "QA_ETL_WAREHOUSE"
    },
    {
      SNOWFLAKE_DATABASE = "FACTS"
    },
    {
      SNOWFLAKE_ROLE = "FACTS_DB_QA_SCHEMA_READ"
    },
    {
      SNOWFLAKE_SCHEMA = "QA"
    },
    {
      SNOWFLAKE_USER = "QA_KAFKA_CONNECT_JDBC_SOURCE_DIM_RELEASE"
    },
    {
      SNOWFLAKE_PRIVATE_KEY_FILENAME = "rsa_key.p8"
    },
    {
      VALIDATE_NON_NULL = "true"
    },
    {
      DEBUG_MODE = "true"
    },
    {
      AWS_SECRETS_MANAGER = "true"
    },
  ]
  secrets = [
    {
      SNOWFLAKE_PRIVATE_KEY_SECRET = "${var.environment}/${local.service_name}/SNOWFLAKE_PRIVATE_KEY"
    }
  ]
}

module "kafka_dim_release_os_ingest_secrets" {
  source = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"

  environment        = var.environment
  application_family = var.application_family
  service_name       = local.service_name
  secret_name        = "SNOWFLAKE_PRIVATE_KEY"
  additional_tags    = var.additional_tags
}

# This rule will allow kafka-connect tasks to communicate with each other in distributed mode
resource "aws_security_group_rule" "allow_intra_cluster_communication" {
  from_port         = 8083
  to_port           = 8083
  protocol          = "tcp"
  security_group_id = module.kafka_jdbc_source_snowflake_dim_release_fargate_environment.fargate_security_group_id
  self              = true
  type              = "ingress"
}

module "fargate_service_dashboard_dim_release_os_ingest" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/kafka_connector?ref=6.18.2"

  environment        = var.environment
  application_family = var.application_family
  additional_tags    = var.additional_tags
  service_name       = local.service_name
  connector_type     = "default_source"

  environment_type = "fargate"

  notification_endpoints            = "@slack-kafka-data-highway-alerts"
  escalation_notification_endpoints = "@slack-kafka-data-highway-alerts"
  monitor_kafka_topics              = ["cdc.snowflakeFacts.dim_release"]

  healthy_tasks_monitor_enabled = true
  service_cpu_monitor_enabled   = true
  error_monitor_enabled         = true

  error_monitor_critical_number_errors = 10
  healthy_tasks_evaluation_window      = "last_2h"
  service_cpu_time_window              = "last_1h"
}

