-
Notifications
You must be signed in to change notification settings - Fork 11
/
patching_function_api.go
444 lines (387 loc) · 12.5 KB
/
patching_function_api.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// Copyright (c) 2020 Sorint.lab S.p.A.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package service is a package that provides methods for querying data
package service
import (
"strings"
"github.com/ercole-io/ercole/model"
"github.com/ercole-io/ercole/utils"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// DatabaseTagsAdderCode is the code used to add a tag to a database when the DatabaseTagsAdderMarker is missing
// It assumes that the vars has the following structure:
/*
{
"tags": {
"dbname1": ["tag1", "tag2", ...],
"dbname2": ["tag1", "tag2", ...],
"..."
}
}
*/
const DatabaseTagsAdderMarker = "DATABASE_TAGS_ADDER"
const DatabaseTagsAdderCode = `
/*<DATABASE_TAGS_ADDER>*/
if (hostdata.features.oracle != undefined && hostdata.features.oracle.database != undefined) {
hostdata.features.oracle.database.databases.forEach(function addTag(db) {
if (db.name in vars.tags) {
db.tags = vars.tags[db.name];
}
});
}
/*</DATABASE_TAGS_ADDER>*/
`
// DatabaseLicensesFixerCode is the code used to modify the value of a license when the DatabaseLicensesFixerMarker is missing
// It assumes that the vars has the following structure:
/*
{
"licenseModifiers": {
"dbname1": {
"license1": 0,
"license2": 2,
...
},
"dbname2": {
"license1": 0,
"license2": 2,
...
},
"..."
}
}
*/
const DatabaseLicensesFixerMarker = "DATABASE_LICENSES_FIXER"
const DatabaseLicensesFixerCode = `
/*<DATABASE_LICENSES_FIXER>*/
if (hostdata.features.oracle != undefined && hostdata.features.oracle.database != undefined) {
hostdata.features.oracle.database.databases.forEach(function fixLicensesDb(db) {
db.licenses.forEach(function fixLicense(lic) {
if (db.name in vars.licenseModifiers && lic.name in vars.licenseModifiers[db.name]) {
if (! ("oldCount" in lic)) {
lic.oldCount = lic.count;
}
lic.count = vars.licenseModifiers[db.name][lic.name];
} else if ("oldCount" in lic) {
lic.count = lic.oldCount;
delete lic.oldCount;
}
});
});
}
/*</DATABASE_LICENSES_FIXER>*/
`
// GetPatchingFunction return the patching function specified in the hostname param
func (as *APIService) GetPatchingFunction(hostname string) (interface{}, utils.AdvancedErrorInterface) {
//Check host existence
exist, err := as.Database.ExistHostdata(hostname)
if err != nil {
return nil, err
} else if !exist {
return nil, utils.AerrHostNotFound
}
//Get the data
return as.Database.FindPatchingFunction(hostname)
}
// SetPatchingFunction set the patching function of a host
func (as *APIService) SetPatchingFunction(hostname string, pf model.PatchingFunction) (interface{}, utils.AdvancedErrorInterface) {
//Check host existence
exist, err := as.Database.ExistHostdata(hostname)
if err != nil {
return nil, err
} else if !exist {
return nil, utils.AerrHostNotFound
}
//Get old patching function of the same host
oldPf, err := as.Database.FindPatchingFunction(hostname)
if err != nil {
return nil, err
}
const DefaultPatchingCode = DatabaseTagsAdderCode + DatabaseLicensesFixerCode
//Fill missing fields in the new pf
pf.Hostname = hostname
pf.CreatedAt = as.TimeNow()
if oldPf.Hostname != hostname || oldPf.Code == "" {
id := new(primitive.ObjectID)
*id = primitive.NewObjectIDFromTimestamp(as.TimeNow())
pf.ID = id
} else {
pf.ID = oldPf.ID
}
//Save the pf
err = as.Database.SavePatchingFunction(pf)
if err != nil {
return nil, err
}
//Apply the patch
err = as.ApplyPatch(pf)
if err != nil {
return nil, err
}
return pf.ID, nil
}
// DeletePatchingFunction delete the patching function of a host
func (as *APIService) DeletePatchingFunction(hostname string) utils.AdvancedErrorInterface {
//Check host existence
exist, err := as.Database.ExistHostdata(hostname)
if err != nil {
return err
} else if !exist {
return utils.AerrHostNotFound
}
//Delete the patching function
return as.Database.DeletePatchingFunction(hostname)
}
// AddTagToOracleDatabase add the tag to the database if it hasn't the tag
func (as *APIService) AddTagToOracleDatabase(hostname string, dbname string, tagname string) utils.AdvancedErrorInterface {
//Find the patching function
pf, err := as.Database.FindPatchingFunction(hostname)
if err != nil {
return err
}
//Check if the pf was found
if pf.Hostname != hostname || pf.Code == "" {
//No, initialze pf
pf.Hostname = hostname
pf.Code = DatabaseTagsAdderCode
pf.Vars = map[string]interface{}{
"tags": map[string]interface{}{
dbname: []interface{}{
tagname,
},
},
}
pf.CreatedAt = as.TimeNow()
} else {
//Check the presence of the marker in the code
if !strings.Contains(pf.Code, "<"+DatabaseTagsAdderMarker+">") {
pf.Code += DatabaseTagsAdderCode
}
//Check the presence and the type of Tags key in Vars. If not (re)initialize it!
if val, ok := pf.Vars["tags"]; !ok {
pf.Vars["tags"] = make(map[string]interface{})
} else if _, ok := val.(map[string]interface{}); !ok {
pf.Vars["tags"] = make(map[string]interface{})
}
tags := pf.Vars["tags"].(map[string]interface{})
//Check the presence of the database with a slice inside
if val, ok := tags[dbname]; !ok {
tags[dbname] = make(bson.A, 0)
} else if _, ok := val.(bson.A); !ok {
tags[dbname] = make(bson.A, 0)
}
//Get the slice inside
dbTags := tags[dbname].(bson.A)
//Check if it already contains the tag
for _, val := range dbTags {
if val == tagname {
return nil
}
}
//Add it because the pf cannot contains it
dbTags = append(dbTags, tagname)
tags[dbname] = dbTags
}
// Save the modified patch
if pf.ID == nil {
oi := primitive.NewObjectIDFromTimestamp(as.TimeNow())
pf.ID = &oi
}
err = as.Database.SavePatchingFunction(pf)
if err != nil {
return err
}
return as.ApplyPatch(pf)
}
// DeleteTagOfOracleDatabase delete the tag from the database if it hasn't the tag
func (as *APIService) DeleteTagOfOracleDatabase(hostname string, dbname string, tagname string) utils.AdvancedErrorInterface {
//Find the patching function
pf, err := as.Database.FindPatchingFunction(hostname)
if err != nil {
return err
}
//Check if the pf was found
if pf.Hostname != hostname || pf.Code == "" {
return nil
}
//Check the presence of the marker in the code
if !strings.Contains(pf.Code, "<"+DatabaseTagsAdderMarker+">") {
pf.Code += DatabaseTagsAdderCode
}
//Check the presence and the type of Tags key in Vars. If not (re)initialize it!
if val, ok := pf.Vars["tags"]; !ok {
return nil
} else if _, ok := val.(map[string]interface{}); !ok {
return nil
}
tags := pf.Vars["tags"].(map[string]interface{})
//Check the presence of the database with a slice inside
if val, ok := tags[dbname]; !ok {
return nil
} else if _, ok := val.(bson.A); !ok {
return nil
}
//Get the slice inside
dbTags := tags[dbname].(bson.A)
//Check if it contain the tag
tagIndex := -1
for i, val := range dbTags {
if val == tagname {
tagIndex = i
break
}
}
if tagIndex == -1 {
return nil
}
//Remove it because the pf contains it
tags[dbname] = append(dbTags[:tagIndex], dbTags[tagIndex+1:]...)
//XXX: if there are no tags in the vars, we can remove the DatabaseTagsAdderCode. If there are no code, we can also remove the PF
// Save the modified patch
err = as.Database.SavePatchingFunction(pf)
if err != nil {
return err
}
return as.ApplyPatch(pf)
}
// ApplyPatch apply the patch pf to the relative host
func (as *APIService) ApplyPatch(pf model.PatchingFunction) utils.AdvancedErrorInterface {
//Get the data
data, aerr := as.Database.FindHostData(pf.Hostname)
if aerr == utils.AerrHostNotFound {
return nil
} else if aerr != nil {
return aerr
}
//Patch it
if as.Config.DataService.LogDataPatching {
as.Log.Printf("Patching %s hostdata with the patch %s\n", pf.Hostname, pf.ID)
}
data, aerr = utils.PatchHostdata(pf, data)
if aerr != nil {
return aerr
}
//Save the patched data
return as.Database.ReplaceHostData(data)
}
// SetOracleDatabaseLicenseModifier set the value of certain license to newValue
func (as *APIService) SetOracleDatabaseLicenseModifier(hostname string, dbname string, licenseName string, newValue int) utils.AdvancedErrorInterface {
//Find the patching function
pf, err := as.Database.FindPatchingFunction(hostname)
if err != nil {
return err
}
//Check if the pf was found
if pf.Hostname != hostname || pf.Code == "" {
//No, initialze pf
pf.Hostname = hostname
pf.Code = DatabaseLicensesFixerCode
pf.Vars = map[string]interface{}{
"licenseModifiers": map[string]interface{}{
dbname: map[string]interface{}{
licenseName: newValue,
},
},
}
pf.CreatedAt = as.TimeNow()
} else {
//Check the presence of the marker in the code
if !strings.Contains(pf.Code, "<"+DatabaseLicensesFixerMarker+">") {
pf.Code += DatabaseLicensesFixerCode
}
//Check the presence and the type of LicenseModifiers key in Vars. If not (re)initialize it!
if val, ok := pf.Vars["licenseModifiers"]; !ok {
pf.Vars["licenseModifiers"] = make(map[string]interface{})
} else if _, ok := val.(map[string]interface{}); !ok {
pf.Vars["licenseModifiers"] = make(map[string]interface{})
}
licenseModifiers := pf.Vars["licenseModifiers"].(map[string]interface{})
// Check the presence of the database with a slice inside
switch licenseModifiers[dbname].(type) {
case nil:
licenseModifiers[dbname] = make(map[string]interface{}, 0)
case map[string]interface{}:
default:
licenseModifiers[dbname] = make(map[string]interface{}, 0)
}
//Get the modifiers mof the db
licenseModifiersOfDb := licenseModifiers[dbname].(map[string]interface{})
//Check if it already contains the licenseName with the same value
if val, ok := licenseModifiersOfDb[licenseName]; !ok {
licenseModifiersOfDb[licenseName] = newValue
} else if val == newValue {
return nil
} else {
licenseModifiersOfDb[licenseName] = newValue
}
}
// Save the modified patch
if pf.ID == nil {
oi := primitive.NewObjectIDFromTimestamp(as.TimeNow())
pf.ID = &oi
}
err = as.Database.SavePatchingFunction(pf)
if err != nil {
return err
}
return as.ApplyPatch(pf)
}
// DeleteOracleDatabaseLicenseModifier delete the modifier of a certain license
func (as *APIService) DeleteOracleDatabaseLicenseModifier(hostname string, dbname string, licenseName string) utils.AdvancedErrorInterface {
//Find the patching function
pf, err := as.Database.FindPatchingFunction(hostname)
if err != nil {
return err
}
//Check if the pf was found
if pf.Hostname != hostname || pf.Code == "" {
return nil
}
//Check the presence of the marker in the code
if !strings.Contains(pf.Code, "<"+DatabaseLicensesFixerMarker+">") {
pf.Code += DatabaseLicensesFixerCode
}
//Check the presence and the type of LicenseModifiers key in Vars. If not (re)initialize it!
if val, ok := pf.Vars["licenseModifiers"]; !ok {
return nil
} else if _, ok := val.(map[string]interface{}); !ok {
return nil
}
licenseModifiers := pf.Vars["licenseModifiers"].(map[string]interface{})
//Check the presence of the database with a slice inside
if val, ok := licenseModifiers[dbname]; !ok {
return nil
} else if _, ok := val.(map[string]interface{}); !ok {
return nil
}
//Get the modifiers mof the db
licenseModifiersOfDb := licenseModifiers[dbname].(map[string]interface{})
//Check if it already contains the licenseName with the same value
if _, ok := licenseModifiersOfDb[licenseName]; !ok {
return nil
}
//TODO: improved the cleanup of the license modifier
delete(licenseModifiersOfDb, licenseName)
// Save the modified patch
err = as.Database.SavePatchingFunction(pf)
if err != nil {
return err
}
return as.ApplyPatch(pf)
}
// SearchOracleDatabaseLicenseModifiers search license modifiers
func (as *APIService) SearchOracleDatabaseLicenseModifiers(search string, sortBy string, sortDesc bool, page int, pageSize int) ([]map[string]interface{}, utils.AdvancedErrorInterface) {
return as.Database.SearchOracleDatabaseLicenseModifiers(strings.Split(search, " "), sortBy, sortDesc, page, pageSize)
}