terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "qa/kafka-infra/neo4j_cdc_source/mg_fingerprint/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

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          = var.dd_team
}

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

# VPC Info
data "aws_caller_identity" "current" {}

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

  environment = var.environment
}

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

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

# Fargate module
module "kc_neo_src_mg_fingerprint_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=6.3.0"

  providers = {
    aws.dns = aws.networking
  }

  environment                           = var.environment
  application_family                    = var.application_family
  additional_tags                       = var.additional_tags
  service_name                          = local.service_name
  non_ecr_image                         = "086679231553.dkr.ecr.us-east-1.amazonaws.com/kc-neo-cdc-src:latest"
  aws_region                            = var.aws_region
  service_platform_version              = "1.4.0"
  desired_task_count                    = 1
  minimum_capacity                      = 1
  maximum_capacity                      = 2
  task_cpu                              = 2048
  task_memory                           = 4096
  container_port                        = 8083
  health_check_grace_period_seconds     = 300
  container_start_period_seconds        = 300
  blocking_waf_enabled                  = true
  health_check_path                     = "/connectors/neo4j_cdc_source"
  web_service_health_check_command      = "[[ $(curl -s http://localhost:8083/connectors/neo4j_cdc_source/status | jq -r '.tasks[].state' | sort | uniq) == 'RUNNING' ]] || exit 1"

  propagate_tags                        = true
  vpc_id                                = module.vpc_info.vpc_id
  https_listener_certificate_id         = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]
  fargate_service_subnets               = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets                 = module.vpc_info.default_private_subnet_ids

  # datadog agent crashes due to OutOfMemoryError with JMX. So increase it from 256
  datadog_task_memory  = 512
  datadog_custom_image = "086679231553.dkr.ecr.${var.aws_region}.amazonaws.com/orchard-datadog-agent-jmx:latest"
  datadog_agent_environment_variables = [
    {
      JMX_PORT = "9095"
    },
    {
      DD_ENV = var.environment
    },
    {
      SERVICE_NAME = local.service_name
    },
    {
      CONNECTOR_TYPE = "default_source"
    },
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      CONNECT_GROUP_UNIQUE_IDENTIFIER = "${var.environment}-${local.service_name}"
    },
    {
      SERVICE_NAME = local.service_name
    },
    {
      CONNECT_BOOTSTRAP_SERVERS = var.msk_brokers
    },
    {
      NEO4J_SERVER_URI = "neo4j+s://qa-neo4j-cluster.theorchard.io:7687"
    },
    {
      NEO4J_DATABASE_NAME = "graph.db"
    },
    {
      MAX_TASKS = "1"
    },
    {
      NEO4J_ENCRYPTION_ENABLED = "true"
    },
    {
      NEO4J_ENFORCE_SCHEMA = "true"
    },
    {
      NEO4J_BATCH_SIZE = "100000"
    },
    {
      NEO4J_POLL_INTERVAL = "1s"
    },
    {
      NEO4J_KEY_CONVERTER = "org.apache.kafka.connect.storage.StringConverter"
    },
    {
      NEO4J_VALUE_CONVERTER = "org.apache.kafka.connect.json.JsonConverter"
    },
    {
      NEO4J_KEY_CONVERTER_SCHEMAS_ENABLE = "false"
    },
    {
      NEO4J_VALUE_CONVERTER_SCHEMAS_ENABLE = "false"
    },
    {
      AWS_SECRETS_MANAGER = "true"
    },
    {
      DEBUG_MODE = "false"
    },
    # this is to ignore the offset stored in topic post DB refresh to avoid failures.
    {
      NEO4J_IGNORE_STORED_OFFSET = "true"
    },
    {
      NEO4J_START_FROM = "NOW"
    },
    {
      NEO4J_KEY_STRATEGY = "ELEMENT_ID"
    },
    {
      CONNECT_LOG4J_ROOT_LOGLEVEL = "WARN"
    },
    # edges
    {
      TOPIC_MAPPING_HAS_FINGERPRINT_RULE = "cdc.musicGraphV5.hasFingerprintRule|()-[:HAS_FINGERPRINT_RULE]->()"
    }
  ]
}

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

# Secrets for Neo4j creds
module "service_credentials_secret" {
  source = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"

  environment                    = var.environment
  application_family             = var.application_family
  additional_tags                = var.additional_tags
  service_name                   = local.service_name
  secret_name                    = "NEO4J_CREDENTIALS"
  secret_recovery_window_in_days = 7
}

# Datadog Dashboard
module "fargate_service_dashboard_neo4j_v5" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/kafka_connector?ref=6.13.7"

  environment        = var.environment
  application_family = var.application_family
  additional_tags    = var.additional_tags
  service_name       = local.service_name
  connector_type     = "default_source"

  environment_type = "fargate"

  notification_endpoints            = "@slack-kafka-data-highway-alerts"
  escalation_notification_endpoints = "@slack-data-alarm @slack-kafka-data-highway-alerts"
  monitor_kafka_topics_cluster_name = "${var.environment}-${var.msk_cluster_name}"
  monitor_kafka_topics = [
    "cdc.musicGraphV5.hasFingerprintRule",
  ]

  healthy_tasks_monitor_enabled   = true
  service_cpu_monitor_enabled     = true
  error_monitor_enabled           = true
  error_monitor_extra_query       = " -\\\"Given ChangeIdentifier describes a transaction that hasn't yet occurred\\\"" # Expected/non-issue

  healthy_tasks_evaluation_window = "last_15m"
  service_cpu_time_window         = "last_15m"
}
