Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / docker / swarmkit / manager / state / store / configs.go
1 package store
2
3 import (
4         "strings"
5
6         "github.com/docker/swarmkit/api"
7         memdb "github.com/hashicorp/go-memdb"
8 )
9
10 const tableConfig = "config"
11
12 func init() {
13         register(ObjectStoreConfig{
14                 Table: &memdb.TableSchema{
15                         Name: tableConfig,
16                         Indexes: map[string]*memdb.IndexSchema{
17                                 indexID: {
18                                         Name:    indexID,
19                                         Unique:  true,
20                                         Indexer: api.ConfigIndexerByID{},
21                                 },
22                                 indexName: {
23                                         Name:    indexName,
24                                         Unique:  true,
25                                         Indexer: api.ConfigIndexerByName{},
26                                 },
27                                 indexCustom: {
28                                         Name:         indexCustom,
29                                         Indexer:      api.ConfigCustomIndexer{},
30                                         AllowMissing: true,
31                                 },
32                         },
33                 },
34                 Save: func(tx ReadTx, snapshot *api.StoreSnapshot) error {
35                         var err error
36                         snapshot.Configs, err = FindConfigs(tx, All)
37                         return err
38                 },
39                 Restore: func(tx Tx, snapshot *api.StoreSnapshot) error {
40                         configs, err := FindConfigs(tx, All)
41                         if err != nil {
42                                 return err
43                         }
44                         for _, s := range configs {
45                                 if err := DeleteConfig(tx, s.ID); err != nil {
46                                         return err
47                                 }
48                         }
49                         for _, s := range snapshot.Configs {
50                                 if err := CreateConfig(tx, s); err != nil {
51                                         return err
52                                 }
53                         }
54                         return nil
55                 },
56                 ApplyStoreAction: func(tx Tx, sa api.StoreAction) error {
57                         switch v := sa.Target.(type) {
58                         case *api.StoreAction_Config:
59                                 obj := v.Config
60                                 switch sa.Action {
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)
67                                 }
68                         }
69                         return errUnknownStoreAction
70                 },
71         })
72 }
73
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
80         }
81
82         return tx.create(tableConfig, c)
83 }
84
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
92                 }
93         }
94
95         return tx.update(tableConfig, c)
96 }
97
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)
102 }
103
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)
108         if c == nil {
109                 return nil
110         }
111         return c.(*api.Config)
112 }
113
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 {
117                 switch by.(type) {
118                 case byName, byNamePrefix, byIDPrefix, byCustom, byCustomPrefix:
119                         return nil
120                 default:
121                         return ErrInvalidFindBy
122                 }
123         }
124
125         configList := []*api.Config{}
126         appendResult := func(o api.StoreObject) {
127                 configList = append(configList, o.(*api.Config))
128         }
129
130         err := tx.find(tableConfig, by, checkType, appendResult)
131         return configList, err
132 }