forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_devices.go
172 lines (150 loc) · 3.59 KB
/
db_devices.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
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/lxc/lxd/lxd/types"
)
func dbDeviceTypeToString(t int) (string, error) {
switch t {
case 0:
return "none", nil
case 1:
return "nic", nil
case 2:
return "disk", nil
case 3:
return "unix-char", nil
case 4:
return "unix-block", nil
case 5:
return "usb", nil
case 6:
return "gpu", nil
default:
return "", fmt.Errorf("Invalid device type %d", t)
}
}
func dbDeviceTypeToInt(t string) (int, error) {
switch t {
case "none":
return 0, nil
case "nic":
return 1, nil
case "disk":
return 2, nil
case "unix-char":
return 3, nil
case "unix-block":
return 4, nil
case "usb":
return 5, nil
case "gpu":
return 6, nil
default:
return -1, fmt.Errorf("Invalid device type %s", t)
}
}
func dbDevicesAdd(tx *sql.Tx, w string, cID int64, devices types.Devices) error {
// Prepare the devices entry SQL
str1 := fmt.Sprintf("INSERT INTO %ss_devices (%s_id, name, type) VALUES (?, ?, ?)", w, w)
stmt1, err := tx.Prepare(str1)
if err != nil {
return err
}
defer stmt1.Close()
// Prepare the devices config entry SQL
str2 := fmt.Sprintf("INSERT INTO %ss_devices_config (%s_device_id, key, value) VALUES (?, ?, ?)", w, w)
stmt2, err := tx.Prepare(str2)
if err != nil {
return err
}
defer stmt2.Close()
// Insert all the devices
for k, v := range devices {
t, err := dbDeviceTypeToInt(v["type"])
if err != nil {
return err
}
result, err := stmt1.Exec(cID, k, t)
if err != nil {
return err
}
id64, err := result.LastInsertId()
if err != nil {
return fmt.Errorf("Error inserting device %s into database", k)
}
id := int(id64)
for ck, cv := range v {
// The type is stored as int in the parent entry
if ck == "type" {
continue
}
_, err = stmt2.Exec(id, ck, cv)
if err != nil {
return err
}
}
}
return nil
}
func dbDeviceConfig(db *sql.DB, id int, isprofile bool) (types.Device, error) {
var query string
var key, value string
newdev := types.Device{} // That's a map[string]string
inargs := []interface{}{id}
outfmt := []interface{}{key, value}
if isprofile {
query = `SELECT key, value FROM profiles_devices_config WHERE profile_device_id=?`
} else {
query = `SELECT key, value FROM containers_devices_config WHERE container_device_id=?`
}
results, err := dbQueryScan(db, query, inargs, outfmt)
if err != nil {
return newdev, err
}
for _, r := range results {
key = r[0].(string)
value = r[1].(string)
newdev[key] = value
}
return newdev, nil
}
func dbDevices(db *sql.DB, qName string, isprofile bool) (types.Devices, error) {
var q string
if isprofile {
q = `SELECT profiles_devices.id, profiles_devices.name, profiles_devices.type
FROM profiles_devices JOIN profiles
ON profiles_devices.profile_id = profiles.id
WHERE profiles.name=?`
} else {
q = `SELECT containers_devices.id, containers_devices.name, containers_devices.type
FROM containers_devices JOIN containers
ON containers_devices.container_id = containers.id
WHERE containers.name=?`
}
var id, dtype int
var name, stype string
inargs := []interface{}{qName}
outfmt := []interface{}{id, name, dtype}
results, err := dbQueryScan(db, q, inargs, outfmt)
if err != nil {
return nil, err
}
devices := types.Devices{}
for _, r := range results {
id = r[0].(int)
name = r[1].(string)
stype, err = dbDeviceTypeToString(r[2].(int))
if err != nil {
return nil, err
}
newdev, err := dbDeviceConfig(db, id, isprofile)
if err != nil {
return nil, err
}
newdev["type"] = stype
devices[name] = newdev
}
return devices, nil
}