data "aws_ecs_cluster" "main" {
  cluster_name = "${local.name_prefix}-clu"
}

module "nexus_task_def" {
  source = "../../../modules/ecs/tasks/fargate_v3"

  container_image = "sonatype/nexus3:3.68.1"
  container_name  = "nexus3"
  cpu             = 4096
  memory          = 8192
  env_prefix      = local.env_prefix
  family          = "${local.name_prefix}-nexus3"
  project_group   = local.project_group

  awslogs_retention = 90

  exec_role_arn = data.aws_iam_role.ecs_task_runner_role.arn
  task_role_arn = module.nexus_role.role.arn

  efs_file_system_id = aws_efs_file_system.nexus_efs_file_system.id
  efs_volume_enabled = true
  efs_volume_name    = "nexus"
  efs_root_directory = "/"

  mount_points = [
    {
      containerPath = "/nexus-data/",
      readOnly      = false,
      sourceVolume  = "nexus"
    }
  ]

  port_mappings = [
    {
      containerPort = 8081
      hostPort      = 8081
      protocol      = "tcp"
    }
  ]

  ulimits = [
    {
      name      = "nofile"
      hardLimit = 65536
      softLimit = 65536
    }
  ]

  common_tags = merge(
    local.common_tags,
    {
      project                  = "Nexus",
      service                  = "ECS",
      plat_env_project_service = "${local.aggregated_tag}_WP_ECS"
    }
  )
}

# Target group for ECS service
resource "aws_lb_target_group" "external_target_group" {
  name                 = "${local.name_prefix}-nexus3-external"
  port                 = 80
  protocol             = "HTTP"
  vpc_id               = data.aws_vpc.main.id
  deregistration_delay = 60
  target_type          = "ip"

  health_check {
    path                = "/"
    port                = "traffic-port"
    protocol            = "HTTP"
    matcher             = "200"
    timeout             = 30
    healthy_threshold   = 10
    interval            = 300
    unhealthy_threshold = 10
  }

  tags = merge(
    local.common_tags,
    {
      project                  = "Nexus",
      service                  = "ECS",
      plat_env_project_service = "${local.aggregated_tag}_WP_ECS"
    }
  )
}

resource "aws_lb_target_group" "internal_target_group" {
  name                 = "${local.name_prefix}-nexus3-internal"
  port                 = 80
  protocol             = "HTTP"
  vpc_id               = data.aws_vpc.main.id
  deregistration_delay = 60
  target_type          = "ip"

  health_check {
    path                = "/"
    port                = "traffic-port"
    protocol            = "HTTP"
    matcher             = "200"
    timeout             = 30
    healthy_threshold   = 10
    interval            = 300
    unhealthy_threshold = 10
  }

  tags = merge(
    local.common_tags,
    {
      project                  = "Nexus",
      service                  = "ECS",
      plat_env_project_service = "${local.aggregated_tag}_WP_ECS"
    }
  )
}

module "alb_external_rules" {
  source             = "../../../modules/ec2/alb/https_rule_with_redirect"
  https_listener_arn = data.aws_alb_listener.private_alb_https.arn
  tg_arn             = aws_lb_target_group.external_target_group.arn
  host-header        = "artifacts.apollo.stream"
}

module "alb_internal_rules" {
  source             = "../../../modules/ec2/alb/https_rule_with_redirect"
  https_listener_arn = data.aws_alb_listener.internal_alb_https.arn
  tg_arn             = aws_lb_target_group.internal_target_group.arn
  host-header        = "artifacts-internal.delphiplatform.io"
}

# ECS Service
resource "aws_ecs_service" "nexus_ecs_webservice" {
  name             = "${local.name_prefix}-nexus3"
  cluster          = data.aws_ecs_cluster.main.arn
  task_definition  = module.nexus_task_def.arn
  launch_type      = "FARGATE"
  platform_version = "1.4.0"
  desired_count    = 1

  deployment_maximum_percent         = 100
  deployment_minimum_healthy_percent = 0

  load_balancer {
    target_group_arn = aws_lb_target_group.external_target_group.arn
    container_name   = "nexus3"
    container_port   = 8081
  }

  load_balancer {
    target_group_arn = aws_lb_target_group.internal_target_group.arn
    container_name   = "nexus3"
    container_port   = 8081
  }

  network_configuration {
    subnets          = data.aws_subnets.private_subnets.ids
    security_groups  = [data.aws_security_group.nexus.id, data.aws_security_group.efs.id]
    assign_public_ip = false
  }

  enable_ecs_managed_tags = true
  propagate_tags          = "TASK_DEFINITION"
  tags = merge(
    local.common_tags,
    {
      project                  = "Nexus",
      service                  = "ECS",
      plat_env_project_service = "${local.aggregated_tag}_WP_ECS"
    }
  )

  wait_for_steady_state = true

  lifecycle {
    ignore_changes = [desired_count]
  }
}
