This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
/
azurefilebroker_config.go
executable file
·284 lines (224 loc) · 6.36 KB
/
azurefilebroker_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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package azurefilebroker
import (
"errors"
"strings"
)
const preexisting = "Preexisting"
type MountConfig struct {
Allowed []string
Forced map[string]string
Options map[string]string
}
type AzureConfig struct {
Environment string
TenanID string
ClientID string
ClientSecret string
DefaultSubscriptionID string
DefaultResourceGroupName string
DefaultLocation string
}
func NewAzureConfig(environment, tenanID, clientID, clientSecret, defaultSubscriptionID, defaultResourceGroupName, defaultLocation string) *AzureConfig {
myConf := new(AzureConfig)
myConf.Environment = environment
myConf.TenanID = tenanID
myConf.ClientID = clientID
myConf.ClientSecret = clientSecret
myConf.DefaultSubscriptionID = defaultSubscriptionID
myConf.DefaultResourceGroupName = defaultResourceGroupName
myConf.DefaultLocation = defaultLocation
return myConf
}
func (config *AzureConfig) IsSupportAzureFileShare() bool {
return config.Environment != preexisting
}
func (config *AzureConfig) Validate() error {
if !config.IsSupportAzureFileShare() {
return nil
}
missingKeys := []string{}
if config.Environment == "" {
missingKeys = append(missingKeys, "environment")
}
if config.TenanID == "" {
missingKeys = append(missingKeys, "tenanID")
}
if config.ClientID == "" {
missingKeys = append(missingKeys, "clientID")
}
if config.ClientSecret == "" {
missingKeys = append(missingKeys, "clientSecret")
}
if len(missingKeys) > 0 {
return errors.New("Missing required parameters: " + strings.Join(missingKeys, ", "))
}
return nil
}
type ControlConfig struct {
AllowCreateStorageAccount bool
AllowCreateFileShare bool
AllowDeleteStorageAccount bool
AllowDeleteFileShare bool
}
func NewControlConfig(allowCreateStorageAccount, allowCreateFileShare, allowDeleteStorageAccount, allowDeleteFileShare bool) *ControlConfig {
myConf := new(ControlConfig)
myConf.AllowCreateStorageAccount = allowCreateStorageAccount
myConf.AllowCreateFileShare = allowCreateFileShare
myConf.AllowDeleteStorageAccount = allowDeleteStorageAccount
myConf.AllowDeleteFileShare = allowDeleteFileShare
return myConf
}
type AzureStackConfig struct {
AzureStackDomain string
AzureStackAuthentication string
AzureStackResource string
AzureStackEndpointPrefix string
}
func NewAzureStackConfig(azureStackDomain, azureStackAuthentication, azureStackResource, azureStackEndpointPrefix string) *AzureStackConfig {
myConf := new(AzureStackConfig)
myConf.AzureStackDomain = azureStackDomain
myConf.AzureStackAuthentication = azureStackAuthentication
myConf.AzureStackResource = azureStackResource
myConf.AzureStackEndpointPrefix = azureStackEndpointPrefix
return myConf
}
func (config *AzureStackConfig) Validate() error {
missingKeys := []string{}
if config.AzureStackDomain == "" {
missingKeys = append(missingKeys, "azureStackDomain")
}
if config.AzureStackAuthentication == "" {
missingKeys = append(missingKeys, "azureStackAuthentication")
}
if config.AzureStackResource == "" {
missingKeys = append(missingKeys, "azureStackResource")
}
if config.AzureStackEndpointPrefix == "" {
missingKeys = append(missingKeys, "azureStackEndpointPrefix")
}
if len(missingKeys) > 0 {
return errors.New("Missing required parameters when 'environment' is 'AzureStack': " + strings.Join(missingKeys, ", "))
}
return nil
}
type CloudConfig struct {
Azure AzureConfig
Control ControlConfig
AzureStack AzureStackConfig
}
type Config struct {
mount MountConfig
cloud CloudConfig
}
func inArray(list []string, key string) bool {
for _, k := range list {
if k == key {
return true
}
}
return false
}
func NewAzurefilebrokerConfig(mountConfig *MountConfig, cloudConfig *CloudConfig) *Config {
myConf := new(Config)
myConf.mount = *mountConfig
myConf.cloud = *cloudConfig
return myConf
}
func NewAzurefilebrokerMountConfig() *MountConfig {
myConf := new(MountConfig)
myConf.Allowed = make([]string, 0)
myConf.Options = make(map[string]string, 0)
myConf.Forced = make(map[string]string, 0)
return myConf
}
func NewAzurefilebrokerCloudConfig(azure *AzureConfig, control *ControlConfig, azureStack *AzureStackConfig) *CloudConfig {
myConf := new(CloudConfig)
myConf.Azure = *azure
myConf.Control = *control
myConf.AzureStack = *azureStack
return myConf
}
func (config *CloudConfig) Validate() error {
if err := config.Azure.Validate(); err != nil {
return err
}
if config.Azure.Environment == AzureStack {
if err := config.AzureStack.Validate(); err != nil {
return err
}
}
return nil
}
func (config *MountConfig) Copy() *MountConfig {
myConf := new(MountConfig)
myConf.Allowed = config.Allowed
myConf.Forced = make(map[string]string, 0)
myConf.Options = make(map[string]string, 0)
for k, v := range config.Forced {
myConf.Forced[k] = v
}
for k, v := range config.Options {
myConf.Options[k] = v
}
return myConf
}
func (config *MountConfig) SetEntries(opts map[string]string) error {
errorList := []string{}
for k, v := range opts {
if inArray(config.Allowed, k) {
config.Options[k] = v
} else {
errorList = append(errorList, k)
}
}
if len(errorList) > 0 {
err := errors.New("Not allowed options : " + strings.Join(errorList, ", "))
return err
}
return nil
}
func (config MountConfig) MakeConfig() map[string]interface{} {
params := map[string]interface{}{}
for k, v := range config.Options {
params[k] = v
}
for k, v := range config.Forced {
params[k] = v
}
return params
}
func (config *MountConfig) ReadConf(allowedFlag string, defaultFlag string) error {
if len(allowedFlag) > 0 {
config.Allowed = strings.Split(allowedFlag, ",")
}
config.readConfDefault(defaultFlag)
return nil
}
func (config *MountConfig) readConfDefault(flagString string) {
if len(flagString) < 1 {
return
}
config.Options = config.parseConfig(strings.Split(flagString, ","))
config.Forced = make(map[string]string)
for k, v := range config.Options {
if !inArray(config.Allowed, k) {
config.Forced[k] = v
delete(config.Options, k)
}
}
}
func (config MountConfig) parseConfig(listEntry []string) map[string]string {
result := map[string]string{}
for _, opt := range listEntry {
key := strings.SplitN(opt, ":", 2)
if len(key[0]) < 1 {
continue
}
if len(key[1]) < 1 {
result[key[0]] = ""
} else {
result[key[0]] = key[1]
}
}
return result
}