{% extends 'common/base_resource.tf.j2' %}
{% import 'common/macros.j2' as macros %}
{% block resources %}
{% for cluster in resources %}
{{ resource_spacer() }}
resource "azurerm_kubernetes_cluster" "{{ cluster.name_sanitized }}" {
  name                = "{{ cluster.name }}"
  location            = "{{ cluster.location }}"
  resource_group_name = "{{ cluster.resource_group_name }}"
  dns_prefix          = "{{ cluster.dns_prefix }}"
  
  {% if cluster.kubernetes_version %}
  kubernetes_version = "{{ cluster.kubernetes_version }}"
  {% endif %}
  
  {% if cluster.automatic_channel_upgrade %}
  automatic_channel_upgrade = "{{ cluster.automatic_channel_upgrade }}"
  {% endif %}
  
  {% if cluster.azure_policy_enabled is defined %}
  azure_policy_enabled = {{ cluster.azure_policy_enabled | lower }}
  {% endif %}
  
  {% if cluster.disk_encryption_set_id %}
  disk_encryption_set_id = "{{ cluster.disk_encryption_set_id }}"
  {% endif %}
  
  {% if cluster.edge_zone %}
  edge_zone = "{{ cluster.edge_zone }}"
  {% endif %}
  
  {% if cluster.http_application_routing_enabled is defined %}
  http_application_routing_enabled = {{ cluster.http_application_routing_enabled | lower }}
  {% endif %}
  
  {% if cluster.image_cleaner_enabled is defined %}
  image_cleaner_enabled = {{ cluster.image_cleaner_enabled | lower }}
  {% endif %}
  
  {% if cluster.image_cleaner_interval_hours %}
  image_cleaner_interval_hours = {{ cluster.image_cleaner_interval_hours }}
  {% endif %}
  
  {% if cluster.local_account_disabled is defined %}
  local_account_disabled = {{ cluster.local_account_disabled | lower }}
  {% endif %}
  
  {% if cluster.node_resource_group %}
  node_resource_group = "{{ cluster.node_resource_group }}"
  {% endif %}
  
  {% if cluster.oidc_issuer_enabled is defined %}
  oidc_issuer_enabled = {{ cluster.oidc_issuer_enabled | lower }}
  {% endif %}
  
  {% if cluster.open_service_mesh_enabled is defined %}
  open_service_mesh_enabled = {{ cluster.open_service_mesh_enabled | lower }}
  {% endif %}
  
  {% if cluster.private_cluster_enabled is defined %}
  private_cluster_enabled = {{ cluster.private_cluster_enabled | lower }}
  {% endif %}
  
  {% if cluster.private_dns_zone_id %}
  private_dns_zone_id = "{{ cluster.private_dns_zone_id }}"
  {% endif %}
  
  {% if cluster.private_cluster_public_fqdn_enabled is defined %}
  private_cluster_public_fqdn_enabled = {{ cluster.private_cluster_public_fqdn_enabled | lower }}
  {% endif %}
  
  {% if cluster.workload_identity_enabled is defined %}
  workload_identity_enabled = {{ cluster.workload_identity_enabled | lower }}
  {% endif %}
  
  {% if cluster.public_network_access_enabled is defined %}
  public_network_access_enabled = {{ cluster.public_network_access_enabled | lower }}
  {% endif %}
  
  {% if cluster.role_based_access_control_enabled is defined %}
  role_based_access_control_enabled = {{ cluster.role_based_access_control_enabled | lower }}
  {% endif %}
  
  {% if cluster.run_command_enabled is defined %}
  run_command_enabled = {{ cluster.run_command_enabled | lower }}
  {% endif %}
  
  {% if cluster.sku_tier %}
  sku_tier = "{{ cluster.sku_tier }}"
  {% endif %}

  {% if cluster.default_node_pool_formatted %}
  default_node_pool {
    name       = "{{ cluster.default_node_pool_formatted.name }}"
    {% if cluster.default_node_pool_formatted.node_count %}
    node_count = {{ cluster.default_node_pool_formatted.node_count }}
    {% endif %}
    vm_size    = "{{ cluster.default_node_pool_formatted.vm_size }}"
    
    {% if cluster.default_node_pool_formatted.enable_auto_scaling %}
    enable_auto_scaling = true
    {% if cluster.default_node_pool_formatted.min_count %}
    min_count = {{ cluster.default_node_pool_formatted.min_count }}
    {% endif %}
    {% if cluster.default_node_pool_formatted.max_count %}
    max_count = {{ cluster.default_node_pool_formatted.max_count }}
    {% endif %}
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.enable_host_encryption is defined %}
    enable_host_encryption = {{ cluster.default_node_pool_formatted.enable_host_encryption | lower }}
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.enable_node_public_ip is defined %}
    enable_node_public_ip = {{ cluster.default_node_pool_formatted.enable_node_public_ip | lower }}
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.fips_enabled is defined %}
    fips_enabled = {{ cluster.default_node_pool_formatted.fips_enabled | lower }}
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.kubelet_disk_type %}
    kubelet_disk_type = "{{ cluster.default_node_pool_formatted.kubelet_disk_type }}"
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.max_pods %}
    max_pods = {{ cluster.default_node_pool_formatted.max_pods }}
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.node_labels %}
    node_labels = {{ cluster.default_node_pool_formatted.node_labels | tojson }}
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.node_public_ip_prefix_id %}
    node_public_ip_prefix_id = "{{ cluster.default_node_pool_formatted.node_public_ip_prefix_id }}"
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.node_taints %}
    node_taints = {{ cluster.default_node_pool_formatted.node_taints | tojson }}
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.only_critical_addons_enabled is defined %}
    only_critical_addons_enabled = {{ cluster.default_node_pool_formatted.only_critical_addons_enabled | lower }}
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.orchestrator_version %}
    orchestrator_version = "{{ cluster.default_node_pool_formatted.orchestrator_version }}"
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.os_disk_size_gb %}
    os_disk_size_gb = {{ cluster.default_node_pool_formatted.os_disk_size_gb }}
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.os_disk_type %}
    os_disk_type = "{{ cluster.default_node_pool_formatted.os_disk_type }}"
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.os_sku %}
    os_sku = "{{ cluster.default_node_pool_formatted.os_sku }}"
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.pod_subnet_id %}
    pod_subnet_id = "{{ cluster.default_node_pool_formatted.pod_subnet_id }}"
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.scale_down_mode %}
    scale_down_mode = "{{ cluster.default_node_pool_formatted.scale_down_mode }}"
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.type %}
    type = "{{ cluster.default_node_pool_formatted.type }}"
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.tags %}
    tags = {{ cluster.default_node_pool_formatted.tags | tojson }}
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.ultra_ssd_enabled is defined %}
    ultra_ssd_enabled = {{ cluster.default_node_pool_formatted.ultra_ssd_enabled | lower }}
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.vnet_subnet_id %}
    vnet_subnet_id = "{{ cluster.default_node_pool_formatted.vnet_subnet_id }}"
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.workload_runtime %}
    workload_runtime = "{{ cluster.default_node_pool_formatted.workload_runtime }}"
    {% endif %}
    
    {% if cluster.default_node_pool_formatted.availability_zones %}
    zones = {{ cluster.default_node_pool_formatted.availability_zones | tojson }}
    {% endif %}
  }
  {% elif cluster.default_node_pool %}
  default_node_pool {
    name       = "{{ cluster.default_node_pool.name }}"
    node_count = {{ cluster.default_node_pool.node_count }}
    vm_size    = "{{ cluster.default_node_pool.vm_size }}"
  }
  {% elif cluster.agent_pool_profiles %}
  default_node_pool {
    name       = "{{ cluster.agent_pool_profiles[0].name }}"
    node_count = {{ cluster.agent_pool_profiles[0].count }}
    vm_size    = "{{ cluster.agent_pool_profiles[0].vm_size }}"
  }
  {% endif %}

  {% if cluster.identity_formatted %}
  identity {
    type = "{{ cluster.identity_formatted.type }}"
    {% if cluster.identity_formatted.identity_ids %}
    identity_ids = {{ cluster.identity_formatted.identity_ids | tojson }}
    {% endif %}
  }
  {% elif cluster.identity %}
  identity {
    type = "{{ cluster.identity.type }}"
  }
  {% elif cluster.service_principal_formatted %}
  service_principal {
    client_id     = "{{ cluster.service_principal_formatted.client_id }}"
    client_secret = "{{ cluster.service_principal_formatted.client_secret }}"
  }
  {% endif %}

  {% if cluster.addon_profiles_formatted %}
  {# Map addon profiles to their corresponding AzureRM provider arguments #}
  {% if cluster.addon_profiles_formatted.get('omsAgent') %}
  oms_agent {
    log_analytics_workspace_id = "{{ cluster.addon_profiles_formatted.omsAgent.config.logAnalyticsWorkspaceResourceID }}"
  }
  {% endif %}
  
  {% if cluster.addon_profiles_formatted.get('azureKeyvaultSecretsProvider') %}
  key_vault_secrets_provider {
    {% if cluster.addon_profiles_formatted.azureKeyvaultSecretsProvider.config.get('enableSecretRotation') == 'true' %}
    secret_rotation_enabled = true
    {% endif %}
    {% if cluster.addon_profiles_formatted.azureKeyvaultSecretsProvider.config.get('rotationPollInterval') %}
    secret_rotation_interval = "{{ cluster.addon_profiles_formatted.azureKeyvaultSecretsProvider.config.rotationPollInterval }}"
    {% endif %}
  }
  {% endif %}
  
  {% if cluster.addon_profiles_formatted.get('azurepolicy') %}
  # Azure Policy is enabled via azure_policy_enabled attribute
  {% endif %}
  
  {% if cluster.addon_profiles_formatted.get('httpApplicationRouting') %}
  # HTTP Application Routing is enabled via http_application_routing_enabled attribute
  {% endif %}
  {% endif %}

  {% if cluster.auto_scaler_profile_formatted %}
  auto_scaler_profile {
    {% if cluster.auto_scaler_profile_formatted.balance_similar_node_groups is not none %}
    balance_similar_node_groups = {{ cluster.auto_scaler_profile_formatted.balance_similar_node_groups | lower }}
    {% endif %}
    {% if cluster.auto_scaler_profile_formatted.expander %}
    expander = "{{ cluster.auto_scaler_profile_formatted.expander }}"
    {% endif %}
    {% if cluster.auto_scaler_profile_formatted.max_graceful_termination_sec %}
    max_graceful_termination_sec = "{{ cluster.auto_scaler_profile_formatted.max_graceful_termination_sec }}"
    {% endif %}
    {% if cluster.auto_scaler_profile_formatted.max_node_provision_time %}
    max_node_provision_time = "{{ cluster.auto_scaler_profile_formatted.max_node_provision_time }}"
    {% endif %}
    {% if cluster.auto_scaler_profile_formatted.ok_total_unready_count %}
    max_unready_nodes = {{ cluster.auto_scaler_profile_formatted.ok_total_unready_count }}
    {% endif %}
    {% if cluster.auto_scaler_profile_formatted.max_total_unready_percentage %}
    max_unready_percentage = {{ cluster.auto_scaler_profile_formatted.max_total_unready_percentage }}
    {% endif %}
    {% if cluster.auto_scaler_profile_formatted.new_pod_scale_up_delay %}
    new_pod_scale_up_delay = "{{ cluster.auto_scaler_profile_formatted.new_pod_scale_up_delay }}"
    {% endif %}
    {% if cluster.auto_scaler_profile_formatted.scale_down_delay_after_add %}
    scale_down_delay_after_add = "{{ cluster.auto_scaler_profile_formatted.scale_down_delay_after_add }}"
    {% endif %}
    {% if cluster.auto_scaler_profile_formatted.scale_down_delay_after_delete %}
    scale_down_delay_after_delete = "{{ cluster.auto_scaler_profile_formatted.scale_down_delay_after_delete }}"
    {% endif %}
    {% if cluster.auto_scaler_profile_formatted.scale_down_delay_after_failure %}
    scale_down_delay_after_failure = "{{ cluster.auto_scaler_profile_formatted.scale_down_delay_after_failure }}"
    {% endif %}
    {% if cluster.auto_scaler_profile_formatted.scale_down_unneeded_time %}
    scale_down_unneeded = "{{ cluster.auto_scaler_profile_formatted.scale_down_unneeded_time }}"
    {% endif %}
    {% if cluster.auto_scaler_profile_formatted.scale_down_unready_time %}
    scale_down_unready = "{{ cluster.auto_scaler_profile_formatted.scale_down_unready_time }}"
    {% endif %}
    {% if cluster.auto_scaler_profile_formatted.scale_down_utilization_threshold %}
    scale_down_utilization_threshold = "{{ cluster.auto_scaler_profile_formatted.scale_down_utilization_threshold }}"
    {% endif %}
    {% if cluster.auto_scaler_profile_formatted.skip_nodes_with_local_storage is not none %}
    skip_nodes_with_local_storage = {{ cluster.auto_scaler_profile_formatted.skip_nodes_with_local_storage | lower }}
    {% endif %}
    {% if cluster.auto_scaler_profile_formatted.skip_nodes_with_system_pods is not none %}
    skip_nodes_with_system_pods = {{ cluster.auto_scaler_profile_formatted.skip_nodes_with_system_pods | lower }}
    {% endif %}
  }
  {% endif %}

  {% if cluster.azure_active_directory_role_based_access_control_formatted %}
  azure_active_directory_role_based_access_control {
    {% if cluster.azure_active_directory_role_based_access_control_formatted.managed %}
    managed = true
    {% if cluster.azure_active_directory_role_based_access_control_formatted.tenant_id %}
    tenant_id = "{{ cluster.azure_active_directory_role_based_access_control_formatted.tenant_id }}"
    {% endif %}
    {% if cluster.azure_active_directory_role_based_access_control_formatted.admin_group_object_ids %}
    admin_group_object_ids = {{ cluster.azure_active_directory_role_based_access_control_formatted.admin_group_object_ids | tojson }}
    {% endif %}
    {% if cluster.azure_active_directory_role_based_access_control_formatted.azure_rbac_enabled is defined %}
    azure_rbac_enabled = {{ cluster.azure_active_directory_role_based_access_control_formatted.azure_rbac_enabled | lower }}
    {% endif %}
    {% else %}
    managed = false
    client_app_id = "{{ cluster.azure_active_directory_role_based_access_control_formatted.client_app_id }}"
    server_app_id = "{{ cluster.azure_active_directory_role_based_access_control_formatted.server_app_id }}"
    server_app_secret = "{{ cluster.azure_active_directory_role_based_access_control_formatted.server_app_secret }}"
    {% endif %}
  }
  {% endif %}

  {% if cluster.network_profile_formatted %}
  network_profile {
    network_plugin = "{{ cluster.network_profile_formatted.network_plugin }}"
    {% if cluster.network_profile_formatted.network_mode %}
    network_mode = "{{ cluster.network_profile_formatted.network_mode }}"
    {% endif %}
    {% if cluster.network_profile_formatted.network_policy %}
    network_policy = "{{ cluster.network_profile_formatted.network_policy }}"
    {% endif %}
    {% if cluster.network_profile_formatted.dns_service_ip %}
    dns_service_ip = "{{ cluster.network_profile_formatted.dns_service_ip }}"
    {% endif %}
    {% if cluster.network_profile_formatted.docker_bridge_cidr %}
    docker_bridge_cidr = "{{ cluster.network_profile_formatted.docker_bridge_cidr }}"
    {% endif %}
    {% if cluster.network_profile_formatted.ebpf_data_plane %}
    ebpf_data_plane = "{{ cluster.network_profile_formatted.ebpf_data_plane }}"
    {% endif %}
    {% if cluster.network_profile_formatted.network_plugin_mode %}
    network_plugin_mode = "{{ cluster.network_profile_formatted.network_plugin_mode }}"
    {% endif %}
    {% if cluster.network_profile_formatted.outbound_type %}
    outbound_type = "{{ cluster.network_profile_formatted.outbound_type }}"
    {% endif %}
    {% if cluster.network_profile_formatted.pod_cidr %}
    pod_cidr = "{{ cluster.network_profile_formatted.pod_cidr }}"
    {% endif %}
    {% if cluster.network_profile_formatted.pod_cidrs %}
    pod_cidrs = {{ cluster.network_profile_formatted.pod_cidrs | tojson }}
    {% endif %}
    {% if cluster.network_profile_formatted.service_cidr %}
    service_cidr = "{{ cluster.network_profile_formatted.service_cidr }}"
    {% endif %}
    {% if cluster.network_profile_formatted.service_cidrs %}
    service_cidrs = {{ cluster.network_profile_formatted.service_cidrs | tojson }}
    {% endif %}
    {% if cluster.network_profile_formatted.load_balancer_sku %}
    load_balancer_sku = "{{ cluster.network_profile_formatted.load_balancer_sku }}"
    {% endif %}
  }
  {% elif cluster.network_profile %}
  network_profile {
    network_plugin     = "{{ cluster.network_profile.network_plugin | default('kubenet') }}"
    {% if cluster.network_profile.network_policy %}
    network_policy     = "{{ cluster.network_profile.network_policy }}"
    {% endif %}
    {% if cluster.network_profile.service_cidr %}
    service_cidr       = "{{ cluster.network_profile.service_cidr }}"
    {% endif %}
    {% if cluster.network_profile.dns_service_ip %}
    dns_service_ip     = "{{ cluster.network_profile.dns_service_ip }}"
    {% endif %}
    {% if cluster.network_profile.docker_bridge_cidr %}
    docker_bridge_cidr = "{{ cluster.network_profile.docker_bridge_cidr }}"
    {% endif %}
  }
  {% endif %}

  {% if cluster.linux_profile %}
  linux_profile {
    admin_username = "{{ cluster.linux_profile.admin_username }}"
    
    ssh_key {
      key_data = "{{ cluster.linux_profile.ssh.public_keys[0].key_data | sanitize_public_key }}"
    }
  }
  {% endif %}

  {% if cluster.windows_profile %}
  windows_profile {
    admin_username = "{{ cluster.windows_profile.admin_username }}"
    {% if cluster.windows_profile.admin_password %}
    admin_password = var.admin_password
    {% endif %}
    {% if cluster.windows_profile.license %}
    license = "{{ cluster.windows_profile.license }}"
    {% endif %}
    
    {% if cluster.windows_profile.gmsa %}
    gmsa {
      dns_server  = "{{ cluster.windows_profile.gmsa.dns_server }}"
      root_domain = "{{ cluster.windows_profile.gmsa.root_domain }}"
    }
    {% endif %}
  }
  {% endif %}

{{ macros.render_tags(cluster.tags) }}

  lifecycle {
    prevent_destroy = true
    ignore_changes = [
      default_node_pool[0].node_count,
      windows_profile[0].admin_password
    ]
  }
}

{% endfor %}
{% endblock %}

