# General settings
variable "environment" {
  description = "Name of the environment, e.g. dev, qa, prod"
  default     = "dev"
}

variable "service_name" {
  description = "AWS stack name"
  default     = "fluentd"
}

# Network and subnet settings
variable "vpc_id" {
  description = "VPC ID in which resources will be provisioned"
  default     = "vpc-34dbfd51"
}

variable "instance_subnet_ids" {
  description = "Subnets in which to run instances"
  type        = list(string)

  default = [
    "subnet-44c19a21",
    "subnet-b649dfef",
  ]
}

# SSH settings
variable "ssh_keypair_name" {
  description = "Name of existing AWS SSH keypair"
  default     = "dev_orchard_admin"
}

variable "ssh_port" {
  type        = number
  description = "SSH port"
  default     = 22
}

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

  default = [
    "10.10.141.0/29",
    "10.140.0.0/29",
    "10.30.0.0/22",
    "192.168.32.0/24",
    "192.168.33.0/24",
    "192.168.40.0/22",
  ]
}

# IAM settings
variable "iam_managed_policy_attachments" {
  description = "List of ARNs of existing IAM policies to attach to role"
  type        = list(string)
  default     = []
}

variable "iam_policy_file_enabled" {
  description = "Does this function require additional IAM policies? If so set this to true and add a file in ./policies/var.service_name.json"
  default     = false
}

# Autoscaling group settings
variable "aws_instance_type" {
  description = "Fluentd instance type"
  default     = "t3.small"
}

variable "root_volume_size" {
  description = "Root EBS volume size"
  default     = "80"
}

variable "user_data" {
  description = "User data script"
  default     = "echo 'hello'"
}

variable "min_size" {
  type        = number
  description = "Minimum size for autoscaling group"
  default     = 3
}

variable "max_size" {
  type        = number
  description = "Maximum size for autoscaling group"
  default     = 8
}

variable "desired_capacity" {
  type        = number
  description = "Desired capacity for autoscaling group. Should typically match minimum"
  default     = 3
}

variable "default_cooldown" {
  type        = number
  description = "Default cooldown period between scaling activities in seconds"
  default     = 300
}

variable "health_check_grace_period" {
  type        = number
  description = "Grace period before health checks begin, in seconds"
  default     = 600
}

variable "wait_for_capacity_timeout" {
  type        = string
  description = "Grace period before health checks begin, in number + unit format, e.g. 10m"
  default     = "10m"
}

variable "enabled_metrics" {
  description = "Enabled metrics for ASG"
  type        = list(string)
  default = [
    "GroupMinSize",
    "GroupMaxSize",
    "GroupDesiredCapacity",
    "GroupInServiceInstances",
    "GroupPendingInstances",
    "GroupStandbyInstances",
    "GroupTerminatingInstances",
    "GroupTotalInstances",
  ]
}

# Health check settings
variable "target_group_health_check_enabled" {
  description = "Whether or not target group health checks are enabled"
  default     = true
}

variable "health_check_healthy_threshold" {
  description = "Number of consecutive positive health checks before a target is considered healthy"
  default     = "3"
}

variable "health_check_interval" {
  description = "Intervals between health checks of target"
  default     = "10"
}

variable "health_check_unhealthy_threshold" {
  description = "Number of consecutive positive health checks before a target is considered unhealthy."
  default     = "3"
}

# Load balancer settings
variable "load_balancer_enable_deletion_protection" {
  description = "Whether or not to enable deletion protection via API. Stops Terraform from deleting this resource."
  default     = false
}

variable "load_balancer_is_internal" {
  description = "Boolean to indicate if load balancer is internal"
  default     = true
}

variable "load_balancer_access_logs_s3_bucket_name" {
  description = "S3 bucket for storing load balancer access logs"
  default     = "dev-orcd-lb-logs"
}

variable "load_balancer_subnets" {
  description = "Subnets (in multiple AZs) for load balancer"
  type        = list(string)

  default = [
    "subnet-44c19a21",
    "subnet-b649dfef",
  ]
}

variable "https_listener_port" {
  description = "Port for HTTPS listener."
  default     = "8888"
}

variable "https_listener_ssl_policy" {
  description = "HTTPS listener SSL policy"
  default     = "ELBSecurityPolicy-TLS-1-2-2017-01"
}

variable "http_plugin_enabled" {
  type        = bool
  description = "Whether or not HTTP plugin is running in fluentd cluster"
  default     = true
}

variable "udp_plugin_enabled" {
  type        = bool
  description = "Whether or not UDP plugin is running in fluentd cluster"
  default     = true
}

variable "tcp_8888_allow_cidr_blocks" {
  type        = list(string)
  description = "List of CIDR blocks from which to accept traffic on TCP 8888"
  default     = []
}

variable "tcp_24224_allow_cidr_blocks" {
  type        = list(string)
  description = "List of CIDR blocks from which to accept traffic on TCP 24224"
  default     = []
}

variable "udp_allow_cidr_blocks" {
  type        = list(string)
  description = "List of CIDR blocks from which to accept traffic on UDP 5160"
  default     = []
}

variable "target_deregistration_delay" {
  description = "Deregistration delay for target group tasks"
  default     = "60"
}

variable "acm_domain" {
  description = "Name of domain used to locate SSL certificate"
  default     = "*.dev.theorchard.io"
}

# DNS settings
variable "aws_route53_params" {
  description = "AWS route 53 parameters"

  default = {
    domain_name = "dev.theorchard.io"
    zone_id     = "Z21XEY26C989RH"
    type        = "CNAME"
    ttl         = "300"
  }
}

# Locals
locals {
  subnet_ids_string = join(",", data.aws_subnet_ids.private_subnet_ids.ids)
  subnet_ids_list   = split(",", local.subnet_ids_string)
  target_group_arns = var.http_plugin_enabled ? var.udp_plugin_enabled ? [aws_lb_target_group.http_8888_target_group[0].arn, aws_lb_target_group.tcp_24224_target_group.arn, aws_lb_target_group.udp_target_group[0].arn] : [aws_lb_target_group.http_8888_target_group[0].arn, aws_lb_target_group.tcp_24224_target_group.arn] : var.udp_plugin_enabled ? [aws_lb_target_group.tcp_24224_target_group.arn, aws_lb_target_group.udp_target_group[0].arn] : [aws_lb_target_group.tcp_24224_target_group.arn]
}
