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

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



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

  environment                           = var.environment
  application_family                    = var.application_family
  service_name                          = local.full_service_name
  non_ecr_image                         = "086679231553.dkr.ecr.${var.aws_region}.amazonaws.com/kafka-connect-pubsub-source:latest"
  aws_region                            = var.aws_region
  desired_task_count                    = 1
  minimum_capacity                      = 1
  maximum_capacity                      = 1
  task_cpu                              = 1024
  task_memory                           = 3072
  container_port                        = 8083
  health_check_grace_period_seconds     = 300
  health_check_path                     = "/connectors/${var.connector_name}"
  web_service_health_check_command      = "[ \"$(curl -s -f http://localhost:8083/connectors/${var.connector_name}/tasks/0/status | jq -r .state)\" == 'RUNNING' ] || exit 1"
  https_listener_certificate_id         = split("/", data.aws_acm_certificate.this.id)[1]
  route53_zone_id                       = data.aws_route53_zone.this.zone_id
  vpc_id                                = data.aws_vpc.this.id
  propagate_tags                        = true
  fargate_spot_capacity_provider_weight = 100
  task_placement_failure_alert_enabled  = true
  fargate_service_subnets               = toset(data.aws_subnets.private_subnets.ids)
  load_balancer_subnets                 = toset(data.aws_subnets.private_subnets.ids)
  datadog_custom_image                  = "086679231553.dkr.ecr.${var.aws_region}.amazonaws.com/orchard-datadog-agent-jmx:latest"
  # datadog agent crashes due to OutOfMemoryError with JMX. So increase it from 256
  datadog_task_memory = 512
  datadog_agent_environment_variables = [
    {
      JMX_PORT = "9095"
    },
    {
      SERVICE_NAME = local.full_service_name
    },
    {
      CONNECTOR_TYPE = "default_source"
    },
  ]
  environment_variables = [
    {
      AWS_REGION = var.aws_region
    },
    {
      Environment = var.environment
    },
    {
      CONNECT_GROUP_UNIQUE_IDENTIFIER = "${var.environment}-${local.full_service_name}"
    },
    {
      CONNECT_GROUP_ID = "${var.environment}-${local.full_service_name}"
    },
    {
      SERVICE_NAME = local.full_service_name
    },
    {
      CONNECT_BOOTSTRAP_SERVERS = join(",", var.connect_bootstrap_servers)
    },
    {
      CONNECT_KEY_CONVERTER = "org.apache.kafka.connect.storage.StringConverter"
    },
    {
      CONNECT_VALUE_CONVERTER = "org.apache.kafka.connect.converters.ByteArrayConverter"
    },
    {
      CONNECT_SCHEMAS_ENABLE = "false"
    },
    {
      CONNECTOR_CLASS = "com.google.pubsub.kafka.source.CloudPubSubSourceConnector"
    },
    {
      CONNECT_LOG4J_ROOT_LOGLEVEL = "INFO"
    },
    {
      CPS_PROJECT = var.cps_project
    },
    {
      CPS_SUBSCRIPTION = var.cps_subscription
    },
    {
      CPS_SUBSCRIPTION_PROJECT = var.cps_subscription_project
    },
    {
      CPS_SUBSCRIPTION_VERIFICATION = "false"
    },
    {
      KAFKA_TOPIC = var.kafka_topic
    },
    {
      DEBUG_MODE = "true"
    },
  ]
}


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

/* No monitors yet since I'm not completely sure who's going to be responsible for this
module "fargate_service_dashboard" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/kafka_connector?ref=6.3.0"

  environment        = var.environment
  application_family = var.application_family
  service_name       = local.full_service_name
  environment_type   = "fargate"
  connector_type     = "default_source"

  notification_endpoints            = "@slack-collaborators-public"
  escalation_notification_endpoints = "@slack-kafka-data-highway-alerts"

  monitor_kafka_topics = [var.kafka_topic]

  healthy_tasks_monitor_enabled = true
  service_cpu_monitor_enabled   = true
  error_monitor_enabled         = true
} */


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

  environment                    = var.environment
  application_family             = var.application_family
  service_name                   = local.full_service_name
  secret_name                    = each.value
  secret_recovery_window_in_days = 7
}
