Skip to content

Commit

Permalink
Support volume detachment upon instance update
Browse files Browse the repository at this point in the history
  • Loading branch information
uzaxirr committed Oct 14, 2024
1 parent cbeb424 commit 35f2b23
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions civo/instances/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ func ResourceInstance() *schema.Resource {
Description: "The ID of the firewall to use, from the current list. If left blank or not sent, the default firewall will be used (open to all)",
},
"volume_type": {
Type: schema.TypeString,
Optional: true,
Default: "ms-xfs-2-replicas",
Type: schema.TypeString,
Optional: true,
//Default: "ms-xfs-2-replicas",
Description: "The type of volume to use, either 'ssd' or 'bssd' (optional; default 'ssd')",
},
"tags": {
Expand Down Expand Up @@ -538,6 +538,51 @@ func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
}
}

if d.HasChange("attached_volume") {
oldVolumes, newVolumes := d.GetChange("attached_volume")
oldVolumeList := oldVolumes.([]interface{})
newVolumeList := newVolumes.([]interface{})

// Check if there are any new volumes being attached
for _, newVolume := range newVolumeList {
newVolumeData := newVolume.(map[string]interface{})
found := false
for _, oldVolume := range oldVolumeList {
oldVolumeData := oldVolume.(map[string]interface{})
if newVolumeData["id"] == oldVolumeData["id"] {
found = true
break
}
}
if !found {
// This is a new volume being attached, which is not allowed
return diag.Errorf("Attaching new volumes after instance creation is not allowed. Please create a new civo_volume_attachment resource for attaching additional volume.")
}
}

// Handle volume detachments
for _, oldVolume := range oldVolumeList {
oldVolumeData := oldVolume.(map[string]interface{})
found := false
for _, newVolume := range newVolumeList {
newVolumeData := newVolume.(map[string]interface{})
if oldVolumeData["id"] == newVolumeData["id"] {
found = true
break
}
}
if !found {
// This volume is no longer in the config, so detach it
volumeID := oldVolumeData["id"].(string)
_, err := apiClient.DetachVolume(volumeID)
if err != nil {
return diag.Errorf("Error detaching volume %s: %s", volumeID, err)
}
log.Printf("[INFO] Successfully detached volume %s from instance %s", volumeID, d.Id())
}
}
}

// If reserved_ipv4 has changed, update the instance with the new reserved IP
if d.HasChange("reserved_ipv4") {
oldReservedIP, newReservedIP := d.GetChange("reserved_ipv4")
Expand Down

0 comments on commit 35f2b23

Please # to comment.