func (vs *VSphere) CreateVolume(volumeOptions *VolumeOptions, dataCenterRef types.ManagedObjectReference) (string, error) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	Connect(ctx, vs)
	datacenter := object.NewDatacenter(vs.Client.Client, dataCenterRef)
	var (
		err                     error
		directoryAlreadyPresent = false
	)
	if datacenter == nil {
		return "", fmt.Errorf("datacenter is nil")
	}
	vs.initVolumeOptions(volumeOptions)
	finder := find.NewFinder(datacenter.Client(), false)
	finder.SetDatacenter(datacenter)
	ds, err := finder.Datastore(ctx, volumeOptions.Datastore)
	if err != nil {
		return "", fmt.Errorf("Failed while searching for datastore: %s. err: %+v", volumeOptions.Datastore, err)
	}
	directoryPath := filepath.Clean(ds.Path(volDir)) + "/"
	fileManager := object.NewFileManager(ds.Client())
	err = fileManager.MakeDirectory(ctx, directoryPath, datacenter, false)
	if err != nil {
		if soap.IsSoapFault(err) {
			soapFault := soap.ToSoapFault(err)
			if _, ok := soapFault.VimFault().(types.FileAlreadyExists); ok {
				directoryAlreadyPresent = true
				framework.Logf("Directory with the path %+q is already present", directoryPath)
			}
		}
		if !directoryAlreadyPresent {
			framework.Logf("Cannot create dir %#v. err %s", directoryPath, err)
			return "", err
		}
	}
	framework.Logf("Created dir with path as %+q", directoryPath)
	vmdkPath := directoryPath + volumeOptions.Name + ".vmdk"

	// Create a virtual disk manager
	vdm := object.NewVirtualDiskManager(ds.Client())
	// Create specification for new virtual disk
	vmDiskSpec := &types.FileBackedVirtualDiskSpec{
		VirtualDiskSpec: types.VirtualDiskSpec{
			AdapterType: volumeOptions.SCSIControllerType,
			DiskType:    volumeOptions.DiskFormat,
		},
		CapacityKb: int64(volumeOptions.CapacityKB),
	}
	// Create virtual disk
	task, err := vdm.CreateVirtualDisk(ctx, vmdkPath, datacenter, vmDiskSpec)
	if err != nil {
		framework.Logf("Failed to create virtual disk: %s. err: %+v", vmdkPath, err)
		return "", err
	}
	taskInfo, err := task.WaitForResult(ctx, nil)
	if err != nil {
		framework.Logf("Failed to complete virtual disk creation: %s. err: %+v", vmdkPath, err)
		return "", err
	}
	volumePath := taskInfo.Result.(string)
	canonicalDiskPath, err := getCanonicalVolumePath(ctx, datacenter, volumePath)
	if err != nil {
		return "", err
	}
	return canonicalDiskPath, nil
}
// CreateVolume creates a vsphere volume using given volume parameters specified in VolumeOptions.
// If volume is created successfully the canonical disk path is returned else error is returned.
CreateVolume creates a vsphere volume using given volume parameters specified in VolumeOptions.
If volume is created successfully the canonical disk path is returned else error is returned.
// CreateVolume creates a vsphere volume using given volume parameters specified in VolumeOptions . // If volume is created successfully the canonical disk path is returned else error is returned .
func (vs *VSphere) CreateVolume(volumeOptions *VolumeOptions, dataCenterRef types.ManagedObjectReference) (string, error) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	Connect(ctx, vs)
	datacenter := object.NewDatacenter(vs.Client.Client, dataCenterRef)
	var (
		err                     error
		directoryAlreadyPresent = false
	)
	if datacenter == nil {
		return "", fmt.Errorf("datacenter is nil")
	}
	vs.initVolumeOptions(volumeOptions)
	finder := find.NewFinder(datacenter.Client(), false)
	finder.SetDatacenter(datacenter)
	ds, err := finder.Datastore(ctx, volumeOptions.Datastore)
	if err != nil {
		return "", fmt.Errorf("Failed while searching for datastore: %s. err: %+v", volumeOptions.Datastore, err)
	}
	directoryPath := filepath.Clean(ds.Path(volDir)) + "/"
	fileManager := object.NewFileManager(ds.Client())
	err = fileManager.MakeDirectory(ctx, directoryPath, datacenter, false)
	if err != nil {
		if soap.IsSoapFault(err) {
			soapFault := soap.ToSoapFault(err)
			if _, ok := soapFault.VimFault().(types.FileAlreadyExists); ok {
				directoryAlreadyPresent = true
				framework.Logf("Directory with the path %+q is already present", directoryPath)
			}
		}
		if !directoryAlreadyPresent {
			framework.Logf("Cannot create dir %#v. err %s", directoryPath, err)
			return "", err
		}
	}
	framework.Logf("Created dir with path as %+q", directoryPath)
	vmdkPath := directoryPath + volumeOptions.Name + ".vmdk"
	vdm := object.NewVirtualDiskManager(ds.Client())
	vmDiskSpec := &types.FileBackedVirtualDiskSpec{
		VirtualDiskSpec: types.VirtualDiskSpec{
			AdapterType: volumeOptions.SCSIControllerType,
			DiskType:    volumeOptions.DiskFormat,
		},
		CapacityKb: int64(volumeOptions.CapacityKB),
	}
	task, err := vdm.CreateVirtualDisk(ctx, vmdkPath, datacenter, vmDiskSpec)
	if err != nil {
		framework.Logf("Failed to create virtual disk: %s. err: %+v", vmdkPath, err)
		return "", err
	}
	taskInfo, err := task.WaitForResult(ctx, nil)
	if err != nil {
		framework.Logf("Failed to complete virtual disk creation: %s. err: %+v", vmdkPath, err)
		return "", err
	}
	volumePath := taskInfo.Result.(string)
	canonicalDiskPath, err := getCanonicalVolumePath(ctx, datacenter, volumePath)
	if err != nil {
		return "", err
	}
	return canonicalDiskPath, nil
}
func ( vs * VSphere ) CreateVolume ( volumeOptions * VolumeOptions , dataCenterRef types . ManagedObjectReference ) ( string , error ) { ctx , cancel := context . WithCancel ( context . Background ( ) ) defer cancel ( ) Connect ( ctx , vs ) datacenter := object . NewDatacenter ( vs . Client . Client , dataCenterRef ) var ( err error directoryAlreadyPresent = false ) if datacenter == nil { return " " , fmt . Errorf ( " " ) } vs . initVolumeOptions ( volumeOptions ) finder := find . NewFinder ( datacenter . Client ( ) , false ) finder . SetDatacenter ( datacenter ) ds , err := finder . Datastore ( ctx , volumeOptions . Datastore ) if err != nil { return " " , fmt . Errorf ( " " , volumeOptions . Datastore , err ) } directoryPath := filepath . Clean ( ds . Path ( volDir ) ) + " " fileManager := object . NewFileManager ( ds . Client ( ) ) err = fileManager . MakeDirectory ( ctx , directoryPath , datacenter , false ) if err != nil { if soap . IsSoapFault ( err ) { soapFault := soap . ToSoapFault ( err ) if _ , ok := soapFault . VimFault ( ) . ( types . FileAlreadyExists ) ; ok { directoryAlreadyPresent = true framework . Logf ( " " , directoryPath ) } } if ! directoryAlreadyPresent { framework . Logf ( " " , directoryPath , err ) return " " , err } } framework . Logf ( " " , directoryPath ) vmdkPath := directoryPath + volumeOptions . Name + " " vdm := object . NewVirtualDiskManager ( ds . Client ( ) ) vmDiskSpec := & types . FileBackedVirtualDiskSpec { VirtualDiskSpec : types . VirtualDiskSpec { AdapterType : volumeOptions . SCSIControllerType , DiskType : volumeOptions . DiskFormat , } , CapacityKb : int64 ( volumeOptions . CapacityKB ) , } task , err := vdm . CreateVirtualDisk ( ctx , vmdkPath , datacenter , vmDiskSpec ) if err != nil { framework . Logf ( " " , vmdkPath , err ) return " " , err } taskInfo , err := task . WaitForResult ( ctx , nil ) if err != nil { framework . Logf ( " " , vmdkPath , err ) return " " , err } volumePath := taskInfo . Result . ( string ) canonicalDiskPath , err := getCanonicalVolumePath ( ctx , datacenter , volumePath ) if err != nil { return " " , err } return canonicalDiskPath , nil }
CreateVolume creates a vsphere volume using given volume parameters specified in VolumeOptions.
CreateVolume creates a vsphere volume using given volume parameters specified in VolumeOptions .
// Create a virtual disk manager // Create specification for new virtual disk // Create virtual disk