-
Notifications
You must be signed in to change notification settings - Fork 115
/
file_metadata_service.go
161 lines (123 loc) · 4 KB
/
file_metadata_service.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package infrastructure
import (
"encoding/json"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type PublicKeyContent struct {
PublicKey string `json:"public_key"`
}
type fileMetadataService struct {
metaDataFilePath string
userDataFilePath string
settingsFilePath string
fs boshsys.FileSystem
logger boshlog.Logger
logTag string
}
func NewFileMetadataService(
metaDataFilePath string,
userDataFilePath string,
settingsFilePath string,
fs boshsys.FileSystem,
logger boshlog.Logger,
) MetadataService {
return fileMetadataService{
metaDataFilePath: metaDataFilePath,
userDataFilePath: userDataFilePath,
settingsFilePath: settingsFilePath,
fs: fs,
logTag: "fileMetadataService",
logger: logger,
}
}
func (ms fileMetadataService) Load() error {
return nil
}
func (ms fileMetadataService) GetPublicKey() (string, error) {
var p PublicKeyContent
contents, err := ms.fs.ReadFile(ms.settingsFilePath)
if err != nil {
return "", bosherr.WrapError(err, "Reading metadata file")
}
err = json.Unmarshal([]byte(contents), &p)
if err != nil {
return "", bosherr.WrapError(err, "Unmarshalling metadata")
}
return p.PublicKey, nil
}
func (ms fileMetadataService) GetInstanceID() (string, error) {
var metadata MetadataContentsType
contents, err := ms.fs.ReadFile(ms.metaDataFilePath)
if err != nil {
return "", bosherr.WrapError(err, "Reading metadata file")
}
err = json.Unmarshal([]byte(contents), &metadata)
if err != nil {
return "", bosherr.WrapError(err, "Unmarshalling metadata")
}
ms.logger.Debug(ms.logTag, "Read metadata '%#v'", metadata)
return metadata.InstanceID, nil
}
func (ms fileMetadataService) GetServerName() (string, error) {
var userData UserDataContentsType
contents, err := ms.fs.ReadFile(ms.userDataFilePath)
if err != nil {
return "", bosherr.WrapError(err, "Reading user data")
}
err = json.Unmarshal([]byte(contents), &userData)
if err != nil {
return "", bosherr.WrapError(err, "Unmarshalling user data")
}
ms.logger.Debug(ms.logTag, "Read user data '%#v'", userData)
return userData.Server.Name, nil
}
func (ms fileMetadataService) GetRegistryEndpoint() (string, error) {
var userData UserDataContentsType
contents, err := ms.fs.ReadFile(ms.userDataFilePath)
if err != nil {
// Older versions of bosh-warden-cpi placed
// full settings file at a specific location.
return ms.settingsFilePath, nil
}
err = json.Unmarshal([]byte(contents), &userData)
if err != nil {
return "", bosherr.WrapError(err, "Unmarshalling user data")
}
ms.logger.Debug(ms.logTag, "Read user data '%#v'", userData)
return userData.Registry.Endpoint, nil
}
func (ms fileMetadataService) GetNetworks() (boshsettings.Networks, error) {
var userData UserDataContentsType
contents, err := ms.fs.ReadFile(ms.userDataFilePath)
if err != nil {
return nil, bosherr.WrapError(err, "Reading user data")
}
err = json.Unmarshal([]byte(contents), &userData)
if err != nil {
return nil, bosherr.WrapError(err, "Unmarshalling user data")
}
ms.logger.Debug(ms.logTag, "Read user data '%#v'", userData)
return userData.Networks, nil
}
func (ms fileMetadataService) GetSettings() (boshsettings.Settings, error) {
var userData UserDataContentsType
contents, err := ms.fs.ReadFile(ms.userDataFilePath)
if err != nil {
return boshsettings.Settings{}, bosherr.WrapError(err, "Reading user data")
}
err = json.Unmarshal([]byte(contents), &userData)
if err != nil {
return boshsettings.Settings{}, bosherr.WrapError(err, "Unmarshalling user data")
}
ms.logger.Debug(ms.logTag, "Read user data '%#v'", userData)
if userData.Settings.AgentID == "" {
return boshsettings.Settings{}, bosherr.Error("Metadata does not provide settings")
}
return userData.Settings, nil
}
func (ms fileMetadataService) IsAvailable() bool {
return ms.fs.FileExists(ms.settingsFilePath)
}