Skip to content

Commit 2cd02a6

Browse files
committed
added deleteprotection option for instances and disks
1 parent 16915b6 commit 2cd02a6

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

cloudstack/resource_cloudstack_disk.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,18 @@ func resourceCloudStackDisk() *schema.Resource {
9292
ForceNew: true,
9393
},
9494

95-
"tags": tagsSchema(),
96-
9795
"reattach_on_change": {
9896
Type: schema.TypeBool,
9997
Optional: true,
10098
Default: false,
10199
},
100+
101+
"deleteprotection": {
102+
Type: schema.TypeBool,
103+
Optional: true,
104+
},
105+
106+
"tags": tagsSchema(),
102107
},
103108
}
104109
}
@@ -147,6 +152,21 @@ func resourceCloudStackDiskCreate(d *schema.ResourceData, meta interface{}) erro
147152
// Set the volume ID and partials
148153
d.SetId(r.Id)
149154

155+
// Set deleteprotection using UpdateVolume
156+
if v, ok := d.GetOk("deleteprotection"); ok {
157+
// p_update := cs.Volume.NewUpdateVolumeParams()
158+
// p_update.SetDeleteprotection(v.(bool))
159+
p := cs.Volume.NewUpdateVolumeParams()
160+
p.SetId(d.Id())
161+
p.SetDeleteprotection(v.(bool))
162+
163+
_, err := cs.Volume.UpdateVolume(p)
164+
if err != nil {
165+
return fmt.Errorf(
166+
"Error updating the deleteprotection for disk %s: %s", name, err)
167+
}
168+
}
169+
150170
// Set tags if necessary
151171
err = setTags(cs, d, "Volume")
152172
if err != nil {
@@ -278,6 +298,19 @@ func resourceCloudStackDiskUpdate(d *schema.ResourceData, meta interface{}) erro
278298
}
279299
}
280300

301+
// Check if the deleteprotection has changed and if so, update the deleteprotection
302+
if d.HasChange("deleteprotection") {
303+
p := cs.Volume.NewUpdateVolumeParams()
304+
p.SetId(d.Id())
305+
p.SetDeleteprotection(d.Get("deleteprotection").(bool))
306+
307+
_, err := cs.Volume.UpdateVolume(p)
308+
if err != nil {
309+
return fmt.Errorf(
310+
"Error updating the deleteprotection for disk %s: %s", name, err)
311+
}
312+
}
313+
281314
return resourceCloudStackDiskRead(d, meta)
282315
}
283316

cloudstack/resource_cloudstack_instance.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ func resourceCloudStackInstance() *schema.Resource {
249249
Optional: true,
250250
},
251251

252+
"deleteprotection": {
253+
Type: schema.TypeBool,
254+
Optional: true,
255+
},
256+
252257
"tags": tagsSchema(),
253258
},
254259
}
@@ -479,6 +484,20 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{})
479484

480485
d.SetId(r.Id)
481486

487+
// Set deleteprotection using UpdateVirtualMachine
488+
if v, ok := d.GetOk("deleteprotection"); ok {
489+
// p_update := cs.VirtualMachine.NewUpdateVirtualMachineParams(r.Id)
490+
// p_update.SetDeleteprotection(v.(bool))
491+
p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
492+
p.SetDeleteprotection(v.(bool))
493+
494+
_, err := cs.VirtualMachine.UpdateVirtualMachine(p)
495+
if err != nil {
496+
return fmt.Errorf(
497+
"Error updating the deleteprotection for instance %s: %s", name, err)
498+
}
499+
}
500+
482501
// Set tags if necessary
483502
if err = setTags(cs, d, "userVm"); err != nil {
484503
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{})
873892
}
874893
}
875894

895+
// Check if the deleteprotection has changed and if so, update the deleteprotection
896+
if d.HasChange("deleteprotection") {
897+
p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
898+
p.SetDeleteprotection(d.Get("deleteprotection").(bool))
899+
900+
_, err := cs.VirtualMachine.UpdateVirtualMachine(p)
901+
if err != nil {
902+
return fmt.Errorf(
903+
"Error updating the deleteprotection for instance %s: %s", name, err)
904+
}
905+
}
906+
876907
return resourceCloudStackInstanceRead(d, meta)
877908
}
878909

website/docs/r/disk.html.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ The following arguments are supported:
5656
* `reattach_on_change` - (Optional) Determines whether or not to detach the disk volume
5757
from the virtual machine on disk offering or size change.
5858

59+
* `deleteprotection` - (Optional) Set delete protection for the volume. If true, The volume will be protected from deletion.
60+
Note: If the volume is managed by another service like autoscaling groups or CKS, delete protection will be ignored.
61+
5962
## Attributes Reference
6063

6164
The following attributes are exported:

website/docs/r/instance.html.markdown

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,11 @@ The following arguments are supported:
175175
* `user_data` - (Optional) The user data to provide when launching the
176176
instance. This can be either plain text or base64 encoded text.
177177

178-
* `userdata_id` - (Optional) The ID of a registered CloudStack user data to use for this instance. Cannot be used together with `user_data`.
178+
* `userdata_id` - (Optional) The ID of a registered CloudStack user data to use for this instance.
179+
Cannot be used together with `user_data`.
179180

180-
* `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.
181+
* `userdata_details` - (Optional) A map of key-value pairs to pass as parameters to the user data script.
182+
Only valid when `userdata_id` is specified. Keys must match the parameter names defined in the user data.
181183

182184
* `keypair` - (Optional) The name of the SSH key pair that will be used to
183185
access this instance. (Mutual exclusive with keypairs)
@@ -192,6 +194,9 @@ The following arguments are supported:
192194

193195
* `boot_mode` - (Optional) The boot mode of the instance. Can only be specified when uefi is true. Valid options are 'Legacy' and 'Secure'.
194196

197+
* `deleteprotection` - (Optional) Set delete protection for the virtual machine. If true, the instance will be protected from deletion.
198+
Note: If the instance is managed by another service like autoscaling groups or CKS, delete protection will be ignored.
199+
195200
## Attributes Reference
196201

197202
The following attributes are exported:

0 commit comments

Comments
 (0)