forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_clients.go
81 lines (65 loc) · 2.4 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
package shared
import (
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
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"
)
// NewClients creates a new V2 Cloud Controller client and UAA client using the
// passed in config.
func NewClients(config command.Config, ui command.UI, targetCF bool) (*ccv2.Client, *uaa.Client, error) {
ccWrappers := []ccv2.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(2))
ccClient := ccv2.NewClient(ccv2.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, command.NoAPISetError{
BinaryName: config.BinaryName(),
}
}
_, err := ccClient.TargetCF(ccv2.TargetSettings{
URL: config.Target(),
SkipSSLValidation: config.SkipSSLValidation(),
DialTimeout: config.DialTimeout(),
})
if err != nil {
return nil, nil, HandleError(err)
}
uaaClient := uaa.NewClient(uaa.Config{
AppName: config.BinaryName(),
AppVersion: config.BinaryVersion(),
ClientID: config.UAAOAuthClient(),
ClientSecret: config.UAAOAuthClientSecret(),
DialTimeout: config.DialTimeout(),
SkipSSLValidation: config.SkipSSLValidation(),
URL: ccClient.TokenEndpoint(),
})
if verbose {
uaaClient.WrapConnection(uaaWrapper.NewRequestLogger(ui.RequestLoggerTerminalDisplay()))
}
if location != nil {
uaaClient.WrapConnection(uaaWrapper.NewRequestLogger(ui.RequestLoggerFileWriter(location)))
}
uaaClient.WrapConnection(uaaWrapper.NewUAAAuthentication(uaaClient, config))
uaaClient.WrapConnection(uaaWrapper.NewRetryRequest(2))
authWrapper.SetClient(uaaClient)
return ccClient, uaaClient, err
}