forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_settings_source.go
61 lines (48 loc) · 1.53 KB
/
multi_settings_source.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
package infrastructure
import (
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type MultiSettingsSource struct {
sources []boshsettings.Source
selectedSSHKeySource boshsettings.Source
selectedSettingsSource boshsettings.Source
}
func NewMultiSettingsSource(sources ...boshsettings.Source) (boshsettings.Source, error) {
var err error
if len(sources) == 0 {
err = bosherr.Error("MultiSettingsSource requires to have at least one source")
}
return &MultiSettingsSource{sources: sources}, err
}
func (s *MultiSettingsSource) PublicSSHKeyForUsername(username string) (string, error) {
if s.selectedSSHKeySource != nil {
return s.selectedSSHKeySource.PublicSSHKeyForUsername(username)
}
var publicSSHKey string
var err error
for _, source := range s.sources {
publicSSHKey, err = source.PublicSSHKeyForUsername(username)
if err == nil {
s.selectedSSHKeySource = source
return publicSSHKey, nil
}
}
return "", bosherr.WrapErrorf(err, "Getting public SSH key for '%s'", username)
}
func (s *MultiSettingsSource) Settings() (boshsettings.Settings, error) {
if s.selectedSettingsSource != nil {
return s.selectedSettingsSource.Settings()
}
var settings boshsettings.Settings
var err error
for _, source := range s.sources {
settings, err = source.Settings()
if err == nil {
s.selectedSettingsSource = source
return settings, nil
}
}
return boshsettings.Settings{},
bosherr.WrapError(err, "Getting settings from all sources")
}