forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_provider.go
64 lines (54 loc) · 1.72 KB
/
handler_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
package mbus
import (
"net/url"
"github.com/cloudfoundry/yagnats"
boshhandler "github.com/cloudfoundry/bosh-agent/handler"
boshplatform "github.com/cloudfoundry/bosh-agent/platform"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
boshdir "github.com/cloudfoundry/bosh-agent/settings/directories"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
"github.com/pivotal-golang/clock"
)
type HandlerProvider struct {
settingsService boshsettings.Service
logger boshlog.Logger
auditLogger boshplatform.AuditLogger
handler boshhandler.Handler
}
func NewHandlerProvider(
settingsService boshsettings.Service,
logger boshlog.Logger,
auditLogger boshplatform.AuditLogger,
) (p HandlerProvider) {
p.settingsService = settingsService
p.logger = logger
p.auditLogger = auditLogger
return
}
func (p HandlerProvider) Get(
platform boshplatform.Platform,
dirProvider boshdir.Provider,
) (handler boshhandler.Handler, err error) {
if p.handler != nil {
handler = p.handler
return
}
mbusURL, err := url.Parse(p.settingsService.GetSettings().Mbus)
if err != nil {
err = bosherr.WrapError(err, "Parsing handler URL")
return
}
switch mbusURL.Scheme {
case "nats":
natsClient := NewTimeoutNatsClient(yagnats.NewClient(), clock.NewClock())
handler = NewNatsHandler(p.settingsService, natsClient, p.logger, platform)
case "https":
mbusKeyPair := p.settingsService.GetSettings().Env.Bosh.Mbus.Cert
handler = NewHTTPSHandler(mbusURL, mbusKeyPair, p.logger, platform.GetFs(), dirProvider, p.auditLogger)
default:
err = bosherr.Errorf("Message Bus Handler with scheme %s could not be found", mbusURL.Scheme)
}
p.handler = handler
return
}