forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_config.go
199 lines (153 loc) · 4.45 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
package config
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
"gopkg.in/yaml.v2"
)
/*
environments:
- url: https://192.168.50.4:25555
ca_cert: |...
username: admin
password: admin
*/
type FSConfig struct {
path string
fs boshsys.FileSystem
schema fsConfigSchema
}
type fsConfigSchema struct {
Environments []fsConfigSchema_Environment `yaml:"environments"`
}
type fsConfigSchema_Environment struct {
URL string `yaml:"url"`
CACert string `yaml:"ca_cert,omitempty"`
Alias string `yaml:"alias,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) 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) AliasEnvironment(url, alias, caCert string) (Config, error) {
if len(url) == 0 {
return nil, bosherr.Error("Expected non-empty environment URL")
}
if len(alias) == 0 {
return nil, bosherr.Error("Expected non-empty environment alias")
}
config := c.deepCopy()
i, tg := config.findOrCreateEnvironmentByUrlOrAlias(url, alias)
tg.URL = url
tg.Alias = alias
tg.CACert = caCert
config.schema.Environments[i] = tg
return config, nil
}
func (c FSConfig) CACert(urlOrAlias string) string {
_, tg := c.findOrCreateEnvironment(urlOrAlias)
return tg.CACert
}
func (c FSConfig) Credentials(urlOrAlias string) Creds {
_, tg := c.findOrCreateEnvironment(urlOrAlias)
return Creds{
Client: tg.Username,
ClientSecret: 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.Client
tg.Password = creds.ClientSecret
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) 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) {
// Always consider empty URL/alias as a new item
if urlOrAlias != "" {
for i, tg := range c.schema.Environments {
if urlOrAlias == tg.URL || urlOrAlias == tg.Alias {
return i, tg
}
}
}
return c.appendNewEnvironmentWithURL(urlOrAlias)
}
func (c *FSConfig) findOrCreateEnvironmentByUrlOrAlias(url, alias string) (int, fsConfigSchema_Environment) {
for i, tg := range c.schema.Environments {
if url == tg.URL || alias == tg.Alias {
return i, tg
}
}
i, tg := c.appendNewEnvironmentWithURL(url)
tg.Alias = alias
return i, tg
}
func (c *FSConfig) appendNewEnvironmentWithURL(url string) (int, fsConfigSchema_Environment) {
tg := fsConfigSchema_Environment{URL: url}
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}
}