locals {
  find_objects_app_name = "${var.service_name}-find-objects"
  find_objects_app_dir  = "apps/fargate/find-objects"
  find_objects_app_hash = md5(join(",", [
    filemd5("${local.find_objects_app_dir}/Dockerfile"),
    filemd5("${local.find_objects_app_dir}/requirements.txt"),
    filemd5("${local.find_objects_app_dir}/find-objects.py"),
  ]))
}

resource "aws_ecr_repository" "find_objects_app_repository" {
  name                 = local.find_objects_app_name
  image_tag_mutability = "MUTABLE"

  encryption_configuration {
    encryption_type = "KMS"
  }

  image_scanning_configuration {
    scan_on_push = true
  }
}

resource "null_resource" "find_objects_app_build" {
  triggers = {
    src_hash = local.find_objects_app_hash
  }

  provisioner "local-exec" {
    command = "/bin/bash ./scripts/build-docker.sh"

    environment = {
      SRC_DIR             = local.find_objects_app_dir
      ECR_REPOSITORY_NAME = aws_ecr_repository.find_objects_app_repository.name
      ECR_REPOSITORY_TAG  = local.find_objects_app_hash
      ECR_REPOSITORY_URL  = aws_ecr_repository.find_objects_app_repository.repository_url
    }
  }
}

resource "aws_iam_policy" "find_objects_app_policy" {
  name   = "${var.environment}-${local.find_objects_app_name}-policy"
  policy = data.aws_iam_policy_document.find_objects_app_policy.json
}

data "aws_iam_policy_document" "find_objects_app_policy" {
  statement {
    effect    = "Allow"
    actions   = ["s3:ListBucketVersions"]
    resources = [data.aws_s3_bucket.bucket.arn]
  }

  statement {
    effect    = "Allow"
    actions   = ["sqs:SendMessage"]
    resources = [module.object_queue.queue_arn]
  }

  statement {
    effect    = "Allow"
    actions   = ["cloudwatch:PutMetricData"]
    resources = ["*"]
    condition {
      test     = "StringEquals"
      variable = "cloudwatch:namespace"
      values   = [var.service_name]
    }
  }
}

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

  environment        = var.environment
  service_name       = local.find_objects_app_name
  aws_region         = var.aws_region
  application_family = var.application_family

  task_type = "worker"

  vpc_id                  = var.vpc_id
  fargate_service_subnets = var.vpc_subnet_ids

  task_cpu             = 256
  task_memory          = 512
  health_check_command = "true"

  desired_task_count = 0
  minimum_capacity   = 0
  maximum_capacity   = 1

  autoscaling_cpu_policy_enabled           = false
  autoscaling_memory_policy_enabled        = false
  autoscaling_load_balancer_policy_enabled = false

  environment_variables = [
    { BUILD_ID = null_resource.find_objects_app_build.id },

    { BUCKET_NAME = var.bucket_name },
    { OBJECT_PREFIX = var.object_prefix },
    { OBJECT_FILTER = var.object_filter },

    { SQS_QUEUE_URL = module.object_queue.queue_url },
    { SQS_BATCH_SIZE = var.sqs_batch_size },
    { SQS_MESSAGE_SIZE = var.sqs_message_size },

    { CLOUDWATCH_NAMESPACE = var.service_name },

    { LOG_LEVEL = var.log_level },
  ]

  iam_managed_policy_attachments = [
    aws_iam_policy.find_objects_app_policy.arn,
  ]
}
