forked from canonical/lxd
/
config.go
60 lines (50 loc) · 1.43 KB
/
config.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
package db
import "github.com/lxc/lxd/lxd/db/query"
// Config fetches all LXD node-level config keys.
func (n *NodeTx) Config() (map[string]string, error) {
return query.SelectConfig(n.tx, "config", "")
}
// UpdateConfig updates the given LXD node-level configuration keys in the
// config table. Config keys set to empty values will be deleted.
func (n *NodeTx) UpdateConfig(values map[string]string) error {
return query.UpdateConfig(n.tx, "config", values)
}
// Config fetches all LXD cluster config keys.
func (c *ClusterTx) Config() (map[string]string, error) {
return query.SelectConfig(c.tx, "config", "")
}
// UpdateConfig updates the given LXD cluster configuration keys in the
// config table. Config keys set to empty values will be deleted.
func (c *ClusterTx) UpdateConfig(values map[string]string) error {
return query.UpdateConfig(c.tx, "config", values)
}
func ConfigValueSet(cluster *Cluster, key string, value string) error {
tx, err := begin(cluster.db)
if err != nil {
return err
}
_, err = tx.Exec("DELETE FROM config WHERE key=?", key)
if err != nil {
tx.Rollback()
return err
}
if value != "" {
str := `INSERT INTO config (key, value) VALUES (?, ?);`
stmt, err := tx.Prepare(str)
if err != nil {
tx.Rollback()
return err
}
defer stmt.Close()
_, err = stmt.Exec(key, value)
if err != nil {
tx.Rollback()
return err
}
}
err = TxCommit(tx)
if err != nil {
return err
}
return nil
}