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

     1  package ovirt
     2  
     3  import (
     4  	"fmt"
     5  
     6  	ovirtsdk "github.com/ovirt/go-ovirt"
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  // getConnection is a convenience method to get a connection to ovirt api
    11  // form a Config Object.
    12  func getConnection(ovirtConfig Config) (*ovirtsdk.Connection, error) {
    13  	con, err := ovirtsdk.NewConnectionBuilder().
    14  		URL(ovirtConfig.URL).
    15  		Username(ovirtConfig.Username).
    16  		Password(ovirtConfig.Password).
    17  		CAFile(ovirtConfig.CAFile).
    18  		CACert([]byte(ovirtConfig.CABundle)).
    19  		Insecure(ovirtConfig.Insecure).
    20  		Build()
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  	return con, nil
    25  }
    26  
    27  // NewConnection returns a new client connection to oVirt's API endpoint.
    28  // It is the responsibility of the caller to close the connection.
    29  func NewConnection() (*ovirtsdk.Connection, error) {
    30  	ovirtConfig, err := NewConfig()
    31  	if err != nil {
    32  		return nil, errors.Wrap(err, "getting Engine configuration")
    33  	}
    34  	con, err := getConnection(ovirtConfig)
    35  	if err != nil {
    36  		return nil, errors.Wrap(err, "establishing Engine connection")
    37  	}
    38  	return con, nil
    39  }
    40  
    41  // FetchVNICProfileByClusterNetwork returns a list of profiles for the given cluster and network name.
    42  func FetchVNICProfileByClusterNetwork(con *ovirtsdk.Connection, clusterID string, networkName string) ([]*ovirtsdk.VnicProfile, error) {
    43  	clusterResponse, err := con.SystemService().ClustersService().ClusterService(clusterID).Get().Follow("networks").Send()
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	cluster, ok := clusterResponse.Cluster()
    49  	if !ok {
    50  		return nil, fmt.Errorf("failed to find cluster with id %s", clusterID)
    51  	}
    52  
    53  	networks, ok := cluster.Networks()
    54  	if !ok {
    55  		return nil, fmt.Errorf("no cluster networks for cluster %s [%s]", cluster.MustName(), clusterID)
    56  	}
    57  
    58  	for _, n := range networks.Slice() {
    59  		if n.MustName() != networkName {
    60  			continue
    61  		}
    62  
    63  		profilesGet, err := con.SystemService().NetworksService().NetworkService(n.MustId()).VnicProfilesService().List().Send()
    64  		if err != nil {
    65  			return nil, fmt.Errorf("failed to fetch vNic profiles")
    66  		}
    67  
    68  		return profilesGet.MustProfiles().Slice(), nil
    69  	}
    70  	return nil, fmt.Errorf("there are no vNic profiles for the given cluster ID %s and network name %s", clusterID, networkName)
    71  }
    72  
    73  // GetClusterName returns the name of the ovirt cluster with the specified Cluster ID
    74  func GetClusterName(con *ovirtsdk.Connection, clusterID string) (string, error) {
    75  	clusterResponse, err := con.SystemService().ClustersService().ClusterService(clusterID).Get().Send()
    76  	if err != nil {
    77  		return "", fmt.Errorf("failed to fetch cluster %s (%w)", clusterID, err)
    78  	}
    79  	cluster, ok := clusterResponse.Cluster()
    80  	if !ok {
    81  		return "", fmt.Errorf("failed to find cluster with id %s", clusterID)
    82  	}
    83  	return cluster.MustName(), nil
    84  }
    85  
    86  // FindHostsInCluster returns a list of the hosts that exists in the specified cluster
    87  func FindHostsInCluster(con *ovirtsdk.Connection, cName string) ([]*ovirtsdk.Host, error) {
    88  	res, err := con.SystemService().HostsService().
    89  		List().Search(fmt.Sprintf("cluster=%s", cName)).Send()
    90  	if err != nil {
    91  		return nil, fmt.Errorf("failed to fetch hosts for cluster %s (%w)", cName, err)
    92  	}
    93  	hosts, ok := res.Hosts()
    94  	if !ok {
    95  		return nil, fmt.Errorf("failed to find hosts in cluster %s", cName)
    96  	}
    97  	return hosts.Slice(), nil
    98  }