{% set config = merged_configs.get('aws_glue_job', {}) %}
{% set comments = compliance_comments.get('aws_glue_job', []) %}
# ------------------------------------------------------------------------------
# Glue Job
# ------------------------------------------------------------------------------
{% for comment in comments %}
# {{ comment }}
{% endfor %}

resource "aws_glue_job" "this" {
  name     = "{{ config.get('name', project_id ~ '-glue-job') }}"
  role_arn = {{ config.get('role_arn', 'aws_iam_role.glue_exec.arn') }}

  glue_version      = "{{ config.get('glue_version', '4.0') }}"
  worker_type       = "{{ config.get('worker_type', 'G.1X') }}"
  number_of_workers = {{ config.get('number_of_workers', 2) }}
  max_retries       = {{ config.get('max_retries', 0) }}
  timeout           = {{ config.get('timeout', 2880) }}

  command {
    script_location = "{{ config.get('script_location', 's3://' ~ project_id ~ '-scripts/glue/job.py') }}"
    python_version  = "{{ config.get('python_version', '3') }}"
    name            = "{{ config.get('command_name', 'glueetl') }}"
  }

  # Security
  security_configuration = {{ config.get('security_configuration', 'null') }}

  default_arguments = {
    "--job-language"                     = "{{ config.get('job_language', 'python') }}"
    "--enable-continuous-cloudwatch-log" = "{{ config.get('enable_cw_log', 'true') }}"
    "--enable-metrics"                   = "{{ config.get('enable_metrics', 'true') }}"
    "--enable-spark-ui"                  = "{{ config.get('enable_spark_ui', 'true') }}"
    "--spark-event-logs-path"            = "{{ config.get('spark_event_logs_path', 's3://' ~ project_id ~ '-scripts/sparkui/') }}"
  }

  tags = {
    Name      = "{{ config.get('name', project_id ~ '-glue-job') }}"
    ManagedBy = "complyform"
  }
}

resource "aws_iam_role" "glue_exec" {
  name = "{{ config.get('name', project_id ~ '-glue-job') }}-exec"

  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Action = "sts:AssumeRole"
        Effect = "Allow"
        Principal = {
          Service = "glue.amazonaws.com"
        }
      }
    ]
  })

  tags = {
    Name      = "{{ project_id }}-glue-exec"
    ManagedBy = "complyform"
  }
}

resource "aws_iam_role_policy_attachment" "glue_service" {
  role       = aws_iam_role.glue_exec.name
  policy_arn = "arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole"
}
