Skip to content

Commit

Permalink
solve bug
Browse files Browse the repository at this point in the history
  • Loading branch information
uzaxirr committed Oct 8, 2024
1 parent 61610a4 commit 97503ad
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions civo/instances/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,29 @@ func resourceInstanceRead(_ context.Context, d *schema.ResourceData, m interface
}

if len(resp.AttachedVolumes) > 0 {
volumes := make([]map[string]interface{}, 0, len(resp.AttachedVolumes))
for _, volume := range resp.AttachedVolumes {
volumeMap := map[string]interface{}{
"id": volume.ID,
// Get the attached volumes from the API response
attachedVolumes := resp.AttachedVolumes

// Get the attached volumes from the Terraform state
tfAttachedVolumes := d.Get("attached_volume").([]interface{})

// Create a map of volumes listed in the Terraform config for comparison
configVolumeMap := make(map[string]bool)
for _, v := range tfAttachedVolumes {
volume := v.(map[string]interface{})
configVolumeMap[volume["id"].(string)] = true
}

// Filter out API volumes that are not in the Terraform config
var filteredVolumes []civogo.AttachedVolume
for _, vol := range attachedVolumes {
if _, exists := configVolumeMap[vol.ID]; exists {
filteredVolumes = append(filteredVolumes, vol)
}
volumes = append(volumes, volumeMap)
}

d.Set("attached_volume", volumes)
// Set only the filtered volumes in the Terraform state
d.Set("attached_volume", filteredVolumes)
}

if resp.Script == "" {
Expand Down

0 comments on commit 97503ad

Please sign in to comment.