# {{ project_name }} - LXD Provider Configuration
# Generated by typedialog-prov-gen

terraform {
  required_providers {
    lxd = {
      source  = "terraform-lxd/lxd"
      version = "~> 1.10"
    }
  }
}

provider "lxd" {
  generate_client_certificates = true
  accept_remote_certificate    = true

  lxd_remote {
    name     = var.lxd_remote_name
    scheme   = "https"
    address  = var.lxd_remote_address
    password = var.lxd_remote_password
    default  = true
  }
}

# LXD-specific variables
variable "lxd_remote_name" {
  description = "LXD remote name"
  type        = string
  default     = "local"
}

variable "lxd_remote_address" {
  description = "LXD remote server address"
  type        = string
  default     = "{{ infrastructure.lxd_address | default(value="127.0.0.1:8443") }}"
}

variable "lxd_remote_password" {
  description = "LXD remote server password"
  type        = string
  sensitive   = true
  default     = ""
}

variable "lxd_image" {
  description = "LXD container image"
  type        = string
  default     = "ubuntu:22.04"
}

# LXD profile for containers
resource "lxd_profile" "app" {
  name = "${local.project_name}-${local.environment}"

  config = {
    "boot.autostart"       = "true"
    "security.nesting"     = "true"
    "security.privileged"  = "false"
  }

  device {
    name = "root"
    type = "disk"

    properties = {
      pool = "default"
      path = "/"
    }
  }

  device {
    name = "eth0"
    type = "nic"

    properties = {
      network = "lxdbr0"
      name    = "eth0"
    }
  }
}

# LXD container instance
resource "lxd_container" "app" {
  name    = "${local.project_name}-${local.environment}-app"
  image   = var.lxd_image
  profiles = [lxd_profile.app.name]

  config = {
    "user.project"     = local.project_name
    "user.environment" = local.environment
  }

  limits = {
    cpu    = "2"
    memory = "2GB"
  }

  provisioner "remote-exec" {
    inline = [
      "apt-get update",
      "apt-get install -y curl wget git",
    ]
  }
}

output "lxd_container_ip" {
  description = "LXD container IP address"
  value       = lxd_container.app.ip_address
}

output "lxd_container_name" {
  description = "LXD container name"
  value       = lxd_container.app.name
}
