{% set config = merged_configs.get('azurerm_container_group', {}) %}
{% set comments = compliance_comments.get('azurerm_container_group', []) %}
# ------------------------------------------------------------------------------
# Azure Container Instances (Container Group)
# ------------------------------------------------------------------------------
{% for comment in comments %}
# {{ comment }}
{% endfor %}

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

  os_type    = "{{ config.get('os_type', 'Linux') }}"
  ip_address_type = "{{ config.get('ip_address_type', 'Private') }}"

  {% if config.get('subnet_ids') %}
  subnet_ids = {{ config.subnet_ids }}
  {% endif %}

  restart_policy = "{{ config.get('restart_policy', 'OnFailure') }}"

  container {
    name   = "{{ config.get('container_name', 'app') }}"
    image  = "{{ config.get('container_image', 'mcr.microsoft.com/azuredocs/aci-helloworld:latest') }}"
    cpu    = "{{ config.get('cpu', '1') }}"
    memory = "{{ config.get('memory', '1.5') }}"

    ports {
      port     = {{ config.get('port', 443) }}
      protocol = "{{ config.get('protocol', 'TCP') }}"
    }
  }

  {% if config.get('image_registry_credential') %}
  image_registry_credential {
    server   = "{{ config.image_registry_credential.server }}"
    username = "{{ config.image_registry_credential.get('username', '') }}"
    password = "{{ config.image_registry_credential.get('password', '') }}"
  }
  {% endif %}

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

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