From 2cd02a6753057ee33fc654864360d02a41dfdbc0 Mon Sep 17 00:00:00 2001 From: Curverneur Date: Wed, 3 Dec 2025 14:02:13 +0100 Subject: [PATCH] added deleteprotection option for instances and disks --- cloudstack/resource_cloudstack_disk.go | 37 ++++++++++++++++++++-- cloudstack/resource_cloudstack_instance.go | 31 ++++++++++++++++++ website/docs/r/disk.html.markdown | 3 ++ website/docs/r/instance.html.markdown | 9 ++++-- 4 files changed, 76 insertions(+), 4 deletions(-) diff --git a/cloudstack/resource_cloudstack_disk.go b/cloudstack/resource_cloudstack_disk.go index dc325bca..e76f7a39 100644 --- a/cloudstack/resource_cloudstack_disk.go +++ b/cloudstack/resource_cloudstack_disk.go @@ -92,13 +92,18 @@ func resourceCloudStackDisk() *schema.Resource { ForceNew: true, }, - "tags": tagsSchema(), - "reattach_on_change": { Type: schema.TypeBool, Optional: true, Default: false, }, + + "deleteprotection": { + Type: schema.TypeBool, + Optional: true, + }, + + "tags": tagsSchema(), }, } } @@ -147,6 +152,21 @@ func resourceCloudStackDiskCreate(d *schema.ResourceData, meta interface{}) erro // Set the volume ID and partials d.SetId(r.Id) + // Set deleteprotection using UpdateVolume + if v, ok := d.GetOk("deleteprotection"); ok { + // p_update := cs.Volume.NewUpdateVolumeParams() + // p_update.SetDeleteprotection(v.(bool)) + p := cs.Volume.NewUpdateVolumeParams() + p.SetId(d.Id()) + p.SetDeleteprotection(v.(bool)) + + _, err := cs.Volume.UpdateVolume(p) + if err != nil { + return fmt.Errorf( + "Error updating the deleteprotection for disk %s: %s", name, err) + } + } + // Set tags if necessary err = setTags(cs, d, "Volume") if err != nil { @@ -278,6 +298,19 @@ func resourceCloudStackDiskUpdate(d *schema.ResourceData, meta interface{}) erro } } + // Check if the deleteprotection has changed and if so, update the deleteprotection + if d.HasChange("deleteprotection") { + p := cs.Volume.NewUpdateVolumeParams() + p.SetId(d.Id()) + p.SetDeleteprotection(d.Get("deleteprotection").(bool)) + + _, err := cs.Volume.UpdateVolume(p) + if err != nil { + return fmt.Errorf( + "Error updating the deleteprotection for disk %s: %s", name, err) + } + } + return resourceCloudStackDiskRead(d, meta) } diff --git a/cloudstack/resource_cloudstack_instance.go b/cloudstack/resource_cloudstack_instance.go index 6a38ddb4..ce6a041d 100644 --- a/cloudstack/resource_cloudstack_instance.go +++ b/cloudstack/resource_cloudstack_instance.go @@ -249,6 +249,11 @@ func resourceCloudStackInstance() *schema.Resource { Optional: true, }, + "deleteprotection": { + Type: schema.TypeBool, + Optional: true, + }, + "tags": tagsSchema(), }, } @@ -479,6 +484,20 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{}) d.SetId(r.Id) + // Set deleteprotection using UpdateVirtualMachine + if v, ok := d.GetOk("deleteprotection"); ok { + // p_update := cs.VirtualMachine.NewUpdateVirtualMachineParams(r.Id) + // p_update.SetDeleteprotection(v.(bool)) + p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id()) + p.SetDeleteprotection(v.(bool)) + + _, err := cs.VirtualMachine.UpdateVirtualMachine(p) + if err != nil { + return fmt.Errorf( + "Error updating the deleteprotection for instance %s: %s", name, err) + } + } + // Set tags if necessary if err = setTags(cs, d, "userVm"); err != nil { return fmt.Errorf("Error setting tags on the new instance %s: %s", name, err) @@ -873,6 +892,18 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{}) } } + // Check if the deleteprotection has changed and if so, update the deleteprotection + if d.HasChange("deleteprotection") { + p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id()) + p.SetDeleteprotection(d.Get("deleteprotection").(bool)) + + _, err := cs.VirtualMachine.UpdateVirtualMachine(p) + if err != nil { + return fmt.Errorf( + "Error updating the deleteprotection for instance %s: %s", name, err) + } + } + return resourceCloudStackInstanceRead(d, meta) } diff --git a/website/docs/r/disk.html.markdown b/website/docs/r/disk.html.markdown index 1f4e0528..15c92913 100644 --- a/website/docs/r/disk.html.markdown +++ b/website/docs/r/disk.html.markdown @@ -56,6 +56,9 @@ The following arguments are supported: * `reattach_on_change` - (Optional) Determines whether or not to detach the disk volume from the virtual machine on disk offering or size change. +* `deleteprotection` - (Optional) Set delete protection for the volume. If true, The volume will be protected from deletion. + Note: If the volume is managed by another service like autoscaling groups or CKS, delete protection will be ignored. + ## Attributes Reference The following attributes are exported: diff --git a/website/docs/r/instance.html.markdown b/website/docs/r/instance.html.markdown index ebf3f20f..d703853d 100644 --- a/website/docs/r/instance.html.markdown +++ b/website/docs/r/instance.html.markdown @@ -175,9 +175,11 @@ The following arguments are supported: * `user_data` - (Optional) The user data to provide when launching the instance. This can be either plain text or base64 encoded text. -* `userdata_id` - (Optional) The ID of a registered CloudStack user data to use for this instance. Cannot be used together with `user_data`. +* `userdata_id` - (Optional) The ID of a registered CloudStack user data to use for this instance. + Cannot be used together with `user_data`. -* `userdata_details` - (Optional) A map of key-value pairs to pass as parameters to the user data script. Only valid when `userdata_id` is specified. Keys must match the parameter names defined in the user data. +* `userdata_details` - (Optional) A map of key-value pairs to pass as parameters to the user data script. + Only valid when `userdata_id` is specified. Keys must match the parameter names defined in the user data. * `keypair` - (Optional) The name of the SSH key pair that will be used to access this instance. (Mutual exclusive with keypairs) @@ -192,6 +194,9 @@ The following arguments are supported: * `boot_mode` - (Optional) The boot mode of the instance. Can only be specified when uefi is true. Valid options are 'Legacy' and 'Secure'. +* `deleteprotection` - (Optional) Set delete protection for the virtual machine. If true, the instance will be protected from deletion. + Note: If the instance is managed by another service like autoscaling groups or CKS, delete protection will be ignored. + ## Attributes Reference The following attributes are exported: