Skip to content

Commit

Permalink
Pick default datastore for extra disks (#897)
Browse files Browse the repository at this point in the history
Pick default datastore for extra disks

When cloning virtual machines it is possible to define additional disks
to be created. If a datastore is not defined either for the disk or for
the virtual machine then the creation of the additional virtual machines
will fail.

This change introduces a fallback where if a datastore is not set we
pick the first datastore found that is associated with the virtual
machine.
  • Loading branch information
koikonom authored Nov 11, 2019
1 parent b1c5718 commit c26e5f0
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package virtualdevice
import (
"errors"
"fmt"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/virtualmachine"
"log"
"math"
"path"
Expand Down Expand Up @@ -1695,8 +1696,24 @@ func (r *DiskSubresource) assignBackingInfo(disk *types.VirtualDisk) error {
if dsID == "" || dsID == diskDatastoreComputedName {
// Default to the default datastore
dsID = r.rdd.Get("datastore_id").(string)
}

if dsID == "" {
vmObj, err := virtualmachine.FromUUID(r.client, r.rdd.Id())
if err != nil {
return err
}

vmprops, err := virtualmachine.Properties(vmObj)
if err != nil {
return err
}
if len(vmprops.Datastore) == 0 {
return fmt.Errorf("no datastore was set and was unable to find a default to fall back to")
}
dsID = vmprops.Datastore[0].Value

}
}
ds, err := datastore.FromID(r.client, dsID)
if err != nil {
return err
Expand Down

0 comments on commit c26e5f0

Please sign in to comment.