-
Notifications
You must be signed in to change notification settings - Fork 0
/
volumes.go
90 lines (78 loc) · 1.43 KB
/
volumes.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
package models
const (
// todo FileDir-->VolFileDir
FileDir = iota
SSH
AWS
RBD
)
type Volume struct {
VolumeID int64
Name string
Backend int
AuthUser string
AuthPW string
Capacity int64
Free int64
Used int64
}
// GetVolumes returns all volumes.
func (c *Client) GetVolumes() ([]Volume, error) {
s := `select * from volumes order by volumeid asc`
rows, err := c.DB.Query(s)
if err != nil {
return []Volume{}, err
}
defer rows.Close()
vols := make([]Volume, 0, 16)
for rows.Next() {
v := Volume{}
err = rows.Scan(
&v.VolumeID,
&v.Name,
&v.Backend,
&v.AuthUser,
&v.AuthPW,
&v.Capacity,
&v.Free,
&v.Used)
if err != nil {
return []Volume{}, err
}
vols = append(vols, v)
}
return vols, nil
}
// InsertVolume inserts a new volume v.
func (c *Client) InsertVolume(v Volume) (int64, error) {
s := `insert into volumes values (?, ?, ?, ?, ?, ?, ?, ?)`
res, err := c.DB.Exec(s,
nil,
v.Name,
v.Backend,
v.AuthUser,
v.AuthPW,
v.Capacity,
v.Free,
v.Used)
if err != nil {
return 0, err
}
return res.LastInsertId()
}
// UpdateVolume updates a volume v identified by v.VolumeID.
func (c *Client) UpdateVolume(v Volume) error {
s := `update volumes
set name=?,backend=?,authuser=?,authpw=?,capacity=?,free=?,used=?
where volumeid=?`
_, err := c.DB.Exec(s,
v.Name,
v.Backend,
v.AuthUser,
v.AuthPW,
v.Capacity,
v.Free,
v.Used,
v.VolumeID)
return err
}