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 {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/neo4j-cypher-scheduler/delete-liquigraphlock/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
}

# Terraform backends cannot contain interpolations
module "neo4j_cypher_scheduler_delete_liquigraphlock_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=5.6.1"

  providers = {
    aws.dns = aws
  }

  environment             = var.environment
  service_name            = "${var.service_name}-${var.neo4j_source_folder_name}"
  application_family      = var.application_family
  aws_region              = var.aws_region
  commit_sha              = "latest"
  container_port          = "8080"
  task_type               = "worker"
  desired_task_count      = 0
  task_cpu                = 512
  task_memory             = 1024
  maximum_capacity        = 1
  minimum_capacity        = 0
  vpc_id                  = module.vpc_info.vpc_id
  fargate_service_subnets = module.vpc_info.default_private_subnet_ids
  health_check_command    = "pgrep python"
  splitio_enabled         = false

  iam_managed_policy_attachments = [
    "arn:aws:iam::${data.aws_caller_identity.current.account_id}:policy/SecretsManager-${var.environment}-neo4j-cypher-scheduler-policy",
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      NEO4J_SOURCE_FOLDER_NAME = var.neo4j_source_folder_name
    },
    {
      NEO4J_URL = "neo4j+s://prod-neo4j-cluster.theorchard.io:7687"
    },
    {
      LOGGER_DSN = "https://${var.environment}-fluentd-applications.theorchard.io:8888/application"
    },
  ]
}

data "aws_iam_policy_document" "task_run_role_assume_role_policy" {
  statement {
    actions = ["sts:AssumeRole"]

    principals {
      type = "AWS"

      identifiers = [
        "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/prod-jenkins-aws-pipeline-agent",
      ]
    }
  }
}

resource "aws_iam_role" "task_run_role" {
  name               = "${var.environment}-${var.service_name}-${var.neo4j_source_folder_name}-run-role"
  assume_role_policy = data.aws_iam_policy_document.task_run_role_assume_role_policy.json
}

data "aws_iam_policy_document" "task_run_role_policy" {
  statement {
    effect = "Allow"
    actions = [
      "ec2:DescribeVpcs",
      "ec2:DescribeSubnets",
      "ec2:DescribeSecurityGroups",
    ]
    resources = ["*"]
  }

  statement {
    effect = "Allow"
    actions = [
      "ecs:DescribeTasks",
    ]
    resources = ["*"]
    condition {
      test     = "ArnEquals"
      variable = "ecs:cluster"
      values   = [module.neo4j_cypher_scheduler_delete_liquigraphlock_fargate_environment.fargate_cluster_arn]
    }
  }

  statement {
    effect = "Allow"
    actions = [
      "ecs:RunTask",
    ]
    resources = [
      "${module.neo4j_cypher_scheduler_delete_liquigraphlock_fargate_environment.fargate_task_definition_arn_without_revision}:*"
    ]
  }

  statement {
    effect = "Allow"
    actions = [
      "ecs:TagResource",
    ]
    resources = ["*"]
    condition {
      test     = "StringEquals"
      variable = "ecs:CreateAction"
      values   = ["RunTask"]
    }
  }

  statement {
    effect = "Allow"
    actions = [
      "iam:PassRole"
    ]
    resources = [
      module.neo4j_cypher_scheduler_delete_liquigraphlock_fargate_environment.fargate_task_iam_role_arn,
      module.neo4j_cypher_scheduler_delete_liquigraphlock_fargate_environment.fargate_task_iam_execution_role_arn,
    ]
  }
}

resource "aws_iam_role_policy" "task_run_role_policy" {
  role   = aws_iam_role.task_run_role.id
  policy = data.aws_iam_policy_document.task_run_role_policy.json
}
