github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/shared/new_v3_based_clients.go (about)

     1  package shared
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     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"
     9  	"code.cloudfoundry.org/cli/command/translatableerror"
    10  )
    11  
    12  // NewV3BasedClients creates a new V3 Cloud Controller client and UAA client using the
    13  // passed in config.
    14  func NewV3BasedClients(config command.Config, ui command.UI, targetCF bool) (*ccv3.Client, *uaa.Client, error) {
    15  	return NewV3BasedClientsWithAuthorizationEndpoint(config, ui, targetCF, "")
    16  }
    17  
    18  // NewV3BasedClientsWithAuthorizationEndpoint creates a new V3 Cloud Controller client
    19  // and UAA client using the passed in config.
    20  func NewV3BasedClientsWithAuthorizationEndpoint(config command.Config, ui command.UI, targetCF bool, authorizationEndpoint string) (*ccv3.Client, *uaa.Client, error) {
    21  	ccWrappers := []ccv3.ConnectionWrapper{}
    22  
    23  	verbose, location := config.Verbose()
    24  	if verbose {
    25  		ccWrappers = append(ccWrappers, ccWrapper.NewRequestLogger(ui.RequestLoggerTerminalDisplay()))
    26  	}
    27  	if location != nil {
    28  		ccWrappers = append(ccWrappers, ccWrapper.NewRequestLogger(ui.RequestLoggerFileWriter(location)))
    29  	}
    30  
    31  	authWrapper := ccWrapper.NewUAAAuthentication(nil, config)
    32  
    33  	ccWrappers = append(ccWrappers, authWrapper)
    34  	ccWrappers = append(ccWrappers, ccWrapper.NewRetryRequest(config.RequestRetryCount()))
    35  
    36  	ccClient := ccv3.NewClient(ccv3.Config{
    37  		AppName:            config.BinaryName(),
    38  		AppVersion:         config.BinaryVersion(),
    39  		JobPollingTimeout:  config.OverallPollingTimeout(),
    40  		JobPollingInterval: config.PollingInterval(),
    41  		Wrappers:           ccWrappers,
    42  	})
    43  
    44  	if !targetCF {
    45  		return ccClient, nil, nil
    46  	}
    47  
    48  	if config.Target() == "" {
    49  		return nil, nil, translatableerror.NoAPISetError{
    50  			BinaryName: config.BinaryName(),
    51  		}
    52  	}
    53  
    54  	_, err := ccClient.TargetCF(ccv3.TargetSettings{
    55  		URL:               config.Target(),
    56  		SkipSSLValidation: config.SkipSSLValidation(),
    57  		DialTimeout:       config.DialTimeout(),
    58  	})
    59  	if err != nil {
    60  		return nil, nil, err
    61  	}
    62  
    63  	if ccClient.UAA() == "" {
    64  		return nil, nil, translatableerror.UAAEndpointNotFoundError{}
    65  	}
    66  
    67  	uaaClient := uaa.NewClient(config)
    68  
    69  	if verbose {
    70  		uaaClient.WrapConnection(uaaWrapper.NewRequestLogger(ui.RequestLoggerTerminalDisplay()))
    71  	}
    72  	if location != nil {
    73  		uaaClient.WrapConnection(uaaWrapper.NewRequestLogger(ui.RequestLoggerFileWriter(location)))
    74  	}
    75  
    76  	uaaAuthWrapper := uaaWrapper.NewUAAAuthentication(uaaClient, config)
    77  	uaaClient.WrapConnection(uaaAuthWrapper)
    78  	uaaClient.WrapConnection(uaaWrapper.NewRetryRequest(config.RequestRetryCount()))
    79  
    80  	if authorizationEndpoint == "" {
    81  		authorizationEndpoint = ccClient.UAA()
    82  	}
    83  
    84  	err = uaaClient.SetupResources(authorizationEndpoint)
    85  	if err != nil {
    86  		return nil, nil, err
    87  	}
    88  
    89  	uaaAuthWrapper.SetClient(uaaClient)
    90  	authWrapper.SetClient(uaaClient)
    91  
    92  	return ccClient, uaaClient, nil
    93  }