0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-30 18:42:58 +00:00

feat(lxc): Add support for container tags (#212)

This commit is contained in:
Moyiz 2023-01-22 02:39:12 +00:00 committed by GitHub
parent 4b2d4b5555
commit 5c8ae3c3f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 0 deletions

View File

@ -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

View File

@ -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" {

View File

@ -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 {

View File

@ -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,
})