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

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "shared-orcd-terraform-state"
    key     = "shared/datadog/software-catalog/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.81.0"
    }
  }
}

module "aws_accounts" {
  source = "git@github.com:theorchard/terraform-aws-accounts-map.git//modules/read_accounts_map?ref=3.1.0"
}

locals {
  datadog_secrets = [
    "${var.environment}/datadog/DD_API_KEY",
    "${var.environment}/datadog/DD_APP_KEY"
  ]
}

module "secrets" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each = toset(var.secrets_manager_secret_names)

  environment        = var.environment
  service_name       = var.service_name
  secret_name        = each.value
  application_family = var.application_family
}

module "secrets_manager_policy" {
  depends_on = [module.secrets]
  source     = "git@github.com:theorchard/terraform-iam-policy-templates.git//documents/secretsmanager/secrets_list?ref=0.4.0"

  secrets      = concat(local.datadog_secrets, [for secret in module.secrets : secret.secret_name])
  action_types = ["read"]
}

module "ecr_policy" {
  source = "git@github.com:theorchard/terraform-iam-policy-templates.git//documents/ecr/repository?ref=0.4.0"

  repositories = ["*"]
  action_types = ["read"]
}

data "aws_iam_policy_document" "resource_explorer_policy" {
  statement {
    actions = [
      "resource-explorer-2:Search",
    ]
    effect = "Allow"

    resources = [var.resource_explorer_view_arn]
  }
}

data "aws_iam_policy_document" "jenkins_assume_role_policy" {
  statement {
    actions = ["sts:AssumeRole"]
    effect  = "Allow"
    principals {
      type = "AWS"

      identifiers = [
        "arn:aws:iam::437795906767:role/prod-jenkins-aws-pipeline-agent",
        "arn:aws:iam::483193324480:role/dev-infra-jenkins_ec2_agent_role", # Agent role for the GDB Jenkins instance
      ]
    }
  }
}

resource "aws_iam_role" "software_catalog_role" {
  name               = "${var.environment}-${var.service_name}-role"
  assume_role_policy = data.aws_iam_policy_document.jenkins_assume_role_policy.json
}

resource "aws_iam_role_policy" "secrets_manager_policy" {
  name   = "SecretsManager-${var.environment}-${var.service_name}-RO"
  policy = module.secrets_manager_policy.policy.json
  role   = aws_iam_role.software_catalog_role.id
}

resource "aws_iam_role_policy" "ecr_policy" {
  name   = "ECR-${var.environment}-${var.service_name}-RO"
  policy = module.ecr_policy.policy.json
  role   = aws_iam_role.software_catalog_role.id
}

resource "aws_iam_role_policy" "resource_explorer_policy" {
  name   = "ResourceExplorer-${var.environment}-${var.service_name}-RO"
  policy = data.aws_iam_policy_document.resource_explorer_policy.json
  role   = aws_iam_role.software_catalog_role.id
}

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

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

  apply_server_side_encryption_by_default = {
    sse_algorithm = "AES256"
  }

  lifecycle_rules_options_noncurrent_version_expiration = [
    {
      prefix  = "/tmp/missing_services.csv"
      enabled = true
      days    = 28
    }
  ]
}
