forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
59 lines (49 loc) · 1.85 KB
/
client.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
package helpers
import (
"encoding/json"
"io/ioutil"
"path/filepath"
"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/util/configv3"
. "github.com/onsi/gomega"
)
func CreateV2Client(homeDir string) *ccv2.Client {
file, err := ioutil.ReadFile(filepath.Join(homeDir, ".cf", "config.json"))
Expect(err).ToNot(HaveOccurred())
var config configv3.Config
err = json.Unmarshal(file, &config.ConfigFile)
Expect(err).ToNot(HaveOccurred())
ccWrappers := []ccv2.ConnectionWrapper{}
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,
})
_, err = ccClient.TargetCF(ccv2.TargetSettings{
URL: config.Target(),
SkipSSLValidation: config.SkipSSLValidation(),
DialTimeout: config.DialTimeout(),
})
Expect(err).ToNot(HaveOccurred())
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(),
})
uaaClient.WrapConnection(uaaWrapper.NewUAAAuthentication(uaaClient, &config))
uaaClient.WrapConnection(uaaWrapper.NewRetryRequest(2))
authWrapper.SetClient(uaaClient)
return ccClient
}