forked from canonical/lxd
/
db_profiles.go
257 lines (209 loc) · 5.48 KB
/
db_profiles.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/lxc/lxd/lxd/types"
"github.com/lxc/lxd/shared/api"
)
// dbProfiles returns a string list of profiles.
func dbProfiles(db *sql.DB) ([]string, error) {
q := fmt.Sprintf("SELECT name FROM profiles")
inargs := []interface{}{}
var name string
outfmt := []interface{}{name}
result, err := dbQueryScan(db, q, inargs, outfmt)
if err != nil {
return []string{}, err
}
response := []string{}
for _, r := range result {
response = append(response, r[0].(string))
}
return response, nil
}
func dbProfileGet(db *sql.DB, name string) (int64, *api.Profile, error) {
id := int64(-1)
description := sql.NullString{}
q := "SELECT id, description FROM profiles WHERE name=?"
arg1 := []interface{}{name}
arg2 := []interface{}{&id, &description}
err := dbQueryRowScan(db, q, arg1, arg2)
if err != nil {
return -1, nil, err
}
config, err := dbProfileConfig(db, name)
if err != nil {
return -1, nil, err
}
devices, err := dbDevices(db, name, true)
if err != nil {
return -1, nil, err
}
profile := api.Profile{
Name: name,
}
profile.Config = config
profile.Description = description.String
profile.Devices = devices
return id, &profile, nil
}
func dbProfileCreate(db *sql.DB, profile string, description string, config map[string]string,
devices types.Devices) (int64, error) {
tx, err := dbBegin(db)
if err != nil {
return -1, err
}
result, err := tx.Exec("INSERT INTO profiles (name, description) VALUES (?, ?)", profile, description)
if err != nil {
tx.Rollback()
return -1, err
}
id, err := result.LastInsertId()
if err != nil {
tx.Rollback()
return -1, err
}
err = dbProfileConfigAdd(tx, id, config)
if err != nil {
tx.Rollback()
return -1, err
}
err = dbDevicesAdd(tx, "profile", id, devices)
if err != nil {
tx.Rollback()
return -1, err
}
err = txCommit(tx)
if err != nil {
return -1, err
}
return id, nil
}
func dbProfileCreateDefault(db *sql.DB) error {
id, _, _ := dbProfileGet(db, "default")
if id != -1 {
// default profile already exists
return nil
}
_, err := dbProfileCreate(db, "default", "Default LXD profile", map[string]string{}, types.Devices{})
if err != nil {
return err
}
return nil
}
// Get the profile configuration map from the DB
func dbProfileConfig(db *sql.DB, name string) (map[string]string, error) {
var key, value string
query := `
SELECT
key, value
FROM profiles_config
JOIN profiles ON profiles_config.profile_id=profiles.id
WHERE name=?`
inargs := []interface{}{name}
outfmt := []interface{}{key, value}
results, err := dbQueryScan(db, query, inargs, outfmt)
if err != nil {
return nil, fmt.Errorf("Failed to get profile '%s'", name)
}
if len(results) == 0 {
/*
* If we didn't get any rows here, let's check to make sure the
* profile really exists; if it doesn't, let's send back a 404.
*/
query := "SELECT id FROM profiles WHERE name=?"
var id int
results, err := dbQueryScan(db, query, []interface{}{name}, []interface{}{id})
if err != nil {
return nil, err
}
if len(results) == 0 {
return nil, NoSuchObjectError
}
}
config := map[string]string{}
for _, r := range results {
key = r[0].(string)
value = r[1].(string)
config[key] = value
}
return config, nil
}
func dbProfileDelete(db *sql.DB, name string) error {
id, _, err := dbProfileGet(db, name)
if err != nil {
return err
}
_, err = dbExec(db, "DELETE FROM profiles WHERE id=?", id)
if err != nil {
return err
}
return nil
}
func dbProfileUpdate(db *sql.DB, name string, newName string) error {
tx, err := dbBegin(db)
if err != nil {
return err
}
_, err = tx.Exec("UPDATE profiles SET name=? WHERE name=?", newName, name)
if err != nil {
tx.Rollback()
return err
}
err = txCommit(tx)
return err
}
func dbProfileDescriptionUpdate(tx *sql.Tx, id int64, description string) error {
_, err := tx.Exec("UPDATE profiles SET description=? WHERE id=?", description, id)
return err
}
func dbProfileConfigClear(tx *sql.Tx, id int64) error {
_, err := tx.Exec("DELETE FROM profiles_config WHERE profile_id=?", id)
if err != nil {
return err
}
_, err = tx.Exec(`DELETE FROM profiles_devices_config WHERE id IN
(SELECT profiles_devices_config.id
FROM profiles_devices_config JOIN profiles_devices
ON profiles_devices_config.profile_device_id=profiles_devices.id
WHERE profiles_devices.profile_id=?)`, id)
if err != nil {
return err
}
_, err = tx.Exec("DELETE FROM profiles_devices WHERE profile_id=?", id)
if err != nil {
return err
}
return nil
}
func dbProfileConfigAdd(tx *sql.Tx, id int64, config map[string]string) error {
str := fmt.Sprintf("INSERT INTO profiles_config (profile_id, key, value) VALUES(?, ?, ?)")
stmt, err := tx.Prepare(str)
defer stmt.Close()
for k, v := range config {
_, err = stmt.Exec(id, k, v)
if err != nil {
return err
}
}
return nil
}
func dbProfileContainersGet(db *sql.DB, profile string) ([]string, error) {
q := `SELECT containers.name FROM containers JOIN containers_profiles
ON containers.id == containers_profiles.container_id
JOIN profiles ON containers_profiles.profile_id == profiles.id
WHERE profiles.name == ?`
results := []string{}
inargs := []interface{}{profile}
var name string
outfmt := []interface{}{name}
output, err := dbQueryScan(db, q, inargs, outfmt)
if err != nil {
return results, err
}
for _, r := range output {
results = append(results, r[0].(string))
}
return results, nil
}