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

resource "azurerm_cdn_frontdoor_profile" "this" {
  name                = "{{ config.get('name', project_id ~ '-fd') }}"
  resource_group_name = var.resource_group_name
  sku_name            = "{{ config.get('sku_name', 'Premium_AzureFrontDoor') }}"

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

resource "azurerm_cdn_frontdoor_endpoint" "this" {
  name                     = "{{ config.get('endpoint_name', project_id ~ '-fd-ep') }}"
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.this.id
  enabled                  = true

  tags = {
    Name      = "{{ config.get('endpoint_name', project_id ~ '-fd-ep') }}"
    ManagedBy = "complyform"
  }
}

{% if config.get('origin_group') %}
resource "azurerm_cdn_frontdoor_origin_group" "this" {
  name                     = "{{ project_id }}-og"
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.this.id

  load_balancing {
    sample_size                 = {{ config.get('lb_sample_size', 4) }}
    successful_samples_required = {{ config.get('lb_successful_samples', 3) }}
  }

  health_probe {
    path                = "{{ config.get('health_probe_path', '/') }}"
    protocol            = "Https"
    interval_in_seconds = {{ config.get('health_probe_interval', 30) }}
  }
}
{% endif %}
