forked from evergreen-ci/evergreen
/
admin_maps.go
116 lines (105 loc) · 3.24 KB
/
admin_maps.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
package migrations
import (
"github.com/evergreen-ci/evergreen/util"
"github.com/mongodb/anser"
"github.com/mongodb/anser/db"
"github.com/mongodb/anser/model"
"github.com/pkg/errors"
"gopkg.in/mgo.v2/bson"
)
const (
adminCollection = "admin"
globalDocId = "global"
migrationAdminMapRestructure = "admin_map_restructure"
)
func adminMapRestructureGenerator(env anser.Environment, args migrationGeneratorFactoryOptions) (anser.Generator, error) {
if err := env.RegisterManualMigrationOperation(migrationAdminMapRestructure, makeAdminMapMigration(args.db)); err != nil {
return nil, err
}
opts := model.GeneratorOptions{
NS: model.Namespace{
DB: args.db,
Collection: adminCollection,
},
Limit: args.limit,
Query: db.Document{
"_id": globalDocId,
},
JobID: args.id,
}
return anser.NewManualMigrationGenerator(env, opts, migrationAdminMapRestructure), nil
}
func makeAdminMapMigration(database string) db.MigrationOperation {
const (
credentialsKey = "credentials"
expansionsKey = "expansions"
keysKey = "keys"
pluginsKey = "plugins"
newCredentialsKey = "credentials_new"
newExpansionsKey = "expansions_new"
newKeysKey = "keys_new"
newPluginsKey = "plugins_new"
)
return func(session db.Session, rawD bson.RawD) error {
defer session.Close()
var credentials map[string]string
var expansions map[string]string
var keys map[string]string
var plugins map[string]map[string]interface{}
for _, raw := range rawD {
switch raw.Name {
case credentialsKey:
if err := raw.Value.Unmarshal(&credentials); err != nil {
return errors.Wrap(err, "error unmarshaling credentials")
}
case expansionsKey:
if err := raw.Value.Unmarshal(&expansions); err != nil {
return errors.Wrap(err, "error unmarshaling expansions")
}
case keysKey:
if err := raw.Value.Unmarshal(&keys); err != nil {
return errors.Wrap(err, "error unmarshaling keys")
}
case pluginsKey:
if err := raw.Value.Unmarshal(&plugins); err != nil {
return errors.Wrap(err, "error unmarshaling plugins")
}
}
}
newCredentials := util.KeyValuePairSlice{}
for k, v := range credentials {
newCredentials = append(newCredentials, util.KeyValuePair{Key: k, Value: v})
}
newExpansions := util.KeyValuePairSlice{}
for k, v := range expansions {
newExpansions = append(newExpansions, util.KeyValuePair{Key: k, Value: v})
}
newKeys := util.KeyValuePairSlice{}
for k, v := range keys {
newKeys = append(newKeys, util.KeyValuePair{Key: k, Value: v})
}
newPlugins := util.KeyValuePairSlice{}
for k, v := range plugins {
newMap := util.KeyValuePairSlice{}
for k2, v2 := range v {
newMap = append(newMap, util.KeyValuePair{Key: k2, Value: v2})
}
newPlugins = append(newPlugins, util.KeyValuePair{Key: k, Value: newMap})
}
return session.DB(database).C(adminCollection).UpdateId(globalDocId,
db.Document{
"$set": db.Document{
newCredentialsKey: newCredentials,
newExpansionsKey: newExpansions,
newKeysKey: newKeys,
newPluginsKey: newPlugins,
},
"$unset": db.Document{
credentialsKey: 1,
expansionsKey: 1,
keysKey: 1,
pluginsKey: 1,
},
})
}
}