-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathfactory.go
65 lines (52 loc) · 1.41 KB
/
factory.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
package uaa
import (
"fmt"
"net"
"net/url"
"time"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
"github.com/cloudfoundry/bosh-utils/httpclient"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type Factory struct {
logTag string
logger boshlog.Logger
}
func NewFactory(logger boshlog.Logger) Factory {
return Factory{
logTag: "uaa.Factory",
logger: logger,
}
}
func (f Factory) New(config Config) (UAA, error) {
err := config.Validate()
if err != nil {
return UAAImpl{}, bosherr.WrapErrorf(
err, "Validating UAA connection config")
}
client, err := f.httpClient(config)
if err != nil {
return UAAImpl{}, err
}
return UAAImpl{client: client}, nil
}
func (f Factory) httpClient(config Config) (Client, error) {
certPool, err := config.CACertPool()
if err != nil {
return Client{}, err
}
if certPool == nil {
f.logger.Debug(f.logTag, "Using default root CAs")
} else {
f.logger.Debug(f.logTag, "Using custom root CAs")
}
rawClient := httpclient.CreateDefaultClient(certPool)
retryClient := httpclient.NewNetworkSafeRetryClient(rawClient, 5, 500*time.Millisecond, f.logger)
httpClient := httpclient.NewHTTPClient(retryClient, f.logger)
endpoint := url.URL{
Scheme: "https",
Host: net.JoinHostPort(config.Host, fmt.Sprintf("%d", config.Port)),
Path: config.Path,
}
return NewClient(endpoint.String(), config.Client, config.ClientSecret, httpClient, f.logger), nil
}