{% set config = merged_configs.get('azurerm_servicebus_namespace', {}) %}
{% set comments = compliance_comments.get('azurerm_servicebus_namespace', []) %}
# ------------------------------------------------------------------------------
# Azure Service Bus
# ------------------------------------------------------------------------------
{% for comment in comments %}
# {{ comment }}
{% endfor %}

resource "azurerm_servicebus_namespace" "this" {
  name                = "{{ config.get('name', project_id ~ '-sb') }}"
  resource_group_name = var.resource_group_name
  location            = var.region

  sku      = "{{ config.get('sku', 'Premium') }}"
  capacity = {{ config.get('capacity', 1) }}

  minimum_tls_version = "{{ config.get('minimum_tls_version', '1.2') }}"

  public_network_access_enabled = {{ config.get('public_network_access_enabled', false) | lower }}

  local_auth_enabled = {{ config.get('local_auth_enabled', false) | lower }}

  {% if config.get('customer_managed_key') %}
  customer_managed_key {
    key_vault_key_id = "{{ config.customer_managed_key.key_vault_key_id }}"
    identity_id      = "{{ config.customer_managed_key.identity_id }}"
  }
  {% endif %}

  identity {
    type = "{{ config.get('identity_type', 'SystemAssigned') }}"
  }

  {% if config.get('network_rule_set') %}
  network_rule_set {
    default_action                = "{{ config.network_rule_set.get('default_action', 'Deny') }}"
    trusted_services_allowed      = {{ config.network_rule_set.get('trusted_services_allowed', true) | lower }}
    public_network_access_enabled = false
  }
  {% endif %}

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