github.com/openshift/installer@v1.4.17/pkg/asset/installconfig/ovirt/storage.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  
    12  	"github.com/openshift/installer/pkg/types/ovirt"
    13  )
    14  
    15  func askStorage(c *ovirtsdk4.Connection, p *ovirt.Platform, clusterName string) error {
    16  	var storageDomainName string
    17  	var domainsForCluster = make(map[string]*ovirtsdk4.StorageDomain)
    18  	var domainNames []string
    19  	systemService := c.SystemService()
    20  	domainsSearch, err := systemService.StorageDomainsService().List().Search(fmt.Sprintf("cluster.name=%s", clusterName)).Send()
    21  	if err != nil {
    22  		return err
    23  	}
    24  	domains, ok := domainsSearch.StorageDomains()
    25  	if !ok {
    26  		return fmt.Errorf("there are no available storage domains for cluster %s", clusterName)
    27  	}
    28  
    29  	for _, domain := range domains.Slice() {
    30  		domainsForCluster[domain.MustName()] = domain
    31  		domainNames = append(domainNames, domain.MustName())
    32  	}
    33  	if err := survey.AskOne(
    34  		&survey.Select{
    35  			Message: "Storage domain",
    36  			Help:    "The storage domain will be used to create the disks of all the cluster nodes.",
    37  			Options: domainNames,
    38  		},
    39  		&storageDomainName,
    40  		survey.WithValidator(func(ans interface{}) error {
    41  			choice := ans.(core.OptionAnswer).Value
    42  			sort.Strings(domainNames)
    43  			i := sort.SearchStrings(domainNames, choice)
    44  			if i == len(domainNames) || domainNames[i] != choice {
    45  				return fmt.Errorf("invalid storage domain %s", choice)
    46  			}
    47  			domain, ok := domainsForCluster[choice]
    48  			if !ok {
    49  				return fmt.Errorf("cannot find storage domain id for the storage domain %s", storageDomainName)
    50  			}
    51  			p.StorageDomainID = domain.MustId()
    52  			return nil
    53  		}),
    54  	); err != nil {
    55  		return errors.Wrap(err, "failed UserInput")
    56  	}
    57  	return nil
    58  }