github.com/openshift/installer@v1.4.17/pkg/types/openstack/defaults/client.go (about)

     1  package defaults
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/gophercloud/gophercloud/v2"
     8  	"github.com/gophercloud/utils/v2/openstack/clientconfig"
     9  
    10  	"github.com/openshift/installer/pkg/version"
    11  )
    12  
    13  // getUserAgent generates a Gophercloud UserAgent to help cloud operators
    14  // disambiguate openshift-installer requests.
    15  func getUserAgent() (gophercloud.UserAgent, error) {
    16  	ua := gophercloud.UserAgent{}
    17  
    18  	version, err := version.Version()
    19  	if err != nil {
    20  		return ua, err
    21  	}
    22  
    23  	ua.Prepend(fmt.Sprintf("openshift-installer/%s", version))
    24  	return ua, nil
    25  }
    26  
    27  // NewServiceClient is a wrapper around Gophercloud's NewServiceClient that
    28  // ensures we consistently set a user-agent.
    29  func NewServiceClient(ctx context.Context, service string, opts *clientconfig.ClientOpts) (*gophercloud.ServiceClient, error) {
    30  	ua, err := getUserAgent()
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	client, err := clientconfig.NewServiceClient(ctx, service, opts)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	client.UserAgent = ua
    41  
    42  	return client, nil
    43  }