Standardizing datastore references to use builting Path func (#8075)

This is a fix to allow use of datastores that are part of a datastore
cluster using the syntax  <datastore cluster name>/<datastore name>
This commit is contained in:
Dan Allegood 2016-08-16 00:58:22 -07:00 committed by Paul Stack
parent fbd0105868
commit 70589fff4d
3 changed files with 57 additions and 17 deletions

View File

@ -115,15 +115,26 @@ func resourceVSphereVirtualDiskCreate(d *schema.ResourceData, meta interface{})
vDisk.datastore = v.(string)
}
diskPath := fmt.Sprintf("[%v] %v", vDisk.datastore, vDisk.vmdkPath)
finder := find.NewFinder(client.Client, true)
err := createHardDisk(client, vDisk.size, diskPath, vDisk.initType, vDisk.adapterType, vDisk.datacenter)
dc, err := getDatacenter(client, d.Get("datacenter").(string))
if err != nil {
return fmt.Errorf("Error finding Datacenter: %s: %s", vDisk.datacenter, err)
}
finder = finder.SetDatacenter(dc)
ds, err := getDatastore(finder, vDisk.datastore)
if err != nil {
return fmt.Errorf("Error finding Datastore: %s: %s", vDisk.datastore, err)
}
err = createHardDisk(client, vDisk.size, ds.Path(vDisk.vmdkPath), vDisk.initType, vDisk.adapterType, vDisk.datacenter)
if err != nil {
return err
}
d.SetId(diskPath)
log.Printf("[DEBUG] Virtual Disk id: %v", diskPath)
d.SetId(ds.Path(vDisk.vmdkPath))
log.Printf("[DEBUG] Virtual Disk id: %v", ds.Path(vDisk.vmdkPath))
return resourceVSphereVirtualDiskRead(d, meta)
}
@ -212,7 +223,16 @@ func resourceVSphereVirtualDiskDelete(d *schema.ResourceData, meta interface{})
if err != nil {
return err
}
diskPath := fmt.Sprintf("[%v] %v", vDisk.datastore, vDisk.vmdkPath)
finder := find.NewFinder(client.Client, true)
finder = finder.SetDatacenter(dc)
ds, err := getDatastore(finder, vDisk.datastore)
if err != nil {
return err
}
diskPath := ds.Path(vDisk.vmdkPath)
virtualDiskManager := object.NewVirtualDiskManager(client.Client)

View File

@ -1279,8 +1279,7 @@ func addHardDisk(vm *object.VirtualMachine, size, iops int64, diskType string, d
if diskPath == "" {
return fmt.Errorf("[ERROR] addHardDisk - No path proided")
} else {
// TODO Check if diskPath & datastore exist
diskPath = fmt.Sprintf("[%v] %v", datastore.Name(), diskPath)
diskPath = datastore.Path(diskPath)
}
log.Printf("[DEBUG] addHardDisk - diskPath: %v", diskPath)
disk := devices.CreateDisk(controller, datastore.Reference(), diskPath)
@ -1368,7 +1367,7 @@ func getNextUnitNumber(devices object.VirtualDeviceList, c types.BaseVirtualCont
}
// addCdrom adds a new virtual cdrom drive to the VirtualMachine and attaches an image (ISO) to it from a datastore path.
func addCdrom(vm *object.VirtualMachine, datastore, path string) error {
func addCdrom(client *govmomi.Client, vm *object.VirtualMachine, datacenter *object.Datacenter, datastore, path string) error {
devices, err := vm.Device(context.TODO())
if err != nil {
return err
@ -1410,7 +1409,14 @@ func addCdrom(vm *object.VirtualMachine, datastore, path string) error {
return err
}
c = devices.InsertIso(c, fmt.Sprintf("[%s] %s", datastore, path))
finder := find.NewFinder(client.Client, true)
finder = finder.SetDatacenter(datacenter)
ds, err := getDatastore(finder, datastore)
if err != nil {
return err
}
c = devices.InsertIso(c, ds.Path(path))
log.Printf("[DEBUG] addCdrom: %#v", c)
return vm.AddDevice(context.TODO(), c)
@ -1618,12 +1624,12 @@ func findDatastore(c *govmomi.Client, sps types.StoragePlacementSpec) (*object.D
}
// createCdroms is a helper function to attach virtual cdrom devices (and their attached disk images) to a virtual IDE controller.
func createCdroms(vm *object.VirtualMachine, cdroms []cdrom) error {
func createCdroms(client *govmomi.Client, vm *object.VirtualMachine, datacenter *object.Datacenter, cdroms []cdrom) error {
log.Printf("[DEBUG] add cdroms: %v", cdroms)
for _, cd := range cdroms {
log.Printf("[DEBUG] add cdrom (datastore): %v", cd.datastore)
log.Printf("[DEBUG] add cdrom (cd path): %v", cd.path)
err := addCdrom(vm, cd.datastore, cd.path)
err := addCdrom(client, vm, datacenter, cd.datastore, cd.path)
if err != nil {
return err
}
@ -1940,7 +1946,7 @@ func (vm *virtualMachine) setupVirtualMachine(c *govmomi.Client) error {
}
// Create the cdroms if needed.
if err := createCdroms(newVM, vm.cdroms); err != nil {
if err := createCdroms(c, newVM, dc, vm.cdroms); err != nil {
return err
}
@ -1967,7 +1973,6 @@ func (vm *virtualMachine) setupVirtualMachine(c *govmomi.Client) error {
default:
return fmt.Errorf("[ERROR] setupVirtualMachine - Neither vmdk path nor vmdk name was given: %#v", vm.hardDisks[i])
}
err = addHardDisk(newVM, vm.hardDisks[i].size, vm.hardDisks[i].iops, vm.hardDisks[i].initType, datastore, diskPath, vm.hardDisks[i].controller)
if err != nil {
err2 := addHardDisk(newVM, vm.hardDisks[i].size, vm.hardDisks[i].iops, vm.hardDisks[i].initType, datastore, diskPath, vm.hardDisks[i].controller)

View File

@ -998,17 +998,20 @@ resource "vsphere_virtual_machine" "foo" {
disk {
size = 1
iops = 500
name = "one"
name = "one"
%s
}
disk {
size = 1
iops = 500
name = "two"
name = "two"
%s
}
disk {
size = 1
iops = 500
name = "three"
name = "three"
%s
}
}
`
@ -1032,7 +1035,19 @@ func TestAccVSphereVirtualMachine_updateDisks(t *testing.T) {
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_basic)
log.Printf("[DEBUG] template config= %s", config_basic)
config_add := basic_vars.testSprintfTemplateBody(testAccCheckVSphereVirtualMachineConfig_updateAddDisks)
config_add := fmt.Sprintf(
testAccCheckVSphereVirtualMachineConfig_updateAddDisks,
basic_vars.locationOpt,
basic_vars.label,
basic_vars.ipv4IpAddress,
basic_vars.ipv4Prefix,
basic_vars.ipv4Gateway,
basic_vars.datastoreOpt,
basic_vars.template,
basic_vars.datastoreOpt,
basic_vars.datastoreOpt,
basic_vars.datastoreOpt,
)
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_basic)
log.Printf("[DEBUG] template config= %s", config_add)