Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions cloudstack/resource_cloudstack_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The deleteprotection field should be marked as Computed: true in addition to Optional: true. This allows Terraform to properly track the value when it's read back from the API, ensuring state synchronization and preventing unnecessary diffs.

Similar to the attach field (lines 47-51), update the schema to:

"deleteprotection": {
    Type:     schema.TypeBool,
    Optional: true,
    Computed: true,
},
Suggested change
Optional: true,
Optional: true,
Computed: true,

Copilot uses AI. Check for mistakes.
},

"tags": tagsSchema(),
},
}
}
Expand Down Expand Up @@ -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))
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented-out code should be removed. If this code is not needed, it should be deleted rather than left as a comment.

Suggested change
// p_update.SetDeleteprotection(v.(bool))

Copilot uses AI. Check for mistakes.
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 {
Expand Down Expand Up @@ -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)
}

Expand Down
31 changes: 31 additions & 0 deletions cloudstack/resource_cloudstack_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ func resourceCloudStackInstance() *schema.Resource {
Optional: true,
},

"deleteprotection": {
Type: schema.TypeBool,
Optional: true,
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The deleteprotection field should be marked as Computed: true in addition to Optional: true. This allows Terraform to properly track the value when it's read back from the API, ensuring state synchronization and preventing unnecessary diffs.

Update the schema to:

"deleteprotection": {
    Type:     schema.TypeBool,
    Optional: true,
    Computed: true,
},
Suggested change
Optional: true,
Optional: true,
Computed: true,

Copilot uses AI. Check for mistakes.
},

"tags": tagsSchema(),
},
}
Expand Down Expand Up @@ -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))
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented-out code should be removed. If this code is not needed, it should be deleted rather than left as a comment.

Suggested change
// p_update.SetDeleteprotection(v.(bool))

Copilot uses AI. Check for mistakes.
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)
Expand Down Expand Up @@ -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)
}

Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/disk.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalization inconsistency: "The" should be lowercase as it continues from "If true,".

Suggested change
* `deleteprotection` - (Optional) Set delete protection for the volume. If true, The volume will be protected from deletion.
* `deleteprotection` - (Optional) Set delete protection for the volume. If true, the volume will be protected from deletion.

Copilot uses AI. Check for mistakes.
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:
Expand Down
9 changes: 7 additions & 2 deletions website/docs/r/instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down