diff --git a/docs/resources/virtual_environment_container.md b/docs/resources/virtual_environment_container.md index 2b9e25fe..996445d7 100644 --- a/docs/resources/virtual_environment_container.md +++ b/docs/resources/virtual_environment_container.md @@ -166,6 +166,7 @@ output "ubuntu_container_public_key" { - `unmanaged` - Unmanaged. - `pool_id` - (Optional) The identifier for a pool to assign the container to. - `started` - (Optional) Whether to start the container (defaults to `true`). +- `tags` - (Optional) A list of tags of the container. This is only meta information (defaults to `[]`). Note: Proxmox always sorts the container tags. If the list in template is not sorted, then Proxmox will always report a difference on the resource. You may use the `ignore_changes` lifecycle meta-argument to ignore changes to this attribute. - `template` - (Optional) Whether to create a template (defaults to `false`). - `vm_id` - (Optional) The virtual machine identifier diff --git a/example/resource_virtual_environment_container.tf b/example/resource_virtual_environment_container.tf index 43160666..46c502e3 100644 --- a/example/resource_virtual_environment_container.tf +++ b/example/resource_virtual_environment_container.tf @@ -40,6 +40,12 @@ resource "proxmox_virtual_environment_container" "example_template" { pool_id = proxmox_virtual_environment_pool.example.id template = true vm_id = 2042 + + tags = [ + "container", + "example", + "terraform", + ] } resource "proxmox_virtual_environment_container" "example" { diff --git a/proxmoxtf/resource_virtual_environment_container.go b/proxmoxtf/resource_virtual_environment_container.go index e1013aa3..04ee4e2c 100644 --- a/proxmoxtf/resource_virtual_environment_container.go +++ b/proxmoxtf/resource_virtual_environment_container.go @@ -7,6 +7,7 @@ package proxmoxtf import ( "context" "fmt" + "sort" "strconv" "strings" @@ -102,6 +103,7 @@ const ( mkResourceVirtualEnvironmentContainerOperatingSystemType = "type" mkResourceVirtualEnvironmentContainerPoolID = "pool_id" mkResourceVirtualEnvironmentContainerStarted = "started" + mkResourceVirtualEnvironmentContainerTags = "tags" mkResourceVirtualEnvironmentContainerTemplate = "template" mkResourceVirtualEnvironmentContainerVMID = "vm_id" ) @@ -561,6 +563,12 @@ func resourceVirtualEnvironmentContainer() *schema.Resource { return d.Get(mkResourceVirtualEnvironmentContainerTemplate).(bool) }, }, + mkResourceVirtualEnvironmentContainerTags: { + Type: schema.TypeList, + Description: "Tags of the container. This is only meta information.", + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, mkResourceVirtualEnvironmentContainerTemplate: { Type: schema.TypeBool, Description: "Whether to create a template", @@ -627,6 +635,7 @@ func resourceVirtualEnvironmentContainerCreateClone( nodeName := d.Get(mkResourceVirtualEnvironmentContainerNodeName).(string) poolID := d.Get(mkResourceVirtualEnvironmentContainerPoolID).(string) + tags := d.Get(mkResourceVirtualEnvironmentContainerTags).([]interface{}) vmID := d.Get(mkResourceVirtualEnvironmentContainerVMID).(int) if vmID == -1 { @@ -922,6 +931,11 @@ func resourceVirtualEnvironmentContainerCreateClone( updateBody.OSType = &operatingSystemType } + if len(tags) > 0 { + tagString := resourceVirtualEnvironmentContainerGetTagsString(d) + updateBody.Tags = &tagString + } + template := proxmox.CustomBool(d.Get(mkResourceVirtualEnvironmentContainerTemplate).(bool)) //nolint:gosimple @@ -1187,6 +1201,7 @@ func resourceVirtualEnvironmentContainerCreateCustom( poolID := d.Get(mkResourceVirtualEnvironmentContainerPoolID).(string) started := proxmox.CustomBool(d.Get(mkResourceVirtualEnvironmentContainerStarted).(bool)) + tags := d.Get(mkResourceVirtualEnvironmentContainerTags).([]interface{}) template := proxmox.CustomBool(d.Get(mkResourceVirtualEnvironmentContainerTemplate).(bool)) vmID := d.Get(mkResourceVirtualEnvironmentContainerVMID).(int) @@ -1247,6 +1262,11 @@ func resourceVirtualEnvironmentContainerCreateCustom( createBody.PoolID = &poolID } + if len(tags) > 0 { + tagsString := resourceVirtualEnvironmentContainerGetTagsString(d) + createBody.Tags = &tagsString + } + err = veClient.CreateContainer(ctx, nodeName, &createBody) if err != nil { return diag.FromErr(err) @@ -1402,6 +1422,19 @@ func resourceVirtualEnvironmentContainerGetOperatingSystemTypeValidator() schema }, false)) } +func resourceVirtualEnvironmentContainerGetTagsString(d *schema.ResourceData) string { + tags := d.Get(mkResourceVirtualEnvironmentContainerTags).([]interface{}) + var sanitizedTags []string + for i := 0; i < len(tags); i++ { + tag := strings.TrimSpace(tags[i].(string)) + if len(tag) > 0 { + sanitizedTags = append(sanitizedTags, tag) + } + } + sort.Strings(sanitizedTags) + return strings.Join(sanitizedTags, ";") +} + func resourceVirtualEnvironmentContainerRead( ctx context.Context, d *schema.ResourceData, @@ -1827,6 +1860,23 @@ func resourceVirtualEnvironmentContainerRead( diags = append(diags, diag.FromErr(err)...) } + currentTags := d.Get(mkResourceVirtualEnvironmentContainerTags).([]interface{}) + + if len(clone) == 0 || len(currentTags) > 0 { + var tags []string + if containerConfig.Tags != nil { + for _, tag := range strings.Split(*containerConfig.Tags, ";") { + t := strings.TrimSpace(tag) + if len(t) > 0 { + tags = append(tags, t) + } + } + sort.Strings(tags) + } + err = d.Set(mkResourceVirtualEnvironmentContainerTags, tags) + diags = append(diags, diag.FromErr(err)...) + } + currentTemplate := d.Get(mkResourceVirtualEnvironmentContainerTemplate).(bool) //nolint:gosimple @@ -2150,6 +2200,11 @@ func resourceVirtualEnvironmentContainerUpdate( rebootRequired = true } + if d.HasChange(mkResourceVirtualEnvironmentContainerTags) { + tagString := resourceVirtualEnvironmentContainerGetTagsString(d) + updateBody.Tags = &tagString + } + // Update the configuration now that everything has been prepared. err = veClient.UpdateContainer(ctx, nodeName, vmID, &updateBody) if err != nil { diff --git a/proxmoxtf/resource_virtual_environment_container_test.go b/proxmoxtf/resource_virtual_environment_container_test.go index 195d660f..4ad03bad 100644 --- a/proxmoxtf/resource_virtual_environment_container_test.go +++ b/proxmoxtf/resource_virtual_environment_container_test.go @@ -36,6 +36,7 @@ func TestResourceVirtualEnvironmentContainerSchema(t *testing.T) { mkResourceVirtualEnvironmentContainerOperatingSystem, mkResourceVirtualEnvironmentContainerPoolID, mkResourceVirtualEnvironmentContainerStarted, + mkResourceVirtualEnvironmentContainerTags, mkResourceVirtualEnvironmentContainerTemplate, mkResourceVirtualEnvironmentContainerVMID, }) @@ -49,6 +50,7 @@ func TestResourceVirtualEnvironmentContainerSchema(t *testing.T) { mkResourceVirtualEnvironmentContainerOperatingSystem: schema.TypeList, mkResourceVirtualEnvironmentContainerPoolID: schema.TypeString, mkResourceVirtualEnvironmentContainerStarted: schema.TypeBool, + mkResourceVirtualEnvironmentContainerTags: schema.TypeList, mkResourceVirtualEnvironmentContainerTemplate: schema.TypeBool, mkResourceVirtualEnvironmentContainerVMID: schema.TypeInt, })