module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=2.1.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  = "shared-orcd-terraform-state"
    key     = "shared/ows-pypi-server/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_route53_zone" "route53_zone" {
  name = "${var.environment}.theorchard.io."
}

data "aws_route53_zone" "route53_zone_networking" {
  provider = aws.networking
  name     = "theorchard.io."
}

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

resource "aws_lb_listener_certificate" "additional_certificate" {
  listener_arn    = module.ows_pypi_server_fargate_environment.fargate_load_balancer_https_listener_arn
  certificate_arn = data.aws_acm_certificate.theorchard_io_certificate.arn
}

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

  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                = 1024
  task_memory             = 4096
  maximum_capacity        = 4
  minimum_capacity        = 2
  health_check_path       = "/"
  vpc_id                  = module.vpc_info.vpc_id
  custom_waf_arn          = module.custom_waf.waf_blocking_arn_output
  fargate_service_subnets = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets   = module.vpc_info.default_private_subnet_ids

  iam_managed_policy_attachments = [
    aws_iam_policy.s3_read_write_policy.arn,
  ]

  # Switchboard does not yet have managed prefix lists
  prod_https_listener_allow_cidr_blocks = [
    "10.110.0.0/16",
    "10.120.0.0/16",
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      PPC_DB_URL = "redis://${module.ows_pypi_server_elasticache.redis_cluster_cname_output[0]}:6379"
    },
    {
      PPC_STORAGE_BUCKET = module.s3_bucket.s3_bucket_name_output
    }
  ]
}

# Get the ec2 prefix lists to allow access
data "aws_ec2_managed_prefix_list" "prefix_lists" {
  for_each = var.prefix_lists_to_allow_ingress_to_lb
  name     = each.value
}

# Allow access from prefix lists to LB
resource "aws_security_group_rule" "allow_inbound_access_to_lb" {
  type              = "ingress"
  from_port         = 443
  to_port           = 443
  protocol          = "TCP"
  prefix_list_ids   = [for list in data.aws_ec2_managed_prefix_list.prefix_lists : list.id]
  security_group_id = module.ows_pypi_server_fargate_environment.fargate_load_balancer_security_group_id
}

module "custom_waf" {
  source            = "git@github.com:theorchard/terraform-aws-waf.git//?ref=2.0.2"
  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",
    "EC2MetaDataSSRF_BODY",
  ]
}

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

  environment        = var.environment
  environment_type   = "fargate"
  service_name       = var.service_name
  application_family = var.application_family

  notification_endpoints            = "@slack-devops"
  escalation_notification_endpoints = "@slack-devops"
}

module "ows_pypi_server_elasticache" {
  # checkov:skip=CKV_AWS_30:pypicloud does not support transit encryption.
  # checkov:skip=CKV_AWS_31:pypicloud does not support transit encryption.

  source = "git@github.com:theorchard/terraform-elasticache.git//?ref=4.0.0"

  providers = {
    aws.dns = aws.networking
  }

  environment                      = var.environment
  service_name                     = var.service_name
  application_family               = var.application_family
  cache_engine                     = "redis"
  cache_subnet_group_name          = "${var.environment}-elasticache-subnet-group"
  cache_node_type                  = "cache.t4g.micro"
  cache_node_count                 = 2
  cache_parameter_group_name       = "default.redis7"
  redis_engine_version             = "7.1"
  vpc_id                           = module.vpc_info.vpc_id
  override_route53_zone_id         = data.aws_route53_zone.route53_zone.zone_id
  cache_transit_encryption_enabled = false
}

resource "aws_route53_record" "theorchard_io_cname_record" {
  provider = aws.networking

  name    = "pypi.theorchard.io"
  type    = "CNAME"
  ttl     = 300
  zone_id = data.aws_route53_zone.route53_zone_networking.zone_id
  records = [module.ows_pypi_server_fargate_environment.non_prod_fargate_service_route53_record_fqdn_output]
}
