6 "github.com/docker/swarmkit/api"
7 memdb "github.com/hashicorp/go-memdb"
10 const tableConfig = "config"
13 register(ObjectStoreConfig{
14 Table: &memdb.TableSchema{
16 Indexes: map[string]*memdb.IndexSchema{
20 Indexer: api.ConfigIndexerByID{},
25 Indexer: api.ConfigIndexerByName{},
29 Indexer: api.ConfigCustomIndexer{},
34 Save: func(tx ReadTx, snapshot *api.StoreSnapshot) error {
36 snapshot.Configs, err = FindConfigs(tx, All)
39 Restore: func(tx Tx, snapshot *api.StoreSnapshot) error {
40 configs, err := FindConfigs(tx, All)
44 for _, s := range configs {
45 if err := DeleteConfig(tx, s.ID); err != nil {
49 for _, s := range snapshot.Configs {
50 if err := CreateConfig(tx, s); err != nil {
56 ApplyStoreAction: func(tx Tx, sa api.StoreAction) error {
57 switch v := sa.Target.(type) {
58 case *api.StoreAction_Config:
61 case api.StoreActionKindCreate:
62 return CreateConfig(tx, obj)
63 case api.StoreActionKindUpdate:
64 return UpdateConfig(tx, obj)
65 case api.StoreActionKindRemove:
66 return DeleteConfig(tx, obj.ID)
69 return errUnknownStoreAction
74 // CreateConfig adds a new config to the store.
75 // Returns ErrExist if the ID is already taken.
76 func CreateConfig(tx Tx, c *api.Config) error {
77 // Ensure the name is not already in use.
78 if tx.lookup(tableConfig, indexName, strings.ToLower(c.Spec.Annotations.Name)) != nil {
79 return ErrNameConflict
82 return tx.create(tableConfig, c)
85 // UpdateConfig updates an existing config in the store.
86 // Returns ErrNotExist if the config doesn't exist.
87 func UpdateConfig(tx Tx, c *api.Config) error {
88 // Ensure the name is either not in use or already used by this same Config.
89 if existing := tx.lookup(tableConfig, indexName, strings.ToLower(c.Spec.Annotations.Name)); existing != nil {
90 if existing.GetID() != c.ID {
91 return ErrNameConflict
95 return tx.update(tableConfig, c)
98 // DeleteConfig removes a config from the store.
99 // Returns ErrNotExist if the config doesn't exist.
100 func DeleteConfig(tx Tx, id string) error {
101 return tx.delete(tableConfig, id)
104 // GetConfig looks up a config by ID.
105 // Returns nil if the config doesn't exist.
106 func GetConfig(tx ReadTx, id string) *api.Config {
107 c := tx.get(tableConfig, id)
111 return c.(*api.Config)
114 // FindConfigs selects a set of configs and returns them.
115 func FindConfigs(tx ReadTx, by By) ([]*api.Config, error) {
116 checkType := func(by By) error {
118 case byName, byNamePrefix, byIDPrefix, byCustom, byCustomPrefix:
121 return ErrInvalidFindBy
125 configList := []*api.Config{}
126 appendResult := func(o api.StoreObject) {
127 configList = append(configList, o.(*api.Config))
130 err := tx.find(tableConfig, by, checkType, appendResult)
131 return configList, err