/
fs_config.go
225 lines (168 loc) · 4.94 KB
/
fs_config.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package config
import (
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
"gopkg.in/yaml.v2"
)
/*
current_environment: https://192.168.50.4:25555
environments:
- url: https://192.168.50.4:25555
ca_cert: |...
current_deployment: test
username: admin
password: admin
*/
type FSConfig struct {
path string
fs boshsys.FileSystem
schema fsConfigSchema
}
type fsConfigSchema struct {
// Environment is always a full URL
CurrentEnvironment string `yaml:"current_environment,omitempty"`
Environments []fsConfigSchema_Environment `yaml:"environments"`
}
type fsConfigSchema_Environment struct {
URL string `yaml:"url"`
CACert string `yaml:"ca_cert,omitempty"`
Alias string `yaml:"alias,omitempty"`
CurrentDeployment string `yaml:"current_deployment,omitempty"`
// Auth
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
RefreshToken string `yaml:"refresh_token,omitempty"`
}
func NewFSConfigFromPath(path string, fs boshsys.FileSystem) (FSConfig, error) {
var schema fsConfigSchema
absPath, err := fs.ExpandPath(path)
if err != nil {
return FSConfig{}, err
}
if fs.FileExists(absPath) {
bytes, err := fs.ReadFile(absPath)
if err != nil {
return FSConfig{}, bosherr.WrapErrorf(err, "Reading config '%s'", absPath)
}
err = yaml.Unmarshal(bytes, &schema)
if err != nil {
return FSConfig{}, bosherr.WrapError(err, "Unmarshalling config")
}
}
return FSConfig{path: absPath, fs: fs, schema: schema}, nil
}
func (c FSConfig) Environment() string { return c.schema.CurrentEnvironment }
func (c FSConfig) Environments() []Environment {
environments := []Environment{}
for _, tg := range c.schema.Environments {
environments = append(environments, Environment{URL: tg.URL, Alias: tg.Alias})
}
return environments
}
func (c FSConfig) ResolveEnvironment(urlOrAlias string) string {
_, tg := c.findOrCreateEnvironment(urlOrAlias)
return tg.URL
}
func (c FSConfig) SetEnvironment(urlOrAlias, alias, caCert string) Config {
config := c.deepCopy()
var url string
// If url is not provided, url might actually be an alias
if len(alias) == 0 {
url = c.ResolveEnvironment(urlOrAlias)
} else {
url = urlOrAlias
i, tg := config.findOrCreateEnvironment(url)
tg.Alias = alias
config.schema.Environments[i] = tg
}
config.schema.CurrentEnvironment = url
i, tg := config.findOrCreateEnvironment(url)
tg.CACert = c.readCACert(caCert)
config.schema.Environments[i] = tg
return config
}
func (c FSConfig) CACert(urlOrAlias string) string {
_, tg := c.findOrCreateEnvironment(urlOrAlias)
return c.readCACert(tg.CACert)
}
func (c FSConfig) readCACert(caCert string) string {
if strings.Contains(caCert, "BEGIN") {
return caCert
}
readCACert, err := c.fs.ReadFileString(caCert)
if err != nil {
return ""
}
return readCACert
}
func (c FSConfig) Credentials(urlOrAlias string) Creds {
_, tg := c.findOrCreateEnvironment(urlOrAlias)
return Creds{
Username: tg.Username,
Password: tg.Password,
RefreshToken: tg.RefreshToken,
}
}
func (c FSConfig) SetCredentials(urlOrAlias string, creds Creds) Config {
config := c.deepCopy()
i, tg := config.findOrCreateEnvironment(urlOrAlias)
tg.Username = creds.Username
tg.Password = creds.Password
tg.RefreshToken = creds.RefreshToken
config.schema.Environments[i] = tg
return config
}
func (c FSConfig) UnsetCredentials(urlOrAlias string) Config {
config := c.deepCopy()
i, tg := config.findOrCreateEnvironment(urlOrAlias)
tg.Username = ""
tg.Password = ""
tg.RefreshToken = ""
config.schema.Environments[i] = tg
return config
}
func (c FSConfig) Deployment(urlOrAlias string) string {
_, tg := c.findOrCreateEnvironment(urlOrAlias)
return tg.CurrentDeployment
}
func (c FSConfig) SetDeployment(urlOrAlias, nameOrPath string) Config {
config := c.deepCopy()
i, tg := config.findOrCreateEnvironment(urlOrAlias)
tg.CurrentDeployment = nameOrPath
config.schema.Environments[i] = tg
return config
}
func (c FSConfig) Save() error {
bytes, err := yaml.Marshal(c.schema)
if err != nil {
return bosherr.WrapError(err, "Marshalling config")
}
err = c.fs.WriteFile(c.path, bytes)
if err != nil {
return bosherr.WrapErrorf(err, "Writing config '%s'", c.path)
}
return nil
}
func (c *FSConfig) findOrCreateEnvironment(urlOrAlias string) (int, fsConfigSchema_Environment) {
for i, tg := range c.schema.Environments {
if urlOrAlias == tg.URL || urlOrAlias == tg.Alias {
return i, tg
}
}
tg := fsConfigSchema_Environment{URL: urlOrAlias}
c.schema.Environments = append(c.schema.Environments, tg)
return len(c.schema.Environments) - 1, tg
}
func (c FSConfig) deepCopy() FSConfig {
bytes, err := yaml.Marshal(c.schema)
if err != nil {
panic("serializing config schema")
}
var schema fsConfigSchema
err = yaml.Unmarshal(bytes, &schema)
if err != nil {
panic("deserializing config schema")
}
return FSConfig{path: c.path, fs: c.fs, schema: schema}
}