github.com/openshift/installer@v1.4.17/pkg/infrastructure/vsphere/clusterapi/folder.go (about)

     1  package clusterapi
     2  
     3  import (
     4  	"context"
     5  	"path"
     6  
     7  	"github.com/pkg/errors"
     8  	"github.com/vmware/govmomi/find"
     9  	"github.com/vmware/govmomi/object"
    10  	"sigs.k8s.io/cluster-api-provider-vsphere/pkg/session"
    11  )
    12  
    13  func folderExists(ctx context.Context, dir string, session *session.Session) (*object.Folder, error) {
    14  	/* scenarios:
    15  	 * 1. folder exists and returns
    16  	 * 2. folder does not exist and err and folder are nil
    17  	 * 3. finder.Folder fails and returns folder nil and error
    18  	 */
    19  	var notFoundError *find.NotFoundError
    20  	folder, err := session.Finder.Folder(ctx, dir)
    21  
    22  	// scenario two
    23  	if folder == nil && errors.As(err, &notFoundError) {
    24  		return nil, nil
    25  	}
    26  	// scenario three
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	// scenario one
    31  	return folder, nil
    32  }
    33  
    34  func createFolder(ctx context.Context, fullpath string, session *session.Session) (*object.Folder, error) {
    35  	var folder *object.Folder
    36  	var err error
    37  
    38  	dir := path.Dir(fullpath)
    39  	base := path.Base(fullpath)
    40  
    41  	// if folder is nil the fullpath does not exist
    42  	if folder, err = folderExists(ctx, dir, session); err == nil && folder == nil {
    43  		folder, err = createFolder(ctx, dir, session)
    44  		if err != nil {
    45  			return nil, err
    46  		}
    47  	}
    48  
    49  	if folder != nil && err == nil {
    50  		return folder.CreateFolder(ctx, base)
    51  	}
    52  	return folder, err
    53  }