data "aws_alb" "alb" {
  name = "${local.name_prefix}-alb-internal"
}

data "aws_alb" "nlb" {
  name = "${local.name_prefix}-private-nlb"
}

data "aws_alb_listener" "alb_https" {
  load_balancer_arn = data.aws_alb.alb.arn
  port              = 443
}

data "aws_alb_listener" "alb_http" {
  load_balancer_arn = data.aws_alb.alb.arn
  port              = 80
}

# Load balancing rules
module "tg_octopus" {
  source               = "../../../modules/ec2/tg"
  name_prefix          = local.name_prefix
  common_tags          = local.infra_ec2_tags
  tg_name              = "octopus-server"
  tg_vpc_id            = data.aws_vpc.main.id
  tg_port              = 80
  tg_health_check_path = "/app"
  tg_targets           = module.octopus_server_instance.ec2[*].id
}

module "alb_rules_octopus" {
  source             = "../../../modules/ec2/alb/https_rule_with_redirect"
  https_listener_arn = data.aws_alb_listener.alb_https.arn
  http_listener_arn  = data.aws_alb_listener.alb_http.arn
  tg_arn             = module.tg_octopus.tg.arn
  host-header        = local.octopus_url
}

resource "aws_lb_listener_certificate" "example" {
  listener_arn    = data.aws_alb_listener.alb_https.arn
  certificate_arn = aws_acm_certificate.wildcard_delphi_zone_cert.arn
}
