forked from moby/swarmkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configs.go
132 lines (119 loc) · 3.23 KB
/
configs.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
package store
import (
"strings"
"github.com/docker/swarmkit/api"
memdb "github.com/hashicorp/go-memdb"
)
const tableConfig = "config"
func init() {
register(ObjectStoreConfig{
Table: &memdb.TableSchema{
Name: tableConfig,
Indexes: map[string]*memdb.IndexSchema{
indexID: {
Name: indexID,
Unique: true,
Indexer: api.ConfigIndexerByID{},
},
indexName: {
Name: indexName,
Unique: true,
Indexer: api.ConfigIndexerByName{},
},
indexCustom: {
Name: indexCustom,
Indexer: api.ConfigCustomIndexer{},
AllowMissing: true,
},
},
},
Save: func(tx ReadTx, snapshot *api.StoreSnapshot) error {
var err error
snapshot.Configs, err = FindConfigs(tx, All)
return err
},
Restore: func(tx Tx, snapshot *api.StoreSnapshot) error {
configs, err := FindConfigs(tx, All)
if err != nil {
return err
}
for _, s := range configs {
if err := DeleteConfig(tx, s.ID); err != nil {
return err
}
}
for _, s := range snapshot.Configs {
if err := CreateConfig(tx, s); err != nil {
return err
}
}
return nil
},
ApplyStoreAction: func(tx Tx, sa api.StoreAction) error {
switch v := sa.Target.(type) {
case *api.StoreAction_Config:
obj := v.Config
switch sa.Action {
case api.StoreActionKindCreate:
return CreateConfig(tx, obj)
case api.StoreActionKindUpdate:
return UpdateConfig(tx, obj)
case api.StoreActionKindRemove:
return DeleteConfig(tx, obj.ID)
}
}
return errUnknownStoreAction
},
})
}
// CreateConfig adds a new config to the store.
// Returns ErrExist if the ID is already taken.
func CreateConfig(tx Tx, c *api.Config) error {
// Ensure the name is not already in use.
if tx.lookup(tableConfig, indexName, strings.ToLower(c.Spec.Annotations.Name)) != nil {
return ErrNameConflict
}
return tx.create(tableConfig, c)
}
// UpdateConfig updates an existing config in the store.
// Returns ErrNotExist if the config doesn't exist.
func UpdateConfig(tx Tx, c *api.Config) error {
// Ensure the name is either not in use or already used by this same Config.
if existing := tx.lookup(tableConfig, indexName, strings.ToLower(c.Spec.Annotations.Name)); existing != nil {
if existing.GetID() != c.ID {
return ErrNameConflict
}
}
return tx.update(tableConfig, c)
}
// DeleteConfig removes a config from the store.
// Returns ErrNotExist if the config doesn't exist.
func DeleteConfig(tx Tx, id string) error {
return tx.delete(tableConfig, id)
}
// GetConfig looks up a config by ID.
// Returns nil if the config doesn't exist.
func GetConfig(tx ReadTx, id string) *api.Config {
c := tx.get(tableConfig, id)
if c == nil {
return nil
}
return c.(*api.Config)
}
// FindConfigs selects a set of configs and returns them.
func FindConfigs(tx ReadTx, by By) ([]*api.Config, error) {
checkType := func(by By) error {
switch by.(type) {
case byName, byNamePrefix, byIDPrefix, byCustom, byCustomPrefix:
return nil
default:
return ErrInvalidFindBy
}
}
configList := []*api.Config{}
appendResult := func(o api.StoreObject) {
configList = append(configList, o.(*api.Config))
}
err := tx.find(tableConfig, by, checkType, appendResult)
return configList, err
}