forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
71 lines (60 loc) · 1.51 KB
/
provider.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
package monit
import (
"net/http"
"time"
boshplatform "github.com/cloudfoundry/bosh-agent/platform"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
"github.com/cloudfoundry/bosh-utils/httpclient"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
const (
shortRetryStrategyAttempts = uint(20)
longRetryStrategyAttempts = uint(300)
retryDelay = 1 * time.Second
monitHost = "127.0.0.1:2822"
)
type ClientProvider interface {
Get() (Client, error)
}
type clientProvider struct {
platform boshplatform.Platform
logger boshlog.Logger
shortHTTPClient HTTPClient
longHTTPClient HTTPClient
}
func NewProvider(platform boshplatform.Platform, logger boshlog.Logger) ClientProvider {
httpClient := http.DefaultClient
shortHTTPClient := httpclient.NewRetryClient(
httpClient,
shortRetryStrategyAttempts,
retryDelay,
logger,
)
longHTTPClient := NewMonitRetryClient(
httpClient,
longRetryStrategyAttempts,
shortRetryStrategyAttempts,
retryDelay,
logger,
)
return clientProvider{
platform: platform,
logger: logger,
shortHTTPClient: shortHTTPClient,
longHTTPClient: longHTTPClient,
}
}
func (p clientProvider) Get() (client Client, err error) {
monitUser, monitPassword, err := p.platform.GetMonitCredentials()
if err != nil {
return nil, bosherr.WrapError(err, "Getting monit credentials")
}
return NewHTTPClient(
monitHost,
monitUser,
monitPassword,
p.shortHTTPClient,
p.longHTTPClient,
p.logger,
), nil
}