provider "aws" {
  region = var.aws_region
}

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

data "aws_caller_identity" "current" {}

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

  env                 = var.environment
  bucket_name         = var.custom_bucket_name
  custom_bucket_name  = var.custom_bucket_name
  application_family  = var.application_family
  s3_read_only_policy = false
  block_public_access = true
  acceleration_status = "Enabled"
  bucket_policy_overrides = [
    data.aws_iam_policy_document.bucket_policy_document.json
  ]

  # CORS Rules
  bucket_cors_rule = [
    {
      allowed_headers = ["*"]
      allowed_methods = ["PUT", "POST"]
      allowed_origins = [
        "https://oa.theorchard.com",
      ]
      expose_headers  = ["ETag"]
      max_age_seconds = 3000
    }
  ]

  apply_server_side_encryption_by_default = {
    sse_algorithm = "AES256"
  }

  # GIR is billed for a minimum of 90 days, so the Glacier transition should be 90 days after the GIR transition
  lifecycle_rules_options_current_version_transition = [
    {
      prefix        = ""
      enabled       = true
      days          = 15
      storage_class = "GLACIER_IR"
    },
    {
      prefix        = ""
      enabled       = true
      days          = 105
      storage_class = "GLACIER"
    },
  ]

  lifecycle_rules_options_noncurrent_version_transition = [
    {
      prefix        = ""
      enabled       = true
      days          = 0
      storage_class = "GLACIER_IR"
    },
    {
      prefix        = ""
      enabled       = true
      days          = 90
      storage_class = "GLACIER"
    },
  ]

  lifecycle_rules_options_noncurrent_version_expiration = [
    {
      prefix  = "bulk-physical-pricing-ingest-archive/"
      enabled = true
      days    = 90
    },
    {
      prefix  = "sme_labelcopy_ingest/source"
      enabled = true
      days    = 60
    }
  ]

  lifecycle_rules_options_current_version_expiration = [
    {
      prefix        = "temp-feed-ingestion/"
      enabled       = true
      days          = 14
    }
  ]
}

data "aws_iam_policy_document" "bucket_policy_document" {
  statement {
    sid = "AmazonML_s3:ListBucket"

    actions = [
      "s3:ListBucket"
    ]

    resources = [
      module.s3_bucket.s3_bucket_arn_output
    ]

    condition {
      test     = "StringLike"
      variable = "s3:prefix"
      values = [
        "Youtube/archives/20150504/YouTube_theorchardmusic_W_20150504_20150510_rawdata.csv.gz*"
      ]
    }

    principals {
      type = "Service"

      identifiers = [
        "machinelearning.amazonaws.com"
      ]
    }
  }
  statement {
    sid    = "AmazonML_s3:GetObject"
    effect = "Allow"
    actions = [
      "s3:GetObject"
    ]

    resources = [
      "${module.s3_bucket.s3_bucket_arn_output}/Youtube/archives/20150504/YouTube_theorchardmusic_W_20150504_20150510_rawdata.csv.gz*"
    ]

    principals {
      type = "Service"

      identifiers = [
        "machinelearning.amazonaws.com"
      ]
    }
  }
}
