terraform-provider-incus icon indicating copy to clipboard operation
terraform-provider-incus copied to clipboard

profile "default" cannot be tracked

Open clbouvier opened this issue 1 year ago • 1 comments

terraform {
  required_providers {
    incus = {
      source = "lxc/incus"
      version = "~> 0.1.1"
    }
  }
  required_version = ">= 1.8"
}

provider "incus" {}

resource "incus_project" "project" {
  name = "myproject"

  config = {
    "features.profiles" = true
  }
}

resource "incus_profile" "default" {
  name = "default"
  project = incus_project.project.name

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

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

returns the next error

│ Error: Failed to create profile "default"
│ 
│   with incus_profile.default,
│   on main.tf line 21, in resource "incus_profile" "default":
│   21: resource "incus_profile" "default" {
│ 
│ Error inserting "default" into database: The profile already exists

The error is expected because incus always creates a default profile. Of course, we can change the name of the profile and working without the default profile but in this case we cannot track the "default" dynamically created. Import would not work because the profile is not known before the project creation. I can remind that other providers (aws) uses data source with the default. Would data for default profile (like data profile_default) fix the problem?

clbouvier avatar May 05 '24 21:05 clbouvier

With the changes from https://github.com/lxc/terraform-provider-incus/pull/63 it would be possible to do the following after we have released a new version of the provider:

terraform {
  required_providers {
    incus = {
      source = "lxc/incus"
    }
  }
  required_version = ">= 1.8"
}

provider "incus" {}

resource "incus_project" "project" {
  name = "myproject"

  config = {
    "features.profiles" = true
  }
}

data "incus_profile" "default" {
  name = "default"
  project = incus_project.project.name
}

resource "incus_profile" "project_default" {
  name = "project_default"
  project = incus_project.project.name

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

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

resource "incus_instance" "d1" {
  profiles = [data.incus_profile.default.name, incus_profile.project_default.name]
  image    = "images:debian/12"
  name     = "d1"
}

Alternatively, you could use the following workaround already:

  1. create only the project with Terraform by defining the resource incus_project.myproject and run terraform apply first.
  2. define the resource incus_profile.default and use terraform import incus_profile.default myproject/default.
  3. from now on, every change to the default profile in Terraform will be transferred to Incus.

maveonair avatar May 13 '24 11:05 maveonair

I am closing this topic due to the merging of https://github.com/lxc/terraform-provider-incus/pull/63

maveonair avatar May 27 '24 12:05 maveonair