-
Notifications
You must be signed in to change notification settings - Fork 115
/
instance_metadata_settings_source.go
90 lines (75 loc) · 2.69 KB
/
instance_metadata_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package infrastructure
import (
"encoding/json"
"fmt"
"time"
boshplatform "github.com/cloudfoundry/bosh-agent/platform"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type InstanceMetadataSettingsSource struct {
metadataHost string
metadataHeaders map[string]string
settingsPath string
platform boshplatform.Platform
logger boshlog.Logger
logTag string
metadataService DynamicMetadataService
}
func NewInstanceMetadataSettingsSource(
metadataHost string,
metadataHeaders map[string]string,
settingsPath string,
platform boshplatform.Platform,
logger boshlog.Logger,
) *InstanceMetadataSettingsSource {
logTag := "InstanceMetadataSettingsSource"
return &InstanceMetadataSettingsSource{
metadataHost: metadataHost,
metadataHeaders: metadataHeaders,
settingsPath: settingsPath,
platform: platform,
logger: logger,
logTag: logTag,
// The HTTPMetadataService provides more functionality than we need (like custom DNS), so we
// pass zero values to the New function and only use its GetValueAtPath method.
metadataService: NewHTTPMetadataService(metadataHost, metadataHeaders, "", "", "", nil, platform, logger),
}
}
func NewInstanceMetadataSettingsSourceWithoutRetryDelay(
metadataHost string,
metadataHeaders map[string]string,
settingsPath string,
platform boshplatform.Platform,
logger boshlog.Logger,
) *InstanceMetadataSettingsSource {
logTag := "InstanceMetadataSettingsSource"
return &InstanceMetadataSettingsSource{
metadataHost: metadataHost,
metadataHeaders: metadataHeaders,
settingsPath: settingsPath,
platform: platform,
logger: logger,
logTag: logTag,
// The HTTPMetadataService provides more functionality than we need (like custom DNS), so we
// pass zero values to the New function and only use its GetValueAtPath method.
metadataService: NewHTTPMetadataServiceWithCustomRetryDelay(metadataHost, metadataHeaders, "", "", "", nil, platform, logger, 0*time.Second),
}
}
func (s InstanceMetadataSettingsSource) PublicSSHKeyForUsername(string) (string, error) {
return "", nil
}
func (s *InstanceMetadataSettingsSource) Settings() (boshsettings.Settings, error) {
var settings boshsettings.Settings
contents, err := s.metadataService.GetValueAtPath(s.settingsPath)
if err != nil {
return settings, bosherr.WrapError(err, fmt.Sprintf("Reading settings from instance metadata at path %q", s.settingsPath))
}
err = json.Unmarshal([]byte(contents), &settings)
if err != nil {
return settings, bosherr.WrapErrorf(
err, "Parsing instance metadata settings from %q", contents)
}
return settings, nil
}