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       = var.service_name
  team_name          = var.team_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/nr-aura-cdc-source/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
}

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

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


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

  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.ecr_name}:latest"
  aws_region                            = var.aws_region
  desired_task_count                    = 1
  minimum_capacity                      = 1
  maximum_capacity                      = 1
  task_cpu                              = 2048
  task_memory                           = 4096
  container_port                        = 8083
  health_check_grace_period_seconds     = 180
  container_start_period_seconds        = 180
  blocking_waf_enabled                  = true
  health_check_path                     = "/connectors/${var.connector_name}"
  web_service_health_check_command      = "[[ $(curl -s http://localhost:8083/connectors/${var.connector_name}/status | jq -r '.tasks[].state,.connector.state' | sort | uniq) == 'RUNNING' ]] || exit 1"
  vpc_id                                = module.vpc_info.vpc_id
  propagate_tags                        = true
  fargate_spot_capacity_provider_weight = 100
  task_placement_failure_alert_enabled  = true

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets   = module.vpc_info.default_private_subnet_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"
    },
    {
      DD_ENV = var.environment
    },
    {
      SERVICE_NAME = var.service_name
    },
    {
      CONNECTOR_TYPE = "default_source"
    },
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      CONNECT_GROUP_UNIQUE_IDENTIFIER = "${var.environment}-${var.service_name}"
    },
    {
      SERVICE_NAME = var.service_name
    },
    {
      MAX_TASKS = 1
    },
    {
      CONNECT_BOOTSTRAP_SERVERS = var.connect_bootstrap_servers
    },
    {
      NEO4J_SERVER_URI = "neo4j+s://b4820bcb.databases.neo4j.io"
    },
    {
      NEO4J_DATABASE_NAME = "neo4j"  # Default is graph.db
    },
    {
      NEO4J_ENCRYPTION_ENABLED = "true"
    },
    {
      NEO4J_ENFORCE_SCHEMA = "true"
    },
    {
      NEO4J_BATCH_SIZE = "1000"
    },
    {
      NEO4J_POLL_INTERVAL = "1s"
    },
    {
      NEO4J_CONNECTION_TIMEOUT = "60s"
    },
    {
      NEO4J_CONNECTION_ACQUISITION_TIMEOUT = "120s"
    },
    {
      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"
    },
    {
      TOPIC_MAPPING_CONTRIBUTION = "cdc.nrGraph.nrContribution|(:NrContribution)"
    },
    {
      TOPIC_MAPPING_CONTRIBUTOR = "cdc.nrGraph.nrContributor|(:NrContributor)"
    },
    {
      TOPIC_MAPPING_OWNERSHIP_SERVICE = "cdc.nrGraph.nrOwnershipService|(:NrOwnershipService:NrOwnership)"
    },
    {
      TOPIC_MAPPING_PARTICIPANT = "cdc.nrGraph.nrParticipant|(:NrParticipant)"
    },
    {
      TOPIC_MAPPING_PRODUCT = "cdc.nrGraph.nrProduct|(:NrProduct)"
    },
    {
      TOPIC_MAPPING_SOUNDRECORDING = "cdc.nrGraph.nrSoundRecording|(:NrSoundRecording)"
    },
    {
      TOPIC_MAPPING_TRACK = "cdc.nrGraph.nrTrack|(:NrTrack:NrOwnership)"
    },
    {
      TOPIC_MAPPING_VENDOR = "cdc.nrGraph.vendor|(:Vendor)"
    },
    {
      TOPIC_MAPPING_SUBACCOUNT = "cdc.nrGraph.subaccount|(:Subaccount)"
    },
    {
      TOPIC_MAPPING_CONTRIBUTED_TO = "cdc.nrGraph.contributedTo|(:NrContribution)-[:CONTRIBUTED_TO]->(:NrSoundRecording)"
    },
    {
      TOPIC_MAPPING_HAS_NR_CONTRIBUTOR = "cdc.nrGraph.hasNrContributor|()-[:HAS_NR_CONTRIBUTOR]->()"
    },
    {
      TOPIC_MAPPING_HAS_FEATURED_ARTIST = "cdc.nrGraph.hasFeaturedArtist|()-[:HAS_FEATURED_ARTIST]->()"
    },
    {
      TOPIC_MAPPING_HAS_OWNERSHIP_RULE_CARVEOUT = "cdc.nrGraph.hasOwnershipRuleCarveout|()-[:HAS_OWNERSHIP_RULE_CARVEOUT]->()"
    },
    {
      TOPIC_MAPPING_HAS_OWNERSHIP_RULE_MONETIZE = "cdc.nrGraph.hasOwnershipRuleMonetize|()-[:HAS_OWNERSHIP_RULE_MONETIZE]->()"
    },
    {
      TOPIC_MAPPING_HAS_PRIMARY_ARTIST = "cdc.nrGraph.hasPrimaryArtist|()-[:HAS_PRIMARY_ARTIST]->()"
    },
    {
      TOPIC_MAPPING_IS_MAIN_PERFORMER = "cdc.nrGraph.isMainPerformer|()-[:IS_MAIN_PERFORMER]->()"
    },
    {
      TOPIC_MAPPING_IS_FEATURING_PERFORMER = "cdc.nrGraph.isFeaturingPerformer|()-[:IS_FEATURING_PERFORMER]->()"
    },
    {
      TOPIC_MAPPING_IS_SESSION_MUSICIAN = "cdc.nrGraph.isSessionMusician|()-[:IS_SESSION_MUSICIAN]->()"
    },
    {
      TOPIC_MAPPING_REPRESENTS = "cdc.nrGraph.represents|()-[:REPRESENTS]->()"
    },
    {
      TOPIC_MAPPING_OWNS = "cdc.nrGraph.owns|()-[:OWNS]->()"
    },
    {
      TOPIC_MAPPING_INCLUDES = "cdc.nrGraph.includes|()-[:INCLUDES]->()"
    },
    {
      TOPIC_MAPPING_IS_PLACEMENT_OF = "cdc.nrGraph.isPlacementOf|()-[:IS_PLACEMENT_OF]->()"
    },
    {
      AWS_SECRETS_MANAGER = "true"
    },
    {
      DEBUG_MODE = "true"
    },
    {
      NEO4J_IGNORE_STORED_OFFSET = "true"
    },
    {
      CONNECT_LOG4J_ROOT_LOGLEVEL = "WARN"
    }
  ]
}

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

# datadog monitors
module "nr_aura_cdc_source_fargate_service_dashboard" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/kafka_connector?ref=6.18.1"

  environment        = var.environment
  application_family = var.application_family
  additional_tags    = var.additional_tags
  environment_type   = "fargate"
  service_name       = var.service_name
  connector_type     = "default_source"

  notification_endpoints            = "@slack-collections-qa-alarms"
  escalation_notification_endpoints = "@slack-kafka-data-highway-alerts"
  monitor_kafka_topics              = [
    "cdc.nrGraph.nrContribution",
    "cdc.nrGraph.nrContributor",
    "cdc.nrGraph.nrOwnershipService",
    "cdc.nrGraph.nrParticipant",
    "cdc.nrGraph.nrProduct",
    "cdc.nrGraph.nrSoundRecording",
    "cdc.nrGraph.nrTrack",
    "cdc.nrGraph.vendor",
    "cdc.nrGraph.subaccount",
    "cdc.nrGraph.contributedTo",
    "cdc.nrGraph.hasNrContributor",
    "cdc.nrGraph.hasFeaturedArtist",
    "cdc.nrGraph.hasOwnershipRuleCarveout",
    "cdc.nrGraph.hasOwnershipRuleMonetize",
    "cdc.nrGraph.hasPrimaryArtist",
    "cdc.nrGraph.isMainPerformer",
    "cdc.nrGraph.isFeaturingPerformer",
    "cdc.nrGraph.isSessionMusician",
    "cdc.nrGraph.represents",
    "cdc.nrGraph.owns",
    "cdc.nrGraph.includes",
    "cdc.nrGraph.isPlacementOf",
    "cdc.nrGraph.ownershipService",
  ]
  monitor_kafka_topics_cluster_name = "qa-managed-kafka-cdc-destination"

  healthy_tasks_monitor_enabled        = true
  service_cpu_monitor_enabled          = true
  error_monitor_enabled                = true
  error_monitor_critical_number_errors = 5
  healthy_tasks_evaluation_window      = "last_1h"
  service_cpu_time_window              = "last_1h"
}
