vsphere/resource_vsphere_virtual_disk.go
func searchForDirectory(client *govmomi.Client, datacenter string, datastore string, directoryPath string) error {
	log.Printf("[DEBUG] Searching for Directory")
	finder := find.NewFinder(client.Client, true)

	dc, err := getDatacenter(client, datacenter)
	if err != nil {
		return fmt.Errorf("Error finding Datacenter: %s: %s", datacenter, err)
	}
	finder = finder.SetDatacenter(dc)

	ds, err := finder.Datastore(context.TODO(), datastore)
	if err != nil {
		return fmt.Errorf("Error finding Datastore: %s: %s", datastore, err)
	}

	ctx := context.TODO()
	b, err := ds.Browser(ctx)
	if err != nil {
		return err
	}

	spec := types.HostDatastoreBrowserSearchSpec{
		Query: []types.BaseFileQuery{&types.FolderFileQuery{}},
		Details: &types.FileQueryFlags{
			FileSize:     true,
			FileType:     true,
			Modification: true,
			FileOwner:    types.NewBool(true),
		},
		MatchPattern: []string{path.Base(directoryPath)},
	}

	dsPath := ds.Path(path.Dir(directoryPath))
	task, err := b.SearchDatastore(context.TODO(), dsPath, &spec)

	if err != nil {
		log.Printf("[DEBUG] searchForDirectory - could not search datastore for: %v", directoryPath)
		return err
	}

	info, err := task.WaitForResult(context.TODO(), nil)
	if err != nil {
		if info == nil || info.Error != nil {
			_, ok := info.Error.Fault.(*types.FileNotFound)
			if ok {
				log.Printf("[DEBUG] searchForDirectory - could not find: %v", directoryPath)
				return nil
			}
		}

		log.Printf("[DEBUG] searchForDirectory - could not search datastore for: %v", directoryPath)
		return err
	}

	res := info.Result.(types.HostDatastoreBrowserSearchResults)
	log.Printf("[DEBUG] num results: %d", len(res.File))
	if len(res.File) == 0 {
		log.Printf("[DEBUG] searchForDirectory - could not find: %v", directoryPath)
		return nil
	}

	if len(res.File) != 1 {
		return errors.New("Datastore search did not return exactly one result")
	}

	fileInfo := res.File[0]
	log.Printf("[DEBUG] searchForDirectory - fileinfo: %#v", fileInfo)

	return nil
}
func searchForDirectory(client *govmomi.Client, datacenter string, datastore string, directoryPath string) error {
	log.Printf("[DEBUG] Searching for Directory")
	finder := find.NewFinder(client.Client, true)

	dc, err := getDatacenter(client, datacenter)
	if err != nil {
		return fmt.Errorf("Error finding Datacenter: %s: %s", datacenter, err)
	}
	finder = finder.SetDatacenter(dc)

	ds, err := finder.Datastore(context.TODO(), datastore)
	if err != nil {
		return fmt.Errorf("Error finding Datastore: %s: %s", datastore, err)
	}

	ctx := context.TODO()
	b, err := ds.Browser(ctx)
	if err != nil {
		return err
	}

	spec := types.HostDatastoreBrowserSearchSpec{
		Query: []types.BaseFileQuery{&types.FolderFileQuery{}},
		Details: &types.FileQueryFlags{
			FileSize:     true,
			FileType:     true,
			Modification: true,
			FileOwner:    types.NewBool(true),
		},
		MatchPattern: []string{path.Base(directoryPath)},
	}

	dsPath := ds.Path(path.Dir(directoryPath))
	task, err := b.SearchDatastore(context.TODO(), dsPath, &spec)

	if err != nil {
		log.Printf("[DEBUG] searchForDirectory - could not search datastore for: %v", directoryPath)
		return err
	}

	info, err := task.WaitForResult(context.TODO(), nil)
	if err != nil {
		if info == nil || info.Error != nil {
			_, ok := info.Error.Fault.(*types.FileNotFound)
			if ok {
				log.Printf("[DEBUG] searchForDirectory - could not find: %v", directoryPath)
				return nil
			}
		}

		log.Printf("[DEBUG] searchForDirectory - could not search datastore for: %v", directoryPath)
		return err
	}

	res := info.Result.(types.HostDatastoreBrowserSearchResults)
	log.Printf("[DEBUG] num results: %d", len(res.File))
	if len(res.File) == 0 {
		log.Printf("[DEBUG] searchForDirectory - could not find: %v", directoryPath)
		return nil
	}

	if len(res.File) != 1 {
		return errors.New("Datastore search did not return exactly one result")
	}

	fileInfo := res.File[0]
	log.Printf("[DEBUG] searchForDirectory - fileinfo: %#v", fileInfo)

	return nil
}
func searchForDirectory ( client * govmomi . Client , datacenter string , datastore string , directoryPath string ) error { log . Printf ( " " ) finder := find . NewFinder ( client . Client , true ) dc , err := getDatacenter ( client , datacenter ) if err != nil { return fmt . Errorf ( " " , datacenter , err ) } finder = finder . SetDatacenter ( dc ) ds , err := finder . Datastore ( context . TODO ( ) , datastore ) if err != nil { return fmt . Errorf ( " " , datastore , err ) } ctx := context . TODO ( ) b , err := ds . Browser ( ctx ) if err != nil { return err } spec := types . HostDatastoreBrowserSearchSpec { Query : [ ] types . BaseFileQuery { & types . FolderFileQuery { } } , Details : & types . FileQueryFlags { FileSize : true , FileType : true , Modification : true , FileOwner : types . NewBool ( true ) , } , MatchPattern : [ ] string { path . Base ( directoryPath ) } , } dsPath := ds . Path ( path . Dir ( directoryPath ) ) task , err := b . SearchDatastore ( context . TODO ( ) , dsPath , & spec ) if err != nil { log . Printf ( " " , directoryPath ) return err } info , err := task . WaitForResult ( context . TODO ( ) , nil ) if err != nil { if info == nil || info . Error != nil { _ , ok := info . Error . Fault . ( * types . FileNotFound ) if ok { log . Printf ( " " , directoryPath ) return nil } } log . Printf ( " " , directoryPath ) return err } res := info . Result . ( types . HostDatastoreBrowserSearchResults ) log . Printf ( " " , len ( res . File ) ) if len ( res . File ) == 0 { log . Printf ( " " , directoryPath ) return nil } if len ( res . File ) != 1 { return errors . New ( " " ) } fileInfo := res . File [ 0 ] log . Printf ( " " , fileInfo ) return nil }
// Searches for the presence of a directory path.
Searches for the presence of a directory path .