github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/command/v6/shared/new_clients.go (about)

     1  package shared
     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/router"
     7  	routerWrapper "code.cloudfoundry.org/cli/api/router/wrapper"
     8  	"code.cloudfoundry.org/cli/api/uaa"
     9  	uaaWrapper "code.cloudfoundry.org/cli/api/uaa/wrapper"
    10  	"code.cloudfoundry.org/cli/command"
    11  	"code.cloudfoundry.org/cli/command/translatableerror"
    12  )
    13  
    14  func NewRouterClient(config command.Config, ui command.UI, uaaClient *uaa.Client) (*router.Client, error) {
    15  	routerConfig := router.Config{
    16  		AppName:    config.BinaryName(),
    17  		AppVersion: config.BinaryVersion(),
    18  		ConnectionConfig: router.ConnectionConfig{
    19  			DialTimeout:       config.DialTimeout(),
    20  			SkipSSLValidation: config.SkipSSLValidation(),
    21  		},
    22  		RoutingEndpoint: config.RoutingEndpoint(),
    23  	}
    24  
    25  	routerWrappers := []router.ConnectionWrapper{}
    26  
    27  	verbose, location := config.Verbose()
    28  
    29  	if verbose {
    30  		routerWrappers = append(routerWrappers, routerWrapper.NewRequestLogger(ui.RequestLoggerTerminalDisplay()))
    31  	}
    32  
    33  	if location != nil {
    34  		routerWrappers = append(routerWrappers, routerWrapper.NewRequestLogger(ui.RequestLoggerFileWriter(location)))
    35  	}
    36  
    37  	authWrapper := routerWrapper.NewUAAAuthentication(uaaClient, config)
    38  	errorWrapper := routerWrapper.NewErrorWrapper()
    39  
    40  	routerWrappers = append(routerWrappers, authWrapper, errorWrapper)
    41  	routerConfig.Wrappers = routerWrappers
    42  
    43  	routerClient := router.NewClient(routerConfig)
    44  	return routerClient, nil
    45  }
    46  
    47  func GetNewClientsAndConnectToCF(config command.Config, ui command.UI) (*ccv2.Client, *uaa.Client, error) {
    48  	var err error
    49  
    50  	ccClient, authWrapper := NewWrappedCloudControllerClient(config, ui)
    51  
    52  	ccClient, err = connectToCF(config, ui, ccClient)
    53  	if err != nil {
    54  		return nil, nil, err
    55  	}
    56  	uaaClient, err := newWrappedUAAClient(config, ui, ccClient, authWrapper)
    57  
    58  	return ccClient, uaaClient, err
    59  }
    60  
    61  func NewWrappedCloudControllerClient(config command.Config, ui command.UI) (*ccv2.Client, *ccWrapper.UAAAuthentication) {
    62  	ccWrappers := []ccv2.ConnectionWrapper{}
    63  
    64  	verbose, location := config.Verbose()
    65  	if verbose {
    66  		ccWrappers = append(ccWrappers, ccWrapper.NewRequestLogger(ui.RequestLoggerTerminalDisplay()))
    67  	}
    68  
    69  	if location != nil {
    70  		ccWrappers = append(ccWrappers, ccWrapper.NewRequestLogger(ui.RequestLoggerFileWriter(location)))
    71  	}
    72  
    73  	authWrapper := ccWrapper.NewUAAAuthentication(nil, config)
    74  
    75  	ccWrappers = append(ccWrappers, authWrapper)
    76  	ccWrappers = append(ccWrappers, ccWrapper.NewRetryRequest(config.RequestRetryCount()))
    77  
    78  	ccClient := ccv2.NewClient(ccv2.Config{
    79  		AppName:            config.BinaryName(),
    80  		AppVersion:         config.BinaryVersion(),
    81  		JobPollingTimeout:  config.OverallPollingTimeout(),
    82  		JobPollingInterval: config.PollingInterval(),
    83  		Wrappers:           ccWrappers,
    84  	})
    85  	return ccClient, authWrapper
    86  }
    87  
    88  func newWrappedUAAClient(config command.Config, ui command.UI, ccClient *ccv2.Client, authWrapper *ccWrapper.UAAAuthentication) (*uaa.Client, error) {
    89  	var err error
    90  	verbose, location := config.Verbose()
    91  
    92  	uaaClient := uaa.NewClient(config)
    93  
    94  	if verbose {
    95  		uaaClient.WrapConnection(uaaWrapper.NewRequestLogger(ui.RequestLoggerTerminalDisplay()))
    96  	}
    97  	if location != nil {
    98  		uaaClient.WrapConnection(uaaWrapper.NewRequestLogger(ui.RequestLoggerFileWriter(location)))
    99  	}
   100  
   101  	uaaAuthWrapper := uaaWrapper.NewUAAAuthentication(nil, config)
   102  	uaaClient.WrapConnection(uaaAuthWrapper)
   103  	uaaClient.WrapConnection(uaaWrapper.NewRetryRequest(config.RequestRetryCount()))
   104  
   105  	err = uaaClient.SetupResources(ccClient.AuthorizationEndpoint())
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  	uaaAuthWrapper.SetClient(uaaClient)
   110  	authWrapper.SetClient(uaaClient)
   111  
   112  	return uaaClient, nil
   113  }
   114  
   115  func connectToCF(config command.Config, ui command.UI, ccClient *ccv2.Client) (*ccv2.Client, error) {
   116  	if config.Target() == "" {
   117  		return nil, translatableerror.NoAPISetError{
   118  			BinaryName: config.BinaryName(),
   119  		}
   120  	}
   121  
   122  	_, err := ccClient.TargetCF(ccv2.TargetSettings{
   123  		URL:               config.Target(),
   124  		SkipSSLValidation: config.SkipSSLValidation(),
   125  		DialTimeout:       config.DialTimeout(),
   126  	})
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  	if err = command.WarnIfAPIVersionBelowSupportedMinimum(ccClient.APIVersion(), ui); err != nil {
   131  		return nil, err
   132  	}
   133  	if ccClient.AuthorizationEndpoint() == "" {
   134  		return nil, translatableerror.AuthorizationEndpointNotFoundError{}
   135  	}
   136  
   137  	return ccClient, err
   138  }