variable "environment" {
  default = "dev"
}

variable "service_name" {}

variable "release_label" {
  default = "emr-5.33.0"
}

variable "applications" {
  default = ["Spark"]
  type    = list(any)
}

variable "configurations" {}

variable "key_name" {}

variable "subnet_id" {}

variable "job_flow_keep_alive" {
  default = false
}

variable "kms_key_enabled" {
  description = "When enabled, this will create a KMS key and set cluster encryption configuration. Using KMS is recommended, but you must grant key usage access to all the services, that interact with the cluster or its data."
  type        = bool
  default     = true
}

variable "leader_instance_group_name" { default = "LeaderInstanceGroup" }
variable "leader_instance_group_instance_type" { default = "t2.micro" }
variable "leader_instance_group_instance_count" { default = 1 }

variable "leader_instance_group_ebs_size" { default = 20 }
variable "leader_instance_group_ebs_type" { default = "gp2" }
variable "leader_instance_group_ebs_iops" {
  type    = number
  default = null
}
variable "leader_instance_group_ebs_volumes_per_instance" { default = "1" }

variable "core_instance_group_name" { default = "CoreInstanceGroup" }
variable "core_instance_group_instance_type" { default = "t2.micro" }
variable "core_instance_group_instance_count" { default = 1 }

variable "core_instance_group_ebs_size" { default = 20 }
variable "core_instance_group_ebs_type" { default = "gp2" }
variable "core_instance_group_ebs_iops" {
  type    = number
  default = null
}
variable "core_instance_group_ebs_volumes_per_instance" { default = "1" }

variable "bootstrap_name" {}

variable "bootstrap_uri" {}

variable "bootstrap_args" {
  default = []
  type    = list(any)
}

variable "log_uri" {}

variable "route53_record_ttl" {
  default = "60"
}

variable "route53_record_creation_enabled" {
  description = "Whether or not to create Route 53 DNS records"
  type        = bool
  default     = true
}

variable "override_route53_zone_id" {
  type        = string
  description = "The ID of the Route 53 zone to create the EMR DNS records in. If not specified, the zone ID is determined based on the environment."
  default     = null
}

variable "leader_ingress_ssh" {
  description = "CIDR blocks from which to allow SSH"
  type        = list(any)

  default = []
}
variable "core_ingress_ssh" {
  description = "CIDR blocks from which to allow SSH"
  type        = list(any)

  default = []
}

variable "dev_8443_listener_allow_cidr_blocks" {
  description = "CIDR blocks from which to allow 8443 for dev environments"
  type        = list(any)

  default = [
    "10.30.0.0/22",
    "10.40.0.0/22",
    "192.168.32.0/24",
    "192.168.33.0/24",
    "192.168.40.0/22",
  ]
}

variable "qa_8443_listener_allow_cidr_blocks" {
  description = "CIDR blocks from which to allow 8443 for QA environments"
  type        = list(any)

  default = [
    "192.168.31.0/24",
    "192.168.32.0/24",
    "192.168.33.0/24",
    "192.168.40.0/22",
    "10.30.0.0/22",
    "10.40.0.0/22",
  ]
}

variable "prod_8443_listener_allow_cidr_blocks" {
  description = "CIDR blocks from which to allow 8443 for prod environments"
  type        = list(any)

  default = [
    "192.168.31.0/24",
    "10.40.0.0/22",
    "10.10.40.0/22",
  ]
}

variable "dev_8998_listener_allow_cidr_blocks" {
  description = "CIDR blocks from which to allow 8998 for dev environments"
  type        = list(any)

  default = [
    "10.30.0.0/22",
    "10.40.0.0/22",
    "192.168.32.0/24",
    "192.168.33.0/24",
    "192.168.40.0/22",
  ]
}

variable "qa_8998_listener_allow_cidr_blocks" {
  description = "CIDR blocks from which to allow 8998 for QA environments"
  type        = list(any)

  default = [
    "192.168.31.0/24",
    "192.168.32.0/24",
    "192.168.33.0/24",
    "192.168.40.0/22",
    "10.30.0.0/22",
    "10.40.0.0/22",
  ]
}

variable "prod_8998_listener_allow_cidr_blocks" {
  description = "CIDR blocks from which to allow 8998 for prod environments"
  type        = list(any)

  default = [
    "192.168.31.0/24",
    "10.40.0.0/22",
    "10.10.40.0/22",
  ]
}

variable "dev_18080_listener_allow_cidr_blocks" {
  description = "CIDR blocks from which to allow 18080 for dev environments"
  type        = list(any)

  default = [
    "10.30.0.0/22",
    "10.40.0.0/22",
    "192.168.32.0/24",
    "192.168.33.0/24",
    "192.168.40.0/22",
  ]
}

variable "qa_18080_listener_allow_cidr_blocks" {
  description = "CIDR blocks from which to allow 18080 for QA environments"
  type        = list(any)

  default = [
    "192.168.31.0/24",
    "192.168.32.0/24",
    "192.168.33.0/24",
    "192.168.40.0/22",
    "10.30.0.0/22",
    "10.40.0.0/22",
  ]
}

variable "prod_18080_listener_allow_cidr_blocks" {
  description = "CIDR blocks from which to allow 18080 for prod environments"
  type        = list(any)

  default = [
    "192.168.31.0/24",
    "10.40.0.0/22",
    "10.10.40.0/22",
  ]
}

variable "tcp_8443_listener_additional_allow_cidr_blocks" {
  description = "CIDR blocks from which to allow 8443 for additional services"
  type        = list(any)
  default     = []
}

variable "tcp_8998_listener_additional_allow_cidr_blocks" {
  description = "CIDR blocks from which to allow 8998 for additional services"
  type        = list(any)
  default     = []
}

variable "tcp_18080_listener_additional_allow_cidr_blocks" {
  description = "CIDR blocks from which to allow 18080 for additional services"
  type        = list(any)
  default     = []
}

variable "application_family" {
  type        = string
  description = "Application family to which this service belongs"
}

variable "additional_tags" {
  type        = map(string)
  description = "Optional map of additional tags to set on resources. These will be combined with programmatically set required tags."
  default     = {}
}

locals {
  tcp_8443_listener_allow_cidr_blocks = {
    dev  = concat(var.dev_8443_listener_allow_cidr_blocks, var.tcp_8443_listener_additional_allow_cidr_blocks)
    qa   = concat(var.qa_8443_listener_allow_cidr_blocks, var.tcp_8443_listener_additional_allow_cidr_blocks)
    prod = concat(var.prod_8443_listener_allow_cidr_blocks, var.tcp_8443_listener_additional_allow_cidr_blocks)
  }
  tcp_8443_listener_allow_cidr_blocks_string = {
    dev  = join(",", concat(var.dev_8443_listener_allow_cidr_blocks, var.tcp_8443_listener_additional_allow_cidr_blocks))
    qa   = join(",", concat(var.qa_8443_listener_allow_cidr_blocks, var.tcp_8443_listener_additional_allow_cidr_blocks))
    prod = join(",", concat(var.prod_8443_listener_allow_cidr_blocks, var.tcp_8443_listener_additional_allow_cidr_blocks))
  }
  tcp_8998_listener_allow_cidr_blocks = {
    dev  = concat(var.dev_8998_listener_allow_cidr_blocks, var.tcp_8998_listener_additional_allow_cidr_blocks)
    qa   = concat(var.qa_8998_listener_allow_cidr_blocks, var.tcp_8998_listener_additional_allow_cidr_blocks)
    prod = concat(var.prod_8998_listener_allow_cidr_blocks, var.tcp_8998_listener_additional_allow_cidr_blocks)
  }
  tcp_18080_listener_allow_cidr_blocks = {
    dev  = concat(var.dev_18080_listener_allow_cidr_blocks, var.tcp_18080_listener_additional_allow_cidr_blocks)
    qa   = concat(var.qa_18080_listener_allow_cidr_blocks, var.tcp_18080_listener_additional_allow_cidr_blocks)
    prod = concat(var.prod_18080_listener_allow_cidr_blocks, var.tcp_18080_listener_additional_allow_cidr_blocks)
  }

  tags = merge({
    environment        = var.environment
    service_name       = var.service_name
    application_family = var.application_family
    terraformed        = true
  }, var.additional_tags)

  # Determine Route53 zone ID based on environment unless overridden
  route53_zone_id = var.override_route53_zone_id != null ? var.override_route53_zone_id : (var.environment == "dev" ? "Z21XEY26C989RH" : "Z0183645HDT0XCWHLW7S")
}

# IAM managed policy attachments for service role
variable "emr_service_role_iam_managed_policy_attachments" {
  description = "List of ARNs of existing IAM policies to attach to role"
  type        = list(string)
  default     = []
}

# IAM managed policy attachments for ec2 role
variable "emr_ec2_role_iam_managed_policy_attachments" {
  description = "List of ARNs of existing IAM policies to attach to role"
  type        = list(string)
  default     = []
}

locals {
  ec2_internal_hostname_regex      = "ip-(\\d{1,3})-(\\d{1,3})-(\\d{1,3})-(\\d{1,3}).ec2.internal"
  leader_node_hostname             = aws_emr_cluster.cluster.master_public_dns
  leader_node_hostname_is_internal = length(regexall(local.ec2_internal_hostname_regex, local.leader_node_hostname)) > 0 ? true : false
  leader_node_ip = local.leader_node_hostname_is_internal ? (
    join(".", regex(local.ec2_internal_hostname_regex, local.leader_node_hostname))
  ) : ""
}
