
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

  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 backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "qa/bulkupload/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

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

  environment = var.environment
}

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

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

module "bulkupload_owsrequest" {
  source           = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.0.1"
  environment_name = var.environment
  service_name     = var.service_name
}

# Create efs file system for shared mounts
module "efs_volume" {
  source = "git@github.com:theorchard/terraform-efs.git//?ref=4.1.0"

  providers = {
    aws.dns = aws.networking
  }

  application_family       = var.application_family
  environment              = var.environment
  service_name             = var.service_name
  vpc_id                   = module.vpc_info.vpc_id
  subnet_ids               = module.vpc_info.default_private_subnet_ids
  owner_gid                = "48"
  owner_uid                = "48"
  access_point_permissions = "700"
}

# This rule will allow the fargate security group to acess the EFS filesystem
resource "aws_security_group_rule" "allow_efs_sg_inbound" {
  from_port                = 2049
  to_port                  = 2049
  protocol                 = "tcp"
  security_group_id        = module.efs_volume.efs_security_group_id_output
  source_security_group_id = module.bulkupload_fargate_environment.fargate_security_group_id
  type                     = "ingress"
}

module "bulkupload_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=5.6.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                           = "80"
  task_type                                = "web_service"
  desired_task_count                       = 2
  task_cpu                                 = 4096
  task_memory                              = 8192
  maximum_capacity                         = 4
  minimum_capacity                         = 2
  route53_zone_id                          = data.aws_route53_zone.route53_zone.id
  load_balancer_access_logs_s3_bucket_name = "orch-elb-logs"
  vpc_id                                   = module.vpc_info.vpc_id
  fargate_service_subnets                  = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets                    = module.vpc_info.default_private_subnet_ids
  https_listener_certificate_id            = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]
  health_check_path                        = "/healthchk.php"
  custom_waf_arn                           = module.custom_waf.waf_blocking_arn_output
  target_group_stickiness_enabled          = true
  fargate_spot_capacity_provider_weight    = 100
  task_placement_failure_alert_enabled     = true

  iam_managed_policy_attachments = [
    module.bulkupload_owsrequest.policy_arn_output,
    module.efs_volume.efs_iam_policy_arn_output
  ]

  docker_volumes = [
    {
      name            = "${var.environment}-${var.service_name}"
      file_system_id  = module.efs_volume.efs_file_system_id_output
      access_point_id = module.efs_volume.efs_access_point_id_output
    }
  ]

  docker_volume_mount_points = [
    {
      source_volume  = "${var.environment}-${var.service_name}"
      container_path = var.efs_volume_container_path
    }
  ]

  environment_variables = [
    {
      ENVIRONMENT = var.environment
    },
    {
      DD_ENV = var.environment
    },
    {
      DD_TRACE_DEBUG = "false"
    },
    {
      DD_SERVICE_NAME = var.service_name
    },
    {
      DD_LOGS_INJECTION = "true"
    },
    {
      DD_VERSION = var.service_version
    },
    {
      APPLICATION_ENV = var.environment
    },
    {
      LOG_LEVEL = var.log_level
    },
    {
      SENTRY_DSN = module.bulkupload_sentry_project.sentry_key_dsn_public_output
    },
    {
      EFS_VOLUME_CONTAINER_PATH = var.efs_volume_container_path
    },
  ]
}

module "custom_waf" {
  source            = "git@github.com:theorchard/terraform-aws-waf.git//?ref=2.0.1"
  environment       = var.environment
  service_name      = var.service_name
  aws_region        = var.aws_region
  count_waf_enabled = false
  block_waf_enabled = true
  excluded_rules = [
    "CrossSiteScripting_BODY",
    "SizeRestrictions_BODY",
    "GenericLFI_BODY",
  ]
}

# Sentry Project
module "bulkupload_sentry_project" {
  source = "git@github.com:theorchard/terraform-sentry.git?ref=4.1.2"

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


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

  application_family = var.application_family
  environment        = var.environment
  service_name       = var.service_name

  notification_endpoints            = var.datadog_notification_endpoints
  escalation_notification_endpoints = var.datadog_notification_endpoints
}
