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

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
  }
}

provider "kafka-connect" {
  url = "https://${local.ecs_cluster_name}.dev.theorchard.io"
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "dev-orcd-terraform-state"
    key     = "dev/kafka-infra/db_deploy/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.1.0"

  environment = var.environment
}

resource "aws_ecr_repository" "kafka_connect_db_deploy" {
  name = local.service_name
  image_scanning_configuration {
    scan_on_push = true
  }
  encryption_configuration {
    encryption_type = "KMS"
  }
}

module "kafka_db_deploy_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
  service_name       = local.service_name
  secret_name        = each.value
  application_family = var.application_family
}

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

  providers = {
    aws.dns = aws
  }

  environment                       = var.environment
  service_name                      = local.service_name
  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                  = 1
  task_cpu                          = 1024
  task_memory                       = 2048
  container_port                    = 8083
  health_check_grace_period_seconds = 180
  blocking_waf_enabled              = true
  health_check_path                 = "/connectors"
  web_service_health_check_command  = "[[ /etc/confluent/docker/healthcheck.sh ]] || exit 1"
  vpc_id                            = module.vpc_info.vpc_id
  fargate_service_subnets           = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets             = module.vpc_info.default_private_subnet_ids
  https_listener_certificate_id     = "a1a3c6c2-523f-4f79-ba24-ce69f21ac066"
  route53_record_creation_enabled   = true
  environment_variables = [
    {
      Environment = var.environment
    },
    {
      SERVICE_NAME = local.service_name
    },
    {
      CONNECT_GROUP_ID = local.ecs_cluster_name
    },
    {
      CONNECT_BOOTSTRAP_SERVERS = var.connect_bootstrap_servers
    },
    {
      SECRETS_MANAGER_SECRET_NAMES = join(",", var.secrets_manager_secret_names)
    }
  ]
}

module "db_deploy_status_dynamodb_table" {
  source = "git@github.com:theorchard/terraform-dynamodb.git//?ref=3.3.1"

  env                = var.environment
  application_family = var.application_family
  table_name         = "kafka_db_deploy_status"
  hash_key           = "changeset_id"
  use_on_demand = false

  attribute = [
    {
      name = "changeset_id"
      type = "S"
    },
  ]
}

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