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