terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "qa/testerloop/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

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
  }
}

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

  environment = var.environment
}

data "aws_acm_certificate" "theorchard_io" {
  domain   = "*.theorchard.io"
  statuses = ["ISSUED"]
}

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

data "aws_route53_zone" "networking_route53_zone" {
  provider = aws.networking

  name = "theorchard.io"
}

data "aws_s3_bucket" "s3_bucket" {
  bucket = var.config_s3_bucket
}

data "aws_iam_policy_document" "s3_bucket_rw_policy_document" {
  statement {
    effect = "Allow"
    actions = [
      "s3:GetBucketLocation",
      "s3:ListBucket",
    ]
    resources = [
      data.aws_s3_bucket.s3_bucket.arn,
    ]
  }

  statement {
    effect = "Allow"
    actions = [
      "s3:GetObject",
      "s3:PutObject",
    ]
    resources = [
      "${data.aws_s3_bucket.s3_bucket.arn}/testerloop-results/*",
      "${data.aws_s3_bucket.s3_bucket.arn}/job_status/*",
    ]
  }
}

# Create IAM group testerloop.
resource "aws_iam_group" "testerloop_iam_group" {
  name = "testerloop"
}

# Reuse IAM cucumber-tests user.
resource "aws_iam_group_membership" "testerloop_iam_group_membership" {
  name = "testerloop_iam_group_membership"
  users = [
    "cucumber-tests",
  ]

  group = aws_iam_group.testerloop_iam_group.name
}

resource "aws_iam_policy" "s3_bucket_rw_policy" {
  name   = "S3-${var.environment}-${var.service_name}-RW-policy"
  policy = data.aws_iam_policy_document.s3_bucket_rw_policy_document.json
  tags   = local.tags
}

resource "aws_iam_group_policy_attachment" "s3_bucket_rw_policy_attachment" {
  group      = aws_iam_group.testerloop_iam_group.name
  policy_arn = aws_iam_policy.s3_bucket_rw_policy.arn
}

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

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

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

  providers = {
    aws.dns = aws.networking
  }

  environment                              = var.environment
  service_name                             = var.service_name
  aws_region                               = var.aws_region
  application_family                       = var.application_family
  commit_sha                               = "latest"
  container_port                           = "8080"
  task_type                                = "web_service"
  desired_task_count                       = 1
  task_cpu                                 = 2048
  task_memory                              = 4096
  maximum_capacity                         = 2
  minimum_capacity                         = 1
  health_check_path                        = "/"
  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            = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]

  # overriding here to add in the private subnets
  qa_https_listener_allow_cidr_blocks = [
    "192.168.31.0/24",
    "192.168.32.0/24",
    "192.168.33.0/24",
    "192.168.40.0/22",
    "10.40.0.0/22",
    "10.30.0.0/22",
    "10.10.40.0/22",
  ]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  load_balancer_subnets = module.vpc_info.default_private_subnet_ids

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      PORT = var.testerloop_port
    },
    {
      AWS_BUCKET_NAME = var.config_s3_bucket
    },
    {
      AWS_BUCKET_PATH = var.config_s3_path
    },
    {
      AWS_BUCKET_REGION = var.aws_region
    },
    {
      EXPIRES_IN = var.testerloop_page_expiration
    }
  ]

  iam_managed_policy_attachments = [
    aws_iam_policy.s3_bucket_rw_policy.arn,
  ]
}

resource "aws_route53_record" "testerloop-cname" {
  zone_id = data.aws_route53_zone.route53_zone.zone_id
  name    = "testerloop.theorchard.io"
  type    = "CNAME"
  ttl     = "60"
  records = [
    module.testerloop_fargate_environment.non_prod_fargate_service_route53_record_fqdn_output,
  ]
}

resource "aws_route53_record" "networking_testerloop-cname" {
  provider = aws.networking
  zone_id  = data.aws_route53_zone.networking_route53_zone.zone_id
  name     = "testerloop.theorchard.io"
  type     = "CNAME"
  ttl      = "60"
  records = [
    module.testerloop_fargate_environment.non_prod_fargate_service_route53_record_fqdn_output,
  ]
}

module "fargate_service_testerloop_dashboard" {
  source                            = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.10.6"
  environment                       = var.environment
  application_family                = var.application_family
  environment_type                  = "fargate"
  service_name                      = var.service_name
  notification_endpoints            = "@slack-testerloop-poc"
  escalation_notification_endpoints = "@slack-testerloop-poc"
  service_4xx_monitor_enabled       = true
  service_5xx_monitor_enabled       = true
}
