From 5a2b9f487fde04e3e653addbd0a9411388fc7731 Mon Sep 17 00:00:00 2001 From: angiecris Date: Fri, 9 Mar 2018 12:59:52 -0800 Subject: [PATCH] vic-machine validator bail out if session doesn't find a vm folder --- lib/install/validate/validator.go | 9 +++++++++ pkg/vsphere/session/session.go | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/lib/install/validate/validator.go b/lib/install/validate/validator.go index bb611e8113..382424b8a7 100644 --- a/lib/install/validate/validator.go +++ b/lib/install/validate/validator.go @@ -163,6 +163,15 @@ func NewValidator(ctx context.Context, input *data.Data) (*Validator, error) { op.Debugf("new validator Session.Populate: %s", err) } + if v.Session.VMFolder == nil { + op.Debugf("Failed to set validator session VM folder") + // it's possible that VMFolder is not set, but session.Populate doesn't return any error + if err == nil { + err = errors.New("validator Session.Populate: no datacenter folder (nil) is found") + } + return nil, err + } + if strings.Contains(sessionconfig.DatacenterPath, "/") { detail := "--target should only specify datacenter in the path (e.g. https://addr/datacenter) - specify cluster, resource pool, or folder with --compute-resource" op.Error(detail) diff --git a/pkg/vsphere/session/session.go b/pkg/vsphere/session/session.go index af53eb0ad5..6d89aeb512 100644 --- a/pkg/vsphere/session/session.go +++ b/pkg/vsphere/session/session.go @@ -359,6 +359,13 @@ func (s *Session) Populate(ctx context.Context) (*Session, error) { } else { op.Debugf("Cached folders: %s", s.DatacenterPath) } + // There could be cases where no error from Datacenter.Folders, but nil folder is returned. In this case we should bail out. + // It's also possible that there's an error, but a valid vm folder is returned + if folders == nil { + errs = append(errs, fmt.Sprintf("Nil folder returned when finding folders (%s)", s.DatacenterPath)) + return nil, errors.New(strings.Join(errs, "\n")) + } + s.VMFolder = folders.VmFolder }