module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=1.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_name
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

provider "aws" {
  region  = var.aws_region
  alias   = "networking"
  profile = "networking"

  default_tags {
    tags = module.default_tags.tags
  }
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "qa/kafka-infra/ksqldb-server/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

module "vpc_info" {
  source = "git@github.com:theorchard/terraform-vpc-info.git?ref=3.0.2"

  environment = var.environment
}

data "aws_acm_certificate" "theorchard_io" {
  domain   = "*.theorchard.io"
  statuses = ["ISSUED"]
}

data "aws_route53_zone" "route53_zone" {
  name = "theorchard.io"
}

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

  providers = {
    aws.dns = aws.networking
  }

  environment                              = var.environment
  application_family                       = var.application_family
  additional_tags                          = var.additional_tags
  service_name                             = var.service_name
  non_ecr_image                            = "086679231553.dkr.ecr.${var.aws_region}.amazonaws.com/${var.service_name}:latest"
  aws_region                               = var.aws_region
  use_custom_task_definition_file          = true
  task_definition_file_location            = file("ksqldb-server.json")
  service_platform_version                 = "1.4.0"
  desired_task_count                       = 2
  minimum_capacity                         = 2
  maximum_capacity                         = 4
  deployment_minimum_healthy_percent       = 100
  deployment_maximum_percent               = 150
  task_cpu                                 = 4096
  task_memory                              = 8192
  container_port                           = 8000
  health_check_grace_period_seconds        = 300
  container_start_period_seconds           = 300
  web_service_health_check_command         = "/usr/bin/bash /usr/local/bin/healthcheck.sh"
  health_check_path                        = "/healthcheck"
  vpc_id                                   = module.vpc_info.vpc_id
  load_balancer_access_logs_s3_bucket_name = "orch-elb-logs"
  https_listener_certificate_id            = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]
  route53_zone_id                          = data.aws_route53_zone.route53_zone.zone_id
  propagate_tags                           = true
  task_ephemeral_storage_size              = 200
  https_listener_allow_prefix_list_names   = ["shared-orcd-private-subnet-prefix-list"]
  fargate_service_subnets                  = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets                    = module.vpc_info.default_private_subnet_ids
}

# 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         = 8088
  to_port           = 8088
  protocol          = "tcp"
  security_group_id = module.ksqldb_server_fargate_environment.fargate_security_group_id
  self              = true
  type              = "ingress"
}


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

  environment        = var.environment
  application_family = var.application_family
  additional_tags    = var.additional_tags
  environment_type   = "fargate"
  service_name       = var.service_name
  teams              = ["kdh"]

  notification_endpoints            = "@slack-kafka-data-highway-alerts"
  escalation_notification_endpoints = "@slack-kafka-data-highway-alerts"
  connector_type                    = "ksqldb_server"

  healthy_tasks_monitor_enabled   = true
  healthy_tasks_evaluation_window = "last_20m"
  service_cpu_monitor_enabled     = true
  service_cpu_time_window         = "last_20m"

  error_monitor_enabled = false
}

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

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