terraform {
  backend "s3" {
    bucket  = "qa-permissions-platform-terraform-state"
    key     = "qa/uat/auth0-m2m-config/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

provider "aws" {
  region = var.aws_region
}

data "aws_caller_identity" "current" {}

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

  environment = "qa"
}

module "aws_accounts" {
  source = "git@github.com:theorchard/terraform-aws-accounts-map.git//modules/read_accounts_map?ref=3.1.0"
}


locals {
  roles_to_assume = [for aws_account in var.supported_aws_accounts :
    "arn:aws:iam::${module.aws_accounts.all_accounts[aws_account]}:role/${var.environment}-assume-${var.service_name}"
  ]
}


# auth0-m2m-config-task-role needs to assume these roles in other AWS accounts
# in order to touch the AWS Secrets Manager secrets
data "aws_iam_policy_document" "auth0_m2m_config_assume_role_policy" {
  statement {
    effect  = "Allow"
    actions = ["sts:AssumeRole"]
    resources = concat(
      local.roles_to_assume
    )
  }
}

resource "aws_iam_policy" "auth0_m2m_config_assume_role_policy" {
  name   = "STS-${var.environment}-${var.service_name}-AssumeRole-policy"
  policy = data.aws_iam_policy_document.auth0_m2m_config_assume_role_policy.json

  tags = local.combined_resource_tags
}

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

  providers = {
    aws.dns = aws
  }

  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        = 0
  minimum_capacity          = 0
  maximum_capacity          = 0
  task_cpu                  = 1024
  task_memory               = 2048
  vpc_id                    = module.vpc_info.vpc_id
  fargate_service_subnets   = module.vpc_info.default_private_subnet_ids
  health_check_mode         = "CMD-SHELL"
  health_check_command_list = ["poetry", "run", "m2mconfig", "greetings", "bonjour", "hello"]
  iam_managed_policy_attachments = [
    resource.aws_iam_policy.auth0_m2m_config_assume_role_policy.arn,
  ]
  additional_task_role_principals = [
    "arn:aws:iam::437795906767:role/prod-jenkins-aws-pipeline-agent",
    "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/generic-engineer-role",
    "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/permissions-platform-role",
  ]
  worker_run_task_role_principals = [
    "arn:aws:iam::437795906767:role/prod-jenkins-aws-task-agent",
  ]

  environment_variables = [
    {
      AUTH0_DOMAIN = "qa-orchard.auth0.com"
    },
    {
      Environment = var.environment
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      DD_LOGS_INJECTION = "true"
    },
  ]

  secrets = [
    { AUTH0_M2M_CONFIG_CLIENT_CREDENTIALS = "${var.environment}/${var.service_name}/M2M_AUTH0_CLIENT_CREDENTIALS" },
    { SENTRY_DSN = "${var.environment}/${var.service_name}/SENTRY_DSN" }
  ]
}

module "secrets" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.4.0"
  for_each = toset(var.secrets_manager_secret_names)

  environment                    = var.environment
  application_family             = var.application_family
  service_name                   = var.service_name
  secret_name                    = each.value
  secret_recovery_window_in_days = 7
}


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

  environment        = var.environment
  platform           = "python"
  service_name       = var.service_name
  application_family = var.application_family
}

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

  environment                       = var.environment
  environment_type                  = "fargate"
  service_name                      = var.service_name
  service_4xx_monitor_enabled       = false
  service_5xx_monitor_enabled       = false
  healthy_tasks_monitor_enabled     = false
  application_family                = var.application_family
  notification_endpoints            = var.team_slack
  escalation_notification_endpoints = var.team_slack
  teams                             = [var.application_family]
}
