github.com/loafoe/cli@v7.1.0+incompatible/integration/helpers/client.go (about)

     1  package helpers
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
     5  	ccWrapper "code.cloudfoundry.org/cli/api/cloudcontroller/wrapper"
     6  	"code.cloudfoundry.org/cli/api/uaa"
     7  	uaaWrapper "code.cloudfoundry.org/cli/api/uaa/wrapper"
     8  	"code.cloudfoundry.org/cli/command/translatableerror"
     9  	"code.cloudfoundry.org/cli/util/configv3"
    10  )
    11  
    12  // CreateCCV2Client constructs a client object able to communicate with the
    13  // cloudcontroller V2 API.
    14  func CreateCCV2Client() (*ccv2.Client, error) {
    15  	config, err := configv3.LoadConfig(configv3.FlagOverride{})
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  
    20  	ccWrappers := []ccv2.ConnectionWrapper{}
    21  	authWrapper := ccWrapper.NewUAAAuthentication(nil, config)
    22  
    23  	ccWrappers = append(ccWrappers, authWrapper)
    24  	ccWrappers = append(ccWrappers, ccWrapper.NewRetryRequest(config.RequestRetryCount()))
    25  
    26  	ccClient := ccv2.NewClient(ccv2.Config{
    27  		AppName:            config.BinaryName(),
    28  		AppVersion:         config.BinaryVersion(),
    29  		JobPollingTimeout:  config.OverallPollingTimeout(),
    30  		JobPollingInterval: config.PollingInterval(),
    31  		Wrappers:           ccWrappers,
    32  	})
    33  
    34  	_, err = ccClient.TargetCF(ccv2.TargetSettings{
    35  		URL:               config.Target(),
    36  		SkipSSLValidation: config.SkipSSLValidation(),
    37  		DialTimeout:       config.DialTimeout(),
    38  	})
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	if ccClient.AuthorizationEndpoint() == "" {
    44  		return nil, translatableerror.AuthorizationEndpointNotFoundError{}
    45  	}
    46  
    47  	uaaClient := uaa.NewClient(config)
    48  
    49  	uaaAuthWrapper := uaaWrapper.NewUAAAuthentication(nil, config)
    50  	uaaClient.WrapConnection(uaaAuthWrapper)
    51  	uaaClient.WrapConnection(uaaWrapper.NewRetryRequest(config.RequestRetryCount()))
    52  
    53  	err = uaaClient.SetupResources(ccClient.AuthorizationEndpoint())
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	uaaAuthWrapper.SetClient(uaaClient)
    59  	authWrapper.SetClient(uaaClient)
    60  	return ccClient, nil
    61  }