/
config.go
252 lines (206 loc) · 6.48 KB
/
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
package util
import (
"bufio"
"bytes"
"encoding/json"
"github.com/BurntSushi/toml"
"io/ioutil"
"net/http"
"os"
"regexp"
)
type Config struct {
ProfileName string `json:"profileName,omitempty"`
ConfigName string `json:"configName,omitempty"`
WorkflowId string `json:"workflowId,omitempty"`
AppPlugin string `json:"appPlugin"`
StoragePlugin string `json:"storagePlugin"`
ArchivePlugin string `json:"archivePlugin"`
AutoDiscovery bool `json:"autoDiscovery"`
JobRetention int `json:"jobRetention"`
BackupRetentions []BackupRetention `json:"backupRetentions"`
SelectedBackupPolicy string `json:"backupPolicy,omitmepty"`
SelectedBackupRetention int `json:"backupRetention,omitmepty"`
SelectedWorkflowId int `json:"selectedWorkflowId,omitmepty"`
PreAppQuiesceCmd string `json:"preAppQuiesceCmd,omitempty"`
AppQuiesceCmd string `json:"appQuiesceCmd,omitempty"`
PostAppQuiesceCmd string `json:"postAppQuiesceCmd,omitempty"`
BackupCreateCmd string `json:"backupCreateCmd,omitempty"`
BackupDeleteCmd string `json:"backupDeleteCmd,omitempty"`
ArchiveCreateCmd string `json:"archiveCreateCmd,omitempty"`
ArchiveDeleteCmd string `json:"archiveDeleteCmd,omitempty"`
PreAppUnquiesceCmd string `json:"preAppUnquiesceCmd,omitempty"`
AppUnquiesceCmd string `json:"appUnquiesceCmd,omitempty"`
PostAppUnquiesceCmd string `json:"postAppUnquiesceCmd,omitempty"`
PreAppRestoreCmd string `json:"preAppRestoreCmd,omitempty"`
RestoreCmd string `json:"restoreCmd,omitempty"`
PostAppRestoreCmd string `json:"postAppRestoreCmd,omitempty"`
SendTrapErrorCmd string `json:"sendTrapErrorCmd,omitempty"`
SendTrapSuccessCmd string `json:"sendTrapSuccessCmd,omitempty"`
AppPluginParameters map[string]string `json:"appPluginParameters,omitempty"`
StoragePluginParameters map[string]string `json:"storagePluginParameters,omitempty"`
}
type ConfigResult struct {
Config Config `json:"config,omitmepty"`
Result Result `json:"result,omitmepty"`
}
type ConfigMapResult struct {
ConfigMap map[string]string `json:"configMap,omitmepty"`
Result Result `json:"result,omitmepty"`
}
type BackupRetention struct {
Policy string `json:"policy"`
RetentionDays int `json:"retentionDays"`
}
type PluginConfigMap struct {
Key1 string `json:"key1,omitempty"`
Key2 string `json:"key2,omitempty"`
}
func ReadConfig(filePath string) (Config, error) {
var config Config
b, err := ioutil.ReadFile(filePath)
if err != nil {
return config, err
} else {
str := string(b)
config, err = decodeConfig(str)
if err != nil {
return config, err
}
return config, nil
}
}
func WriteConfig(filePath string, config Config) error {
buf, err := EncodeConfig(config)
if err != nil {
return err
}
err = ioutil.WriteFile(filePath, buf.Bytes(), 0644)
if err != nil {
return err
}
return nil
}
func WritePluginConfig(filePath string, configMap map[string]string) error {
buf, err := EncodePluginConfig(configMap)
if err != nil {
return err
}
err = ioutil.WriteFile(filePath, buf.Bytes(), 0644)
if err != nil {
return err
}
return nil
}
func EncodePluginConfig(configMap map[string]string) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(configMap); err != nil {
return buf, err
}
return buf, nil
}
func EncodeConfig(config Config) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(config); err != nil {
return buf, err
}
return buf, nil
}
func decodeConfig(blob string) (Config, error) {
var config Config
if _, err := toml.Decode(blob, &config); err != nil {
return config, err
}
return config, nil
}
func ReadConfigToMap(filePath string) (map[string]string, error) {
var configMap = map[string]string{}
file, err := os.Open(filePath)
if err != nil {
return configMap, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
if err := scanner.Err(); err != nil {
return configMap, err
}
for scanner.Scan() {
re := regexp.MustCompile(`(\S+)\s*=\s*\"(\S+)\"`)
match := re.FindStringSubmatch(scanner.Text())
if len(match) != 0 {
configMap[match[1]] = match[2]
}
}
return configMap, nil
}
func SetAppPluginParameters(appConfigPath string, config Config) (Config, error) {
var err error
configAppMap := make(map[string]string)
if len(config.AppPlugin) != 0 {
configAppMap, err = ReadConfigToMap(appConfigPath)
if err != nil {
return config, err
}
}
config.AppPluginParameters = configAppMap
return config, nil
}
func SetStoragePluginParameters(storageConfigPath string, config Config) (Config, error) {
var err error
configStorageMap := make(map[string]string)
if len(config.StoragePlugin) != 0 {
configStorageMap, err = ReadConfigToMap(storageConfigPath)
if err != nil {
return config, err
}
}
config.StoragePluginParameters = configStorageMap
return config, nil
}
func GetConfig(w http.ResponseWriter, r *http.Request) (Config, error) {
var config Config
if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
return config, err
}
defer r.Body.Close()
_, err := json.Marshal(&config)
if err != nil {
return config, err
}
return config, nil
}
func GetPluginConfig(w http.ResponseWriter, r *http.Request) (map[string]string, error) {
var configMap map[string]string
if err := json.NewDecoder(r.Body).Decode(&configMap); err != nil {
return configMap, err
}
defer r.Body.Close()
_, err := json.Marshal(&configMap)
if err != nil {
return configMap, err
}
return configMap, nil
}
func ConfigMapToJson(configMap map[string]string) (string, error) {
jsonString, err := json.Marshal(configMap)
if err != nil {
return " ", err
}
return string(jsonString), nil
}
func ExistsBackupRetention(policy string, retentions []BackupRetention) bool {
for _, retention := range retentions {
if retention.Policy == policy {
return true
}
}
return false
}
func GetBackupRetention(policy string, retentions []BackupRetention) int {
for _, retention := range retentions {
if retention.Policy == policy {
return retention.RetentionDays
}
}
return -1
}