{% set config = merged_configs.get('azurerm_monitor_diagnostic_setting', {}) %}
{% set comments = compliance_comments.get('azurerm_monitor_diagnostic_setting', []) %}
# ------------------------------------------------------------------------------
# Azure Monitor Diagnostic Setting
# ------------------------------------------------------------------------------
{% for comment in comments %}
# {{ comment }}
{% endfor %}

resource "azurerm_monitor_diagnostic_setting" "this" {
  name                       = "{{ config.get('name', project_id ~ '-diag') }}"
  target_resource_id         = "{{ config.get('target_resource_id', '') }}"
  log_analytics_workspace_id = {{ config.get('log_analytics_workspace_id', 'azurerm_log_analytics_workspace.this.id') }}

  {% if config.get('enabled_logs', true) %}
  enabled_log {
    category = "{{ config.get('log_category', 'AuditEvent') }}"
  }
  {% endif %}

  {% if config.get('additional_log_categories') %}
  {% for category in config.additional_log_categories %}
  enabled_log {
    category = "{{ category }}"
  }
  {% endfor %}
  {% endif %}

  metric {
    category = "{{ config.get('metric_category', 'AllMetrics') }}"
  }
}
