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.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/snowflake-sink-nrgraph/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"]
}

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

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

  providers = {
    aws.dns = aws.networking
  }

  environment = var.environment
  # Shortened to accommodate AWS name length limits
  service_name                      = local.service_name
  application_family                = var.application_family
  additional_tags                   = var.additional_tags
  non_ecr_image                     = "086679231553.dkr.ecr.${var.aws_region}.amazonaws.com/${var.ecr_name}:latest"
  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                       = 3072
  container_port                    = 8083
  health_check_grace_period_seconds = 180
  container_start_period_seconds    = 180
  blocking_waf_enabled              = true
  health_check_path                 = "/connectors/${local.service_name}"
  web_service_health_check_command  = "[[ $(curl -s http://localhost:8083/connectors/${local.service_name}/status | jq -r '.tasks[].state' | sort | uniq) == 'RUNNING' ]] || exit 1"

  # Fargate SPOT Task, QA only
  force_new_deployment                  = true
  fargate_spot_capacity_provider_weight = 100
  task_placement_failure_alert_enabled  = true


  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_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 = local.service_name
    },
    {
      CONNECTOR_TYPE = "default_sink"
    },
  ]
  environment_variables = [
    {
      Environment = var.environment
    },
    {
      CONNECT_GROUP_UNIQUE_IDENTIFIER = local.ecs_cluster_name
    },
    {
      CONNECTOR_NAME = local.service_name
    },
    {
      MAX_TASKS = 1
    },
    {
      BUFFER_COUNT_RECORDS = 10000
    },
    {
      BUFFER_SIZE_BYTES = 5000000
    },
    {
      CONNECT_BOOTSTRAP_SERVERS = var.connect_bootstrap_servers
    },
    {
      SNOWFLAKE_INGESTION_METHOD = "SNOWPIPE_STREAMING"
    },
    {
      SNOWFLAKE_HOST = "delphi.us-east-1.snowflakecomputing.com:443"
    },
    {
      SNOWFLAKE_USER = "QA_KAFKA_CONNECT"
    },
    {
      SNOWFLAKE_ROLE = "FACTS_DB_QA_SCHEMA_READWRITE"
    },
    {
      SNOWFLAKE_DATABASE = "FACTS"
    },
    {
      SNOWFLAKE_SCHEMA = "QA"
    },
    {
      DEBUG_MODE = "true"
    },
    {
      SERVICE_NAME = local.service_name
    },
    {
      KAFKA_TOPICS = join(",", [
        "cdc.nrGraph.contributedTo",
        "cdc.nrGraph.hasFeaturedArtist",
        "cdc.nrGraph.hasNrContributor",
        "cdc.nrGraph.hasOwnershipRuleCarveout",
        "cdc.nrGraph.hasOwnershipRuleMonetize",
        "cdc.nrGraph.hasPrimaryArtist",
        "cdc.nrGraph.isFeaturingPerformer",
        "cdc.nrGraph.isMainPerformer",
        "cdc.nrGraph.isSessionMusician",
        "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.owns",
        "cdc.nrGraph.includes",
        "cdc.nrGraph.isPlacementOf"
      ])
    },
    {
      SNOWFLAKE_TOPIC_TABLE_MAP = join(",", [
        "cdc.nrGraph.contributedTo:CDC_NRGRAPH_CONTRIBUTED_TO",
        "cdc.nrGraph.hasFeaturedArtist:CDC_NRGRAPH_HAS_FEATURED_ARTIST",
        "cdc.nrGraph.hasNrContributor:CDC_NRGRAPH_HAS_NR_CONTRIBUTOR",
        "cdc.nrGraph.hasOwnershipRuleCarveout:CDC_NRGRAPH_HAS_OWNERSHIP_RULE_CARVEOUT",
        "cdc.nrGraph.hasOwnershipRuleMonetize:CDC_NRGRAPH_HAS_OWNERSHIP_RULE_MONETIZE",
        "cdc.nrGraph.hasPrimaryArtist:CDC_NRGRAPH_HAS_PRIMARY_ARTIST",
        "cdc.nrGraph.isFeaturingPerformer:CDC_NRGRAPH_IS_FEATURING_PERFORMER",
        "cdc.nrGraph.isMainPerformer:CDC_NRGRAPH_IS_MAIN_PERFORMER",
        "cdc.nrGraph.isSessionMusician:CDC_NRGRAPH_IS_SESSION_MUSICIAN",
        "cdc.nrGraph.nrContribution:CDC_NRGRAPH_NR_CONTRIBUTION",
        "cdc.nrGraph.nrContributor:CDC_NRGRAPH_NR_CONTRIBUTOR",
        "cdc.nrGraph.nrOwnershipService:CDC_NRGRAPH_NR_OWNERSHIP_SERVICE",
        "cdc.nrGraph.nrParticipant:CDC_NRGRAPH_NR_PARTICIPANT",
        "cdc.nrGraph.nrProduct:CDC_NRGRAPH_NR_PRODUCT",
        "cdc.nrGraph.nrSoundRecording:CDC_NRGRAPH_NR_SOUND_RECORDING",
        "cdc.nrGraph.nrTrack:CDC_NRGRAPH_NR_TRACK",
        "cdc.nrGraph.vendor:CDC_NRGRAPH_VENDOR",
        "cdc.nrGraph.subaccount:CDC_NRGRAPH_SUBACCOUNT",
        "cdc.nrGraph.owns:CDC_NRGRAPH_OWNS",
        "cdc.nrGraph.includes:CDC_NRGRAPH_INCLUDES",
        "cdc.nrGraph.isPlacementOf:CDC_NRGRAPH_IS_PLACEMENT_OF"
      ])
    }
  ]
}


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


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

  environment        = var.environment
  application_family = var.application_family
  additional_tags    = var.additional_tags
  environment_type   = "fargate"
  service_name       = local.service_name
  connector_type     = "default_sink"

  notification_endpoints            = "@slack-kafka-data-highway-alerts"
  escalation_notification_endpoints = "@slack-data"
  monitor_kafka_topics = [
    "cdc.nrGraph.contributedTo",
    "cdc.nrGraph.hasFeaturedArtist",
    "cdc.nrGraph.hasNrContributor",
    "cdc.nrGraph.hasOwnershipRuleCarveout",
    "cdc.nrGraph.hasOwnershipRuleMonetize",
    "cdc.nrGraph.hasPrimaryArtist",
    "cdc.nrGraph.isFeaturingPerformer",
    "cdc.nrGraph.isMainPerformer",
    "cdc.nrGraph.isSessionMusician",
    "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.owns",
    "cdc.nrGraph.includes",
    "cdc.nrGraph.isPlacementOf"
  ]

  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"
  # Alert on the number of times the metric shows unhealthy tasks within the last hour
  healthy_tasks_critical_number = 5
}

