provider "aws" {
  region = var.aws_region
}

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

data "aws_caller_identity" "current" {}

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

  environment        = var.environment
  application_family = var.application_family
  additional_tags    = var.additional_tags
  # Shortened to accommodate AWS name length limits
  service_name                      = "${var.service_name}-es-sink"
  non_ecr_image                     = "086679231553.dkr.ecr.${var.aws_region}.amazonaws.com/${var.service_name}-es-sink:latest"
  aws_region                        = var.aws_region
  service_platform_version          = "1.4.0"
  desired_task_count                = 0
  minimum_capacity                  = 0
  maximum_capacity                  = 0
  task_cpu                          = 1024
  task_memory                       = 2048
  container_port                    = 8083
  health_check_grace_period_seconds = 180
  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' | sort | uniq) == 'RUNNING' ]] || exit 1"
  vpc_id                            = "vpc-34dbfd51"

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      CONNECT_BOOTSTRAP_SERVERS = var.connect_bootstrap_servers
    },
    {
      ELASTICSEARCH_HOST = "https://vpc-dev-mymac-kafka-neo4j-swmab7laolvwt7rp7utfnhtbxm.us-east-1.es.amazonaws.com/"
    },
    {
      ELASTICSEARCH_DOC_TYPE = "_doc"
    },
    {
      ELASTICSEARCH_KEY_IGNORE = "false"
    },
    {
      ELASTICSEARCH_SCHEMA_IGNORE = "true"
    },
    {
      KAFKA_TOPICS = "default_node_topic,topic.musicGraph.globalSoundRecording.elasticSearch,topic.musicGraph.globalParticipant.elasticSearch"
    },
  ]
}

# 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.elasticsearch_sink_fargate_environment.fargate_security_group_id
  self              = true
  type              = "ingress"
}


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

  environment        = var.environment
  application_family = var.application_family
  additional_tags    = var.additional_tags
  # Shortened to accommodate AWS name length limits
  service_name                      = "${var.service_name}-es-sink-lp"
  non_ecr_image                     = "086679231553.dkr.ecr.${var.aws_region}.amazonaws.com/${var.service_name}-es-sink:latest"
  aws_region                        = var.aws_region
  service_platform_version          = "1.4.0"
  desired_task_count                = 0
  minimum_capacity                  = 0
  maximum_capacity                  = 0
  task_cpu                          = 1024
  task_memory                       = 2048
  container_port                    = 8083
  health_check_grace_period_seconds = 180
  # connector_name cannot have hyphens.
  health_check_path                = "/connectors/${var.connector_name}_lp"
  web_service_health_check_command = "[[ $(curl -s http://localhost:8083/connectors/${var.connector_name}/status | jq -r '.tasks[].state' | sort | uniq) == 'RUNNING' ]] || exit 1"
  vpc_id                           = "vpc-34dbfd51"

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      CONNECT_BOOTSTRAP_SERVERS = var.connect_bootstrap_servers
    },
    {
      CONNECT_GROUP_UNIQUE_IDENTIFIER = "${var.environment}-${var.service_name}-es-sink-lp"
    },
    {
      CONNECTOR_NAME = "${var.environment}-${var.service_name}_lp"
    },
    {
      ELASTICSEARCH_HOST = "https://vpc-dev-music-graph-acmopy7lxup33smbtog2jyu2be.us-east-1.es.amazonaws.com/"
    },
    {
      ELASTICSEARCH_DOC_TYPE = "_doc"
    },
    {
      ELASTICSEARCH_KEY_IGNORE = "false"
    },
    {
      ELASTICSEARCH_SCHEMA_IGNORE = "true"
    },
    {
      KAFKA_TOPICS = "topic.musicGraph.labelParticipant.elasticSearch,topic.musicGraph.delete.labelParticipant.elasticSearch"
    },
    {
      SERVICE_NAME = "${var.service_name}-es-sink-lp"
    },
    {
      WRITE_METHOD = "upsert"
    },
    # Transform topic name to ES index name.
    {
      TRANSFORMS = "renameTopic"
    },
    {
      TRANSFORMS_RENAMETOPIC_REGEX = "topic.*"
    },
    {
      TRANSFORMS_RENAMETOPIC_TYPE = "org.apache.kafka.connect.transforms.RegexRouter"
    },
    {
      TRANSFORMS_RENAMETOPIC_REPLACEMENT = "label_participant_write"
    }
  ]
}


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