{% extends "common/base_resource.tf.j2" %}

{% block resources %}
{% for resource in resources %}
{{ resource_spacer() }}
resource "azurerm_windows_virtual_machine_scale_set" "{{ resource.name }}" {
  name                = "{{ resource.properties.name }}"
  location            = "{{ resource.properties.location }}"
  resource_group_name = "{{ resource.properties.resource_group_name }}"
  sku                 = "{{ resource.properties.sku }}"
  instances           = {{ resource.properties.instances | default(1) }}
  
  {% if resource.properties.admin_username is defined %}
  admin_username = "{{ resource.properties.admin_username }}"
  {% endif %}
  
  {% if resource.properties.admin_password is defined %}
  admin_password = var.admin_password
  {% endif %}
  
  {% if resource.properties.computer_name_prefix is defined %}
  computer_name_prefix = "{{ resource.properties.computer_name_prefix }}"
  {% endif %}
  
  {% if resource.properties.custom_data is defined %}
  custom_data = "{{ resource.properties.custom_data }}"
  {% endif %}
  
  {% if resource.properties.enable_automatic_updates is defined %}
  enable_automatic_updates = {{ resource.properties.enable_automatic_updates | lower }}
  {% endif %}
  
  {% if resource.properties.hotpatching_enabled is defined %}
  hotpatching_enabled = {{ resource.properties.hotpatching_enabled | lower }}
  {% endif %}
  
  {% if resource.properties.provision_vm_agent is defined %}
  provision_vm_agent = {{ resource.properties.provision_vm_agent | lower }}
  {% endif %}
  
  {% if resource.properties.timezone is defined %}
  timezone = "{{ resource.properties.timezone }}"
  {% endif %}
  
  {% if resource.properties.source_image_id is defined %}
  source_image_id = "{{ resource.properties.source_image_id }}"
  {% else %}
  source_image_reference {
    publisher = "{{ resource.properties.source_image_reference.publisher }}"
    offer     = "{{ resource.properties.source_image_reference.offer }}"
    sku       = "{{ resource.properties.source_image_reference.sku }}"
    version   = "{{ resource.properties.source_image_reference.version }}"
  }
  {% endif %}
  
  os_disk {
    caching              = "{{ resource.properties.os_disk.caching | default('ReadWrite') }}"
    storage_account_type = "{{ resource.properties.os_disk.storage_account_type | default('Standard_LRS') }}"
    
    {% if resource.properties.os_disk.disk_size_gb is defined %}
    disk_size_gb = {{ resource.properties.os_disk.disk_size_gb }}
    {% endif %}
    
    {% if resource.properties.os_disk.disk_encryption_set_id is defined %}
    disk_encryption_set_id = "{{ resource.properties.os_disk.disk_encryption_set_id }}"
    {% endif %}
  }
  
  {% for interface in resource.properties.network_interfaces %}
  network_interface {
    name    = "{{ interface.name }}"
    primary = {{ interface.primary | default(false) | lower }}
    
    {% for ip_config in interface.ip_configurations %}
    ip_configuration {
      name      = "{{ ip_config.name }}"
      primary   = {{ ip_config.primary | default(false) | lower }}
      subnet_id = "{{ ip_config.subnet_id }}"
      
      {% if ip_config.public_ip_address is defined %}
      public_ip_address {
        name = "{{ ip_config.public_ip_address.name }}"
        {% if ip_config.public_ip_address.domain_name_label is defined %}
        domain_name_label = "{{ ip_config.public_ip_address.domain_name_label }}"
        {% endif %}
      }
      {% endif %}
      
      {% if ip_config.load_balancer_backend_address_pool_ids is defined %}
      load_balancer_backend_address_pool_ids = {{ ip_config.load_balancer_backend_address_pool_ids | tojson }}
      {% endif %}
      
      {% if ip_config.load_balancer_inbound_nat_rules_ids is defined %}
      load_balancer_inbound_nat_rules_ids = {{ ip_config.load_balancer_inbound_nat_rules_ids | tojson }}
      {% endif %}
      
      {% if ip_config.application_gateway_backend_address_pool_ids is defined %}
      application_gateway_backend_address_pool_ids = {{ ip_config.application_gateway_backend_address_pool_ids | tojson }}
      {% endif %}
      
      {% if ip_config.application_security_group_ids is defined %}
      application_security_group_ids = {{ ip_config.application_security_group_ids | tojson }}
      {% endif %}
    }
    {% endfor %}
    
    {% if interface.network_security_group_id is defined %}
    network_security_group_id = "{{ interface.network_security_group_id }}"
    {% endif %}
    
    {% if interface.enable_accelerated_networking is defined %}
    enable_accelerated_networking = {{ interface.enable_accelerated_networking | lower }}
    {% endif %}
    
    {% if interface.enable_ip_forwarding is defined %}
    enable_ip_forwarding = {{ interface.enable_ip_forwarding | lower }}
    {% endif %}
  }
  {% endfor %}
  
  {% if resource.properties.boot_diagnostics is defined %}
  boot_diagnostics {
    {% if resource.properties.boot_diagnostics.storage_account_uri is defined %}
    storage_account_uri = "{{ resource.properties.boot_diagnostics.storage_account_uri }}"
    {% endif %}
  }
  {% endif %}
  
  {% if resource.properties.identity is defined and resource.properties.identity and resource.properties.identity.type is defined %}

  
  identity {

  
    type = "{{ resource.properties.identity.type }}"
    {% if resource.properties.identity.identity_ids is defined %}
    identity_ids = {{ resource.properties.identity.identity_ids | tojson }}
    {% endif %}
  }
  {% endif %}
  
  {% if resource.properties.plan is defined %}
  plan {
    name      = "{{ resource.properties.plan.name }}"
    publisher = "{{ resource.properties.plan.publisher }}"
    product   = "{{ resource.properties.plan.product }}"
  }
  {% endif %}
  
  {% if resource.properties.automatic_os_upgrade_policy is defined %}
  automatic_os_upgrade_policy {
    disable_automatic_rollback = {{ resource.properties.automatic_os_upgrade_policy.disable_automatic_rollback | default(false) | lower }}
    enable_automatic_os_upgrade = {{ resource.properties.automatic_os_upgrade_policy.enable_automatic_os_upgrade | default(false) | lower }}
  }
  {% endif %}
  
  {% if resource.properties.rolling_upgrade_policy is defined %}
  rolling_upgrade_policy {
    max_batch_instance_percent              = {{ resource.properties.rolling_upgrade_policy.max_batch_instance_percent | default(20) }}
    max_unhealthy_instance_percent          = {{ resource.properties.rolling_upgrade_policy.max_unhealthy_instance_percent | default(20) }}
    max_unhealthy_upgraded_instance_percent = {{ resource.properties.rolling_upgrade_policy.max_unhealthy_upgraded_instance_percent | default(20) }}
    pause_time_between_batches              = "{{ resource.properties.rolling_upgrade_policy.pause_time_between_batches | default('PT0S') }}"
  }
  {% endif %}
  
  {% if resource.properties.health_probe_id is defined %}
  health_probe_id = "{{ resource.properties.health_probe_id }}"
  {% endif %}
  
  {% if resource.properties.automatic_instance_repair is defined %}
  automatic_instance_repair {
    enabled      = {{ resource.properties.automatic_instance_repair.enabled | lower }}
    grace_period = "{{ resource.properties.automatic_instance_repair.grace_period | default('PT30M') }}"
  }
  {% endif %}
  
  {% if resource.properties.terminate_notification is defined %}
  terminate_notification {
    enabled = {{ resource.properties.terminate_notification.enabled | lower }}
    timeout = "{{ resource.properties.terminate_notification.timeout | default('PT5M') }}"
  }
  {% endif %}
  
  {% if resource.properties.scale_in is defined %}
  scale_in {
    rule                   = "{{ resource.properties.scale_in.rule | default('Default') }}"
    force_deletion_enabled = {{ resource.properties.scale_in.force_deletion_enabled | default(false) | lower }}
  }
  {% endif %}
  
  {% if resource.properties.overprovision is defined %}
  overprovision = {{ resource.properties.overprovision | lower }}
  {% endif %}
  
  {% if resource.properties.single_placement_group is defined %}
  single_placement_group = {{ resource.properties.single_placement_group | lower }}
  {% endif %}
  
  {% if resource.properties.zone_balance is defined %}
  zone_balance = {{ resource.properties.zone_balance | lower }}
  {% endif %}
  
  {% if resource.properties.zones is defined %}
  zones = {{ resource.properties.zones | tojson }}
  {% endif %}
  
  {% if resource.properties.proximity_placement_group_id is defined %}
  proximity_placement_group_id = "{{ resource.properties.proximity_placement_group_id }}"
  {% endif %}
  
  {% if resource.properties.tags is defined %}
  tags = {{ resource.properties.tags | tojson }}
  {% endif %}
}

{% endfor %}
{% endblock %}
