variable "environment" {
  default = "qa"
}

variable "service_name" {
  default = "ows-terraform-fargate"
}

variable "aws_region" {
  default = "us-east-1"
}

variable "application_family" {
  default = "devops"
}

variable "team_name" {
  default = "devops"
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

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
  alias   = "networking"
  profile = "networking"

  default_tags {
    tags = module.default_tags_networking.tags
  }
}

module "default_tags_networking" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=2.0.0"
  environment        = "networking"
  application_family = var.application_family
  service_name       = var.service_name
  team_name          = var.team_name
}

data "aws_route53_zone" "route53_zone" {
  name = "theorchard.io"
}

module "vpc_info" {
  source = "git@github.com:theorchard/terraform-vpc-info.git?ref=1.0.0"

  environment = var.environment
}

module "qa_ows_service_environment" {
  source = "../../"

  providers = {
    aws.dns = aws.networking
  }

  environment                              = var.environment
  service_name                             = var.service_name
  application_family                       = "platform"
  aws_region                               = var.aws_region
  commit_sha                               = "latest"
  container_port                           = "8080"
  task_type                                = "web_service"
  desired_task_count                       = 2
  task_cpu                                 = 2048
  task_memory                              = 4096
  maximum_capacity                         = 4
  minimum_capacity                         = 2
  override_route53_zone_id                 = data.aws_route53_zone.route53_zone.zone_id
  load_balancer_access_logs_s3_bucket_name = "orch-elb-logs"
  vpc_id                                   = module.vpc_info.vpc_id
  https_listener_certificate_id            = "a76e4883-28aa-4062-a4cc-c06b8012ae28"

  fargate_service_subnets = [
    "subnet-073c5ca5affc267b5",
    "subnet-098b89d0c58c5693a",
  ]

  load_balancer_subnets = [
    "subnet-073c5ca5affc267b5",
    "subnet-098b89d0c58c5693a",
  ]

  environment_variables = [
    {
      Environment = "qa"
    },
    {
      LOGGER_DSN = "https://qa-fluentd-applications.theorchard.io:8888/application"
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
  ]
}

module "qa_ows_worker_environment" {
  source = "../../"

  providers = {
    aws.dns = aws.networking
  }

  environment        = var.environment
  service_name       = var.service_name
  application_family = "platform"
  aws_region         = var.aws_region
  commit_sha         = "latest"
  container_port     = "8080"
  task_type          = "worker"
  desired_task_count = 1
  task_cpu           = 2048
  task_memory        = 4096
  maximum_capacity   = 4
  minimum_capacity   = 1
  vpc_id             = "vpc-7f0e841a"

  fargate_service_subnets = [
    "subnet-073c5ca5affc267b5",
    "subnet-098b89d0c58c5693a",
  ]

  environment_variables = [
    {
      Environment = "qa"
    },
    {
      LOGGER_DSN = "https://qa-fluentd-applications.theorchard.io:8888/application"
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
  ]
}

module "fargate_task_triggered_by_cloudwatch_event_environment" {
  source = "../../"

  providers = {
    aws.dns = aws.networking
  }

  environment                       = var.environment
  service_name                      = var.service_name
  application_family                = "platform"
  autoscaling_cpu_policy_enabled    = false
  autoscaling_memory_policy_enabled = false
  aws_region                        = "us-east-1"
  commit_sha                        = "latest"
  container_port                    = "8080"
  task_type                         = "worker"
  desired_task_count                = 0
  task_cpu                          = 512
  task_memory                       = 1024
  maximum_capacity                  = 1
  minimum_capacity                  = 0
  vpc_id                            = "vpc-7f0e841a"
  cloudwatch_event_enabled          = true
  cloudwatch_event_schedule         = "rate(5 minutes)"

  fargate_service_subnets = [
    "subnet-073c5ca5affc267b5",
    "subnet-098b89d0c58c5693a",
  ]

  environment_variables = [
    {
      Environment = "${var.service_name}"
    },
    {
      LOGGER_DSN = "https://${var.service_name}-fluentd-applications.theorchard.io:8888/application"
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
  ]
}
