locals {
  terraform_repo           = "terraform-infra"
  terraform_pr_workspace   = try(regex("/[0-9]*/${terraform.workspace}", abspath(path.module)), "")
  terraform_relative_path  = "${local.terraform_repo}${reverse(split(local.terraform_repo, abspath(path.module)))[0]}"
  terraform_formatted_path = replace(local.terraform_relative_path, local.terraform_pr_workspace, "")
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = {
      "terraform_config" = local.terraform_formatted_path
    }
  }
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/orcd-ftp/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

module "ftp_bucket" {
  source = "git@github.com:theorchard/terraform-s3.git//modules/s3_bucket?ref=3.15.7"

  env                = var.environment
  bucket_name        = var.service_name
  application_family = var.application_family

  apply_server_side_encryption_by_default = {
    sse_algorithm = "AES256"
  }

  enable_guardduty_malware_protection = true

  block_public_access = false

  custom_object_ownership = "ObjectWriter"

  lifecycle_rules_options_noncurrent_version_transition = [
    {
      prefix        = ""
      enabled       = true
      days          = 30
      storage_class = "GLACIER"
    }
  ]

  lifecycle_rules_options_current_version_expiration = [
    {
      prefix  = "ftp/BandsInTown/"
      enabled = true
      days    = 14
    },
  ]

  bucket_policy_overrides = [data.aws_iam_policy_document.ftp_bucket_resource_policy.json]
}

module "datadog" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/s3?ref=6.16.2"

  environment                       = var.environment
  service_name                      = var.service_name
  application_family                = var.application_family
  teams                             = ["devops"]
  notification_endpoints            = "@slack-devops-internal-alerts"
  escalation_notification_endpoints = "@slack-devops-internal-alerts"

  dashboard_configuration = {
    enabled                       = false
    show_advanced_storage_metrics = false
    show_replication_metrics      = false
    show_activity_metrics         = false
  }
  guardduty_malware_monitor_configuration = {
    enabled = true
  }
}

data "aws_iam_policy_document" "ftp_bucket_resource_policy" {
  statement {
    sid    = "DelphiReadRoles"
    effect = "Allow"
    actions = sort([
      "s3:GetObject",
      "s3:GetObjectTagging",
      "s3:GetObjectVersion",
      "s3:GetObjectVersionTagging"
    ])
    resources = formatlist("${module.ftp_bucket.s3_bucket_arn_output}/%s*", var.delphi_read_prefixes)
    principals {
      type        = "AWS"
      identifiers = sort(var.delphi_read_roles)
    }
  }
  statement {
    sid       = "DelphiReadRolesList"
    effect    = "Allow"
    actions   = ["s3:ListBucket"]
    resources = [module.ftp_bucket.s3_bucket_arn_output]
    principals {
      type        = "AWS"
      identifiers = sort(var.delphi_read_roles)
    }
    condition {
      test     = "StringLike"
      variable = "s3:prefix"
      values   = var.delphi_read_prefixes
    }
  }
}
