Skip to content

Commit

Permalink
Update deleteFile function methods
Browse files Browse the repository at this point in the history
A destroy would complete as a false positive when deleting a .`vmdk` and would leave the file since the `DeleteDatastoreFile_Task `could not delete a VMDK.

This change updates the methods used in the `deleteFile` function.
- If the source file is a `.vmdk`, the Delete method uses the correct `DeleteVirtualDisk_Task`.
- If the source file is not a `.vmdk`, the Delete method uses the correct `DeleteDatastoreFile_Task`.

Signed-off-by: Ryan Johnson <[email protected]>
  • Loading branch information
Ryan Johnson authored Feb 9, 2022
1 parent 677a35c commit 86a38bb
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions vsphere/resource_vsphere_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ func resourceVSphereFileDelete(d *schema.ResourceData, meta interface{}) error {
}

func deleteFile(client *govmomi.Client, f *file) error {

dc, err := getDatacenter(client, f.datacenter)
if err != nil {
return err
Expand All @@ -405,17 +406,36 @@ func deleteFile(client *govmomi.Client, f *file) error {
return fmt.Errorf("error %s", err)
}

fm := object.NewFileManager(client.Client)
task, err := fm.DeleteDatastoreFile(context.TODO(), ds.Path(f.destinationFile), dc)
if err != nil {
return err
}
// If the source file is a VMDK, the Delete method uses the correct DeleteVirtualDisk_Task
if path.Ext(f.destinationFile) == ".vmdk" {
vdm := object.NewVirtualDiskManager(client.Client)
task, err := vdm.DeleteVirtualDisk(context.TODO(), ds.Path(f.destinationFile), dc)
if err != nil {
return err
}

_, err = task.WaitForResult(context.TODO(), nil)
if err != nil {
return err
}
return nil

} else {
// If the source file is not a VMDK, the Delete method uses the correct DeleteDatastoreFile_Task
fm := object.NewFileManager(client.Client)
task, err := fm.DeleteDatastoreFile(context.TODO(), ds.Path(f.destinationFile), dc)
if err != nil {
return err
}

_, err = task.WaitForResult(context.TODO(), nil)
if err != nil {
return err
}
return nil

_, err = task.WaitForResult(context.TODO(), nil)
if err != nil {
return err
}
return nil

}

// getDatastore gets datastore object
Expand Down

0 comments on commit 86a38bb

Please sign in to comment.