module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=1.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_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 {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "qa/examples/worker/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

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

  environment = var.environment
}

module "worker_service_sentry_project" {
  source = "git@github.com:theorchard/terraform-sentry.git//?ref=5.0.0"

  environment        = var.environment
  platform           = "python"
  service_name       = var.service_name
  application_family = var.application_family
  teams              = [var.environment]
}

module "worker_service_owsrequest" {
  source = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.1.0"

  environment_name = var.environment
  service_name     = var.service_name
}

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

  providers = {
    aws.dns = aws.networking
  }

  application_family      = var.application_family
  environment             = var.environment
  service_name            = var.service_name
  aws_region              = var.aws_region
  task_type               = "worker"
  container_port          = "9000"
  desired_task_count      = 2
  minimum_capacity        = 2
  maximum_capacity        = 4
  task_cpu                = 512
  task_memory             = 1024
  vpc_id                  = module.vpc_info.vpc_id
  fargate_service_subnets = module.vpc_info.default_private_subnet_ids
  iam_managed_policy_attachments = [
    module.worker_service_owsrequest.policy_arn_output
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      SENTRY_DSN  = module.worker_service_sentry_project.sentry_key_dsn_public_output,
    },
  ]
}

module "worker_service_fargate_service_dashboard" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.13.7"

  environment                       = var.environment
  environment_type                  = "fargate"
  service_name                      = var.service_name
  service_4xx_monitor_enabled       = false
  service_5xx_monitor_enabled       = false
  application_family                = var.application_family
  notification_endpoints            = "@slack-permissions-platform"
  escalation_notification_endpoints = "@slack-permissions-platform"
}
