# Generated by Merlya - basic-vm template v{{ template_version }}
# Provider: {{ provider }}

{% if provider == "aws" %}
provider "aws" {
  # Region should be configured via environment or CLI
}

resource "aws_instance" "{{ vm_name }}" {
  ami           = "{{ image_id }}"
  instance_type = "{{ instance_type }}"

{% if subnet_id %}
  subnet_id = "{{ subnet_id }}"
{% endif %}

{% if security_groups %}
  vpc_security_group_ids = [
{% for sg in security_groups %}
    "{{ sg }}"{{ "," if not loop.last else "" }}
{% endfor %}
  ]
{% endif %}

{% if ssh_key_name %}
  key_name = "{{ ssh_key_name }}"
{% endif %}

{% if public_ip %}
  associate_public_ip_address = true
{% endif %}

  root_block_device {
    volume_size = {{ disk_size_gb }}
    volume_type = "gp3"
  }

  tags = {
    Name = "{{ vm_name }}"{{ "," if tags else "" }}
{% for key, value in tags.items() %}
    {{ key }} = "{{ value }}"{{ "," if not loop.last else "" }}
{% endfor %}
  }
}

{% elif provider == "gcp" %}
provider "google" {
  # Project and region should be configured via environment or CLI
}

resource "google_compute_instance" "{{ vm_name }}" {
  name         = "{{ vm_name }}"
  machine_type = "{{ instance_type }}"

  boot_disk {
    initialize_params {
      image = "{{ image_id }}"
      size  = {{ disk_size_gb }}
    }
  }

  network_interface {
{% if subnet_id %}
    subnetwork = "{{ subnet_id }}"
{% else %}
    network = "default"
{% endif %}

{% if public_ip %}
    access_config {
      // Ephemeral public IP
    }
{% endif %}
  }

  labels = {
{% for key, value in tags.items() %}
    {{ key | lower | replace(" ", "_") }} = "{{ value | lower | replace(" ", "_") }}"
{% endfor %}
  }
}

{% elif provider == "azure" %}
provider "azurerm" {
  features {}
}

resource "azurerm_linux_virtual_machine" "{{ vm_name }}" {
  name                = "{{ vm_name }}"
  resource_group_name = var.resource_group_name
  location            = var.location
  size                = "{{ instance_type }}"

{% if ssh_key_name %}
  admin_username = "adminuser"
  admin_ssh_key {
    username   = "adminuser"
    public_key = file("~/.ssh/{{ ssh_key_name }}.pub")
  }
{% else %}
  admin_username                  = "adminuser"
  admin_password                  = var.admin_password
  disable_password_authentication = false
{% endif %}

  network_interface_ids = [
    azurerm_network_interface.{{ vm_name }}.id,
  ]

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
    disk_size_gb         = {{ disk_size_gb }}
  }

{#- Azure image_id must be in format publisher:offer:sku:version -#}
{%- set image_parts = image_id.split(':') if image_id else [] -%}
{%- if image_parts|length == 4 -%}
  source_image_reference {
    publisher = "{{ image_parts[0] }}"
    offer     = "{{ image_parts[1] }}"
    sku       = "{{ image_parts[2] }}"
    version   = "{{ image_parts[3] }}"
  }
{%- else -%}
  # ERROR: image_id must be in format publisher:offer:sku:version
  # Received: {{ image_id | default('(not provided)') }}
  # Example: Canonical:UbuntuServer:18.04-LTS:latest
  source_image_reference {
    publisher = "INVALID_IMAGE_ID_FORMAT"
    offer     = "INVALID_IMAGE_ID_FORMAT"
    sku       = "INVALID_IMAGE_ID_FORMAT"
    version   = "INVALID_IMAGE_ID_FORMAT"
  }
{%- endif -%}

  tags = {
{% for key, value in tags.items() %}
    {{ key }} = "{{ value }}"{{ "," if not loop.last else "" }}
{% endfor %}
  }
}

resource "azurerm_network_interface" "{{ vm_name }}" {
  name                = "{{ vm_name }}-nic"
  resource_group_name = var.resource_group_name
  location            = var.location

  ip_configuration {
    name                          = "internal"
    subnet_id                     = {{ '"' + subnet_id + '"' if subnet_id else 'var.subnet_id' }}
    private_ip_address_allocation = "Dynamic"
{% if public_ip %}
    public_ip_address_id          = azurerm_public_ip.{{ vm_name }}.id
{% endif %}
  }
}

{% if public_ip %}
resource "azurerm_public_ip" "{{ vm_name }}" {
  name                = "{{ vm_name }}-pip"
  resource_group_name = var.resource_group_name
  location            = var.location
  allocation_method   = "Dynamic"
}
{% endif %}

{% endif %}
