mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-06-30 02:31:10 +00:00
feat(lxc): Implement support for container's "protection flag" (#1512)
* feat(lxc): Implement support for container's "protection flag" The Proxmox VE types already has the `protection` API parameter [1], but it is not exposed to the provider users. This pull request implements the missing logic to make it available in order to allow to protect containers against deletion/update operations, including the container's disks. [1]: https://github.com/bpg/terraform-provider-proxmox/blob/v0.63.0/proxmox/nodes/containers/containers_types.go#L59 Relates GH-1126 Signed-off-by: Sven Greb <development@svengreb.de> * Update example/resource_virtual_environment_container.tf Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> --------- Signed-off-by: Sven Greb <development@svengreb.de> Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
This commit is contained in:
parent
a6eb81af08
commit
9b34c485ab
@ -202,6 +202,7 @@ output "ubuntu_container_public_key" {
|
|||||||
- `ubuntu` - Ubuntu.
|
- `ubuntu` - Ubuntu.
|
||||||
- `unmanaged` - Unmanaged.
|
- `unmanaged` - Unmanaged.
|
||||||
- `pool_id` - (Optional) The identifier for a pool to assign the container to.
|
- `pool_id` - (Optional) The identifier for a pool to assign the container to.
|
||||||
|
- `protection` - (Optional) Whether to set the protection flag of the container (defaults to `false`). This will prevent the container itself and its disk for remove/update operations.
|
||||||
- `started` - (Optional) Whether to start the container (defaults to `true`).
|
- `started` - (Optional) Whether to start the container (defaults to `true`).
|
||||||
- `startup` - (Optional) Defines startup and shutdown behavior of the container.
|
- `startup` - (Optional) Defines startup and shutdown behavior of the container.
|
||||||
- `order` - (Required) A non-negative number defining the general startup
|
- `order` - (Required) A non-negative number defining the general startup
|
||||||
|
@ -85,7 +85,9 @@ resource "proxmox_virtual_environment_container" "example" {
|
|||||||
|
|
||||||
node_name = data.proxmox_virtual_environment_nodes.example.names[0]
|
node_name = data.proxmox_virtual_environment_nodes.example.names[0]
|
||||||
pool_id = proxmox_virtual_environment_pool.example.id
|
pool_id = proxmox_virtual_environment_pool.example.id
|
||||||
vm_id = 2043
|
# Set the protection flag to prevent the deletion/update operations for the container and its disks.
|
||||||
|
# protection = true
|
||||||
|
vm_id = 2043
|
||||||
}
|
}
|
||||||
|
|
||||||
output "resource_proxmox_virtual_environment_container_example_id" {
|
output "resource_proxmox_virtual_environment_container_example_id" {
|
||||||
|
@ -73,6 +73,7 @@ const (
|
|||||||
dvNetworkInterfaceMTU = 0
|
dvNetworkInterfaceMTU = 0
|
||||||
dvOperatingSystemType = "unmanaged"
|
dvOperatingSystemType = "unmanaged"
|
||||||
dvPoolID = ""
|
dvPoolID = ""
|
||||||
|
dvProtection = false
|
||||||
dvStarted = true
|
dvStarted = true
|
||||||
dvStartupOrder = -1
|
dvStartupOrder = -1
|
||||||
dvStartupUpDelay = -1
|
dvStartupUpDelay = -1
|
||||||
@ -153,6 +154,7 @@ const (
|
|||||||
mkOperatingSystemTemplateFileID = "template_file_id"
|
mkOperatingSystemTemplateFileID = "template_file_id"
|
||||||
mkOperatingSystemType = "type"
|
mkOperatingSystemType = "type"
|
||||||
mkPoolID = "pool_id"
|
mkPoolID = "pool_id"
|
||||||
|
mkProtection = "protection"
|
||||||
mkStarted = "started"
|
mkStarted = "started"
|
||||||
mkStartup = "startup"
|
mkStartup = "startup"
|
||||||
mkStartupOrder = "order"
|
mkStartupOrder = "order"
|
||||||
@ -782,6 +784,14 @@ func Container() *schema.Resource {
|
|||||||
ForceNew: true,
|
ForceNew: true,
|
||||||
Default: dvPoolID,
|
Default: dvPoolID,
|
||||||
},
|
},
|
||||||
|
mkProtection: {
|
||||||
|
Type: schema.TypeBool,
|
||||||
|
Description: "Whether to set the protection flag of the container. " +
|
||||||
|
"This will prevent the container itself and its disk for remove/update operations.",
|
||||||
|
Optional: true,
|
||||||
|
ForceNew: false,
|
||||||
|
Default: dvProtection,
|
||||||
|
},
|
||||||
mkStarted: {
|
mkStarted: {
|
||||||
Type: schema.TypeBool,
|
Type: schema.TypeBool,
|
||||||
Description: "Whether to start the container",
|
Description: "Whether to start the container",
|
||||||
@ -1037,6 +1047,9 @@ func containerCreateClone(ctx context.Context, d *schema.ResourceData, m interfa
|
|||||||
startOnBoot := types.CustomBool(d.Get(mkStartOnBoot).(bool))
|
startOnBoot := types.CustomBool(d.Get(mkStartOnBoot).(bool))
|
||||||
updateBody.StartOnBoot = &startOnBoot
|
updateBody.StartOnBoot = &startOnBoot
|
||||||
|
|
||||||
|
protection := types.CustomBool(d.Get(mkProtection).(bool))
|
||||||
|
updateBody.Protection = &protection
|
||||||
|
|
||||||
updateBody.StartupBehavior = containerGetStartupBehavior(d)
|
updateBody.StartupBehavior = containerGetStartupBehavior(d)
|
||||||
|
|
||||||
console := d.Get(mkConsole).([]interface{})
|
console := d.Get(mkConsole).([]interface{})
|
||||||
@ -1672,6 +1685,7 @@ func containerCreateCustom(ctx context.Context, d *schema.ResourceData, m interf
|
|||||||
operatingSystemType := operatingSystemBlock[mkOperatingSystemType].(string)
|
operatingSystemType := operatingSystemBlock[mkOperatingSystemType].(string)
|
||||||
|
|
||||||
poolID := d.Get(mkPoolID).(string)
|
poolID := d.Get(mkPoolID).(string)
|
||||||
|
protection := types.CustomBool(d.Get(mkProtection).(bool))
|
||||||
started := types.CustomBool(d.Get(mkStarted).(bool))
|
started := types.CustomBool(d.Get(mkStarted).(bool))
|
||||||
startOnBoot := types.CustomBool(d.Get(mkStartOnBoot).(bool))
|
startOnBoot := types.CustomBool(d.Get(mkStartOnBoot).(bool))
|
||||||
startupBehavior := containerGetStartupBehavior(d)
|
startupBehavior := containerGetStartupBehavior(d)
|
||||||
@ -1709,6 +1723,7 @@ func containerCreateCustom(ctx context.Context, d *schema.ResourceData, m interf
|
|||||||
NetworkInterfaces: networkInterfaceArray,
|
NetworkInterfaces: networkInterfaceArray,
|
||||||
OSTemplateFileVolume: &operatingSystemTemplateFileID,
|
OSTemplateFileVolume: &operatingSystemTemplateFileID,
|
||||||
OSType: &operatingSystemType,
|
OSType: &operatingSystemType,
|
||||||
|
Protection: &protection,
|
||||||
RootFS: rootFS,
|
RootFS: rootFS,
|
||||||
Start: &started,
|
Start: &started,
|
||||||
StartOnBoot: &startOnBoot,
|
StartOnBoot: &startOnBoot,
|
||||||
@ -2559,6 +2574,22 @@ func containerRead(ctx context.Context, d *schema.ResourceData, m interface{}) d
|
|||||||
diags = append(diags, diag.FromErr(err)...)
|
diags = append(diags, diag.FromErr(err)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentProtection := types.CustomBool(d.Get(mkProtection).(bool))
|
||||||
|
|
||||||
|
//nolint:gosimple
|
||||||
|
if len(clone) == 0 || currentProtection != dvProtection {
|
||||||
|
if containerConfig.Protection != nil {
|
||||||
|
e = d.Set(
|
||||||
|
mkProtection,
|
||||||
|
bool(*containerConfig.Protection),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
e = d.Set(mkProtection, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
diags = append(diags, diag.FromErr(e)...)
|
||||||
|
}
|
||||||
|
|
||||||
currentTags := d.Get(mkTags).([]interface{})
|
currentTags := d.Get(mkTags).([]interface{})
|
||||||
|
|
||||||
if len(clone) == 0 || len(currentTags) > 0 {
|
if len(clone) == 0 || len(currentTags) > 0 {
|
||||||
@ -3000,6 +3031,11 @@ func containerUpdate(ctx context.Context, d *schema.ResourceData, m interface{})
|
|||||||
rebootRequired = true
|
rebootRequired = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if d.HasChange(mkProtection) {
|
||||||
|
protection := types.CustomBool(d.Get(mkProtection).(bool))
|
||||||
|
updateBody.Protection = &protection
|
||||||
|
}
|
||||||
|
|
||||||
if d.HasChange(mkTags) {
|
if d.HasChange(mkTags) {
|
||||||
tagString := containerGetTagsString(d)
|
tagString := containerGetTagsString(d)
|
||||||
updateBody.Tags = &tagString
|
updateBody.Tags = &tagString
|
||||||
|
@ -44,6 +44,7 @@ func TestContainerSchema(t *testing.T) {
|
|||||||
mkMountPoint,
|
mkMountPoint,
|
||||||
mkOperatingSystem,
|
mkOperatingSystem,
|
||||||
mkPoolID,
|
mkPoolID,
|
||||||
|
mkProtection,
|
||||||
mkStarted,
|
mkStarted,
|
||||||
mkTags,
|
mkTags,
|
||||||
mkTemplate,
|
mkTemplate,
|
||||||
@ -63,6 +64,7 @@ func TestContainerSchema(t *testing.T) {
|
|||||||
mkMountPoint: schema.TypeList,
|
mkMountPoint: schema.TypeList,
|
||||||
mkOperatingSystem: schema.TypeList,
|
mkOperatingSystem: schema.TypeList,
|
||||||
mkPoolID: schema.TypeString,
|
mkPoolID: schema.TypeString,
|
||||||
|
mkProtection: schema.TypeBool,
|
||||||
mkStarted: schema.TypeBool,
|
mkStarted: schema.TypeBool,
|
||||||
mkTags: schema.TypeList,
|
mkTags: schema.TypeList,
|
||||||
mkTemplate: schema.TypeBool,
|
mkTemplate: schema.TypeBool,
|
||||||
|
Loading…
Reference in New Issue
Block a user