locals {
  datadog_agent_service_name = "datadog-agent-mysql-${var.service_name}"
}

module "secrets" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each = toset(var.secrets_manager_secret_names)

  environment        = var.environment
  application_family = var.application_family
  service_name       = local.datadog_agent_service_name
  secret_name        = each.value
}

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

  providers = {
    aws.dns = aws.networking
  }

  application_family                    = var.application_family
  environment                           = var.environment
  service_name                          = local.datadog_agent_service_name
  aws_region                            = var.aws_region
  commit_sha                            = "latest"
  container_port                        = "8080"
  task_type                             = "worker"
  desired_task_count                    = 1
  task_cpu                              = 512
  task_memory                           = 1024
  maximum_capacity                      = 1
  minimum_capacity                      = 1
  vpc_id                                = module.vpc_info.vpc_id
  fargate_service_subnets               = module.vpc_info.default_private_subnet_ids
  health_check_command                  = "agent health"
  fargate_spot_capacity_provider_weight = 100
  ows_machine_to_machine_enabled        = false
  splitio_enabled                       = false

  use_custom_task_definition_file = true
  task_definition_file_location = templatefile("task-definitions/task_definition.json.tpl", {
    environment         = var.environment
    mysql_hosts         = tolist(module.art_relations_rds.rds_instance_endpoints)
    service_name        = local.datadog_agent_service_name
    dd_tag_environment  = var.environment
    dd_tag_service_name = var.service_name
  })
}

module "fargate_service_dashboard" {
  source                            = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.13.4"
  environment                       = var.environment
  environment_type                  = "fargate"
  service_name                      = local.datadog_agent_service_name
  application_family                = var.application_family
  service_5xx_monitor_enabled       = false
  service_4xx_monitor_enabled       = false
  service_cpu_monitor_enabled       = false
  failed_tasks_monitor_enabled      = true
  notification_endpoints            = "@slack-devops"
  escalation_notification_endpoints = "@slack-devops"
}
