Skip to content

Commit

Permalink
Bug 1841118: add error handling to vSphere survey
Browse files Browse the repository at this point in the history
Adds error handling for vSphere survey questions. This is
particularly noticeable for handling user interrupts (CTRL C).
Without error handling the interrupts cause survey to skip to the
next question and will ultimately panic when no values have been
assigned to the pointers.
  • Loading branch information
patrickdillon committed May 28, 2020
1 parent 7f3456f commit f59b65f
Showing 1 changed file with 46 additions and 21 deletions.
67 changes: 46 additions & 21 deletions pkg/asset/installconfig/vsphere/vsphere.go
Expand Up @@ -73,7 +73,10 @@ func Platform() (*vsphere.Platform, error) {
return nil, err
}

apiVIP, ingressVIP := getVIPs()
apiVIP, ingressVIP, err := getVIPs()
if err != nil {
return nil, errors.Wrap(err, "failed to get VIPs")
}

platform := &vsphere.Platform{
Datacenter: dc,
Expand All @@ -94,35 +97,42 @@ func Platform() (*vsphere.Platform, error) {
// If creating the client fails, an error is returned.
func getClients() (*vCenterClient, error) {
var vcenter, username, password string
survey.Ask([]*survey.Question{

if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Input{
Message: "vCenter",
Help: "The hostname of the vCenter to be used for installation.",
},
Validate: survey.Required,
},
}, &vcenter)
}, &vcenter); err != nil {
return nil, err
}

survey.Ask([]*survey.Question{
if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Input{
Message: "Username",
Help: "The username to login to the vCenter.",
},
Validate: survey.Required,
},
}, &username)
}, &username); err != nil {
return nil, err
}

survey.Ask([]*survey.Question{
if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Password{
Message: "Password",
Help: "The password to login to the vCenter.",
},
Validate: survey.Required,
},
}, &password)
}, &password); err != nil {
return nil, err
}

// There is a noticeable delay when creating the client, so let the user know what's going on.
logrus.Infof("Connecting to vCenter %s", vcenter)
Expand Down Expand Up @@ -177,7 +187,7 @@ func getDataCenter(ctx context.Context, finder *find.Finder, client *vim25.Clien
sort.Strings(dataCenterChoices)

var selectedDataCenter string
survey.Ask([]*survey.Question{
if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Select{
Message: "Datacenter",
Expand All @@ -186,7 +196,10 @@ func getDataCenter(ctx context.Context, finder *find.Finder, client *vim25.Clien
},
Validate: survey.Required,
},
}, &selectedDataCenter)
}, &selectedDataCenter); err != nil {
return "", "", err
}

selectedDataCenterPath := formatPath(dataCenterPaths[selectedDataCenter])
return selectedDataCenter, selectedDataCenterPath, nil
}
Expand Down Expand Up @@ -216,7 +229,7 @@ func getCluster(ctx context.Context, path string, finder *find.Finder, client *v
sort.Strings(clusterChoices)

var selectedcluster string
survey.Ask([]*survey.Question{
if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Select{
Message: "Cluster",
Expand All @@ -225,7 +238,9 @@ func getCluster(ctx context.Context, path string, finder *find.Finder, client *v
},
Validate: survey.Required,
},
}, &selectedcluster)
}, &selectedcluster); err != nil {
return "", err
}

return selectedcluster, nil
}
Expand Down Expand Up @@ -255,7 +270,7 @@ func getDataStore(ctx context.Context, path string, finder *find.Finder, client
sort.Strings(dataStoreChoices)

var selectedDataStore string
survey.Ask([]*survey.Question{
if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Select{
Message: "Default Datastore",
Expand All @@ -264,7 +279,10 @@ func getDataStore(ctx context.Context, path string, finder *find.Finder, client
},
Validate: survey.Required,
},
}, &selectedDataStore)
}, &selectedDataStore); err != nil {
return "", err
}

return selectedDataStore, nil
}

Expand Down Expand Up @@ -300,7 +318,7 @@ func getNetwork(ctx context.Context, path string, finder *find.Finder, client *v
sort.Strings(networkChoices)

var selectednetwork string
survey.Ask([]*survey.Question{
if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Select{
Message: "Network",
Expand All @@ -309,15 +327,17 @@ func getNetwork(ctx context.Context, path string, finder *find.Finder, client *v
},
Validate: survey.Required,
},
}, &selectednetwork)
}, &selectednetwork); err != nil {
return "", err
}

return selectednetwork, nil
}

func getVIPs() (string, string) {
func getVIPs() (string, string, error) {
var apiVIP, ingressVIP string

survey.Ask([]*survey.Question{
if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Input{
Message: "Virtual IP Address for API",
Expand All @@ -327,9 +347,11 @@ func getVIPs() (string, string) {
return validate.IP((ans).(string))
}),
},
}, &apiVIP)
}, &apiVIP); err != nil {
return "", "", err
}

survey.Ask([]*survey.Question{
if err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Input{
Message: "Virtual IP Address for Ingress",
Expand All @@ -339,8 +361,11 @@ func getVIPs() (string, string) {
return validate.IP((ans).(string))
}),
},
}, &ingressVIP)
return apiVIP, ingressVIP
}, &ingressVIP); err != nil {
return "", "", err
}

return apiVIP, ingressVIP, nil
}

// formatPath is a helper function that appends "/..." to enable recursive
Expand Down

0 comments on commit f59b65f

Please sign in to comment.