drivers/vmwarevsphere/vsphere.go
func (d *Driver) PreCreateCheck() error {
	log.Debug("Connecting to vSphere for pre-create checks...")
	// Create context
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	c, err := d.vsphereLogin(ctx)
	if err != nil {
		return err
	}
	defer c.Logout(ctx)

	// Create a new finder
	f := find.NewFinder(c.Client, true)

	dc, err := f.DatacenterOrDefault(ctx, d.Datacenter)
	if err != nil {
		return err
	}

	f.SetDatacenter(dc)

	// Folder
	if d.Folder != "" {
		// Find the specified Folder to create the VM in.
		folders, err := dc.Folders(ctx)
		if err != nil {
			return err
		}
		folder, err := f.Folder(ctx, fmt.Sprintf("%s/%s", folders.VmFolder.InventoryPath, d.Folder))
		// It's an error to not find the folder, or for the search itself to fail.
		if err != nil {
			// The search itself failed.
			return err
		}
		if folder == nil {
			return fmt.Errorf("failed to find VM Folder '%s'", d.Folder)
		}
	}

	if _, err := f.DatastoreOrDefault(ctx, d.Datastore); err != nil {
		return err
	}

	// TODO: if the user has both the VSPHERE_NETWORK defined and adds --vmwarevsphere-network
	//       both are used at the same time - probably should detect that and remove the one from ENV
	if len(d.Networks) == 0 {
		// machine assumes there will be a network
		d.Networks = append(d.Networks, "VM Network")
	}
	for _, netName := range d.Networks {
		if _, err := f.NetworkOrDefault(ctx, netName); err != nil {
			return err
		}
	}
	// d.Network needs to remain a string to cope with existing machines :/
	d.Network = d.Networks[0]

	var hs *object.HostSystem
	if d.HostSystem != "" {
		var err error
		hs, err = f.HostSystemOrDefault(ctx, d.HostSystem)
		if err != nil {
			return err
		}
	}

	// ResourcePool
	if d.Pool != "" {
		// Find specified Resource Pool
		if _, err := f.ResourcePool(ctx, d.Pool); err != nil {
			return err
		}
	} else if hs != nil {
		// Pick default Resource Pool for Host System
		if _, err := hs.ResourcePool(ctx); err != nil {
			return err
		}
	} else {
		// Pick the default Resource Pool for the Datacenter.
		if _, err := f.DefaultResourcePool(ctx); err != nil {
			return err
		}
	}

	return nil
}
func (d *Driver) PreCreateCheck() error {
	log.Debug("Connecting to vSphere for pre-create checks...")
	// Create context
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	c, err := d.vsphereLogin(ctx)
	if err != nil {
		return err
	}
	defer c.Logout(ctx)

	// Create a new finder
	f := find.NewFinder(c.Client, true)

	dc, err := f.DatacenterOrDefault(ctx, d.Datacenter)
	if err != nil {
		return err
	}

	f.SetDatacenter(dc)

	// Folder
	if d.Folder != "" {
		// Find the specified Folder to create the VM in.
		folders, err := dc.Folders(ctx)
		if err != nil {
			return err
		}
		folder, err := f.Folder(ctx, fmt.Sprintf("%s/%s", folders.VmFolder.InventoryPath, d.Folder))
		// It's an error to not find the folder, or for the search itself to fail.
		if err != nil {
			// The search itself failed.
			return err
		}
		if folder == nil {
			return fmt.Errorf("failed to find VM Folder '%s'", d.Folder)
		}
	}

	if _, err := f.DatastoreOrDefault(ctx, d.Datastore); err != nil {
		return err
	}

	// TODO: if the user has both the VSPHERE_NETWORK defined and adds --vmwarevsphere-network
	//       both are used at the same time - probably should detect that and remove the one from ENV
	if len(d.Networks) == 0 {
		// machine assumes there will be a network
		d.Networks = append(d.Networks, "VM Network")
	}
	for _, netName := range d.Networks {
		if _, err := f.NetworkOrDefault(ctx, netName); err != nil {
			return err
		}
	}
	// d.Network needs to remain a string to cope with existing machines :/
	d.Network = d.Networks[0]

	var hs *object.HostSystem
	if d.HostSystem != "" {
		var err error
		hs, err = f.HostSystemOrDefault(ctx, d.HostSystem)
		if err != nil {
			return err
		}
	}

	// ResourcePool
	if d.Pool != "" {
		// Find specified Resource Pool
		if _, err := f.ResourcePool(ctx, d.Pool); err != nil {
			return err
		}
	} else if hs != nil {
		// Pick default Resource Pool for Host System
		if _, err := hs.ResourcePool(ctx); err != nil {
			return err
		}
	} else {
		// Pick the default Resource Pool for the Datacenter.
		if _, err := f.DefaultResourcePool(ctx); err != nil {
			return err
		}
	}

	return nil
}
// PreCreateCheck checks that the machine creation process can be started safely.