github.com/openshift/installer@v1.4.17/pkg/asset/installconfig/ovirt/cluster.go (about)

     1  package ovirt
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  
     7  	"github.com/AlecAivazis/survey/v2"
     8  	"github.com/AlecAivazis/survey/v2/core"
     9  	ovirtsdk4 "github.com/ovirt/go-ovirt"
    10  	"github.com/pkg/errors"
    11  	"github.com/sirupsen/logrus"
    12  
    13  	"github.com/openshift/installer/pkg/types/ovirt"
    14  )
    15  
    16  func askCluster(c *ovirtsdk4.Connection, p *ovirt.Platform) (string, error) {
    17  	var clusterName string
    18  	var clusterByNames = make(map[string]*ovirtsdk4.Cluster)
    19  	var clusterNames []string
    20  	systemService := c.SystemService()
    21  
    22  	dcResp, err := datacentersAvailable(c, "")
    23  	if err != nil {
    24  		return "", err
    25  	}
    26  
    27  	datacenters := dcResp.MustDataCenters()
    28  	for _, dc := range datacenters.Slice() {
    29  		dcService := systemService.DataCentersService().DataCenterService(dc.MustId())
    30  		logrus.Debug("Datacenter:", dc.MustName())
    31  		clusters, err := dcService.ClustersService().List().Send()
    32  		if err != nil {
    33  			return "", errors.Wrap(err, "failed to list clusters")
    34  		}
    35  		clusterSlice := clusters.MustClusters()
    36  		for _, cluster := range clusterSlice.Slice() {
    37  			logrus.Debug("\tcluster:", cluster.MustName())
    38  			clusterByNames[cluster.MustName()] = cluster
    39  			clusterNames = append(clusterNames, cluster.MustName())
    40  		}
    41  	}
    42  	if err := survey.AskOne(
    43  		&survey.Select{
    44  			Message: "Cluster",
    45  			Help:    "The Cluster where the VMs will be created.",
    46  			Options: clusterNames,
    47  		},
    48  		&clusterName,
    49  		survey.WithValidator(func(ans interface{}) error {
    50  			choice := ans.(core.OptionAnswer).Value
    51  			sort.Strings(clusterNames)
    52  			i := sort.SearchStrings(clusterNames, choice)
    53  			if i == len(clusterNames) || clusterNames[i] != choice {
    54  				return fmt.Errorf("invalid cluster %s", choice)
    55  			}
    56  			cl, ok := clusterByNames[choice]
    57  			if !ok {
    58  				return fmt.Errorf("cannot find a cluster id for the cluster name %s", clusterName)
    59  			}
    60  			p.ClusterID = cl.MustId()
    61  			return nil
    62  		}),
    63  	); err != nil {
    64  		return clusterName, errors.Wrap(err, "failed UserInput")
    65  	}
    66  	return clusterName, nil
    67  }