-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
89 lines (79 loc) · 2.57 KB
/
pool.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
package zvirt
import (
"github.com/facebookgo/ensure"
pb "github.com/ganshane/zvirt/protocol"
"github.com/libvirt/libvirt-go"
"golang.org/x/net/context"
)
//Pool storage pool for libvirt
type Pool struct {
agent *Agent
}
//Define define an inactive persistent storage pool or modify an existing persistent one from an XML file
func (zpool *Pool) Define(ctx context.Context, request *pb.PoolDefineRequest) (*pb.PoolUUID, error) {
poolConn, err := zpool.agent.connectionPool.Acquire()
ensure.Nil(zpool.agent, err)
defer zpool.agent.connectionPool.Release(poolConn)
conn := poolConn.(*libvirtConnWrapper).conn
pool, err := conn.StoragePoolDefineXML(request.Xml, 0)
if err != nil {
return nil, err
}
defer pool.Free()
uuid, err := pool.GetUUIDString()
if err != nil {
return nil, err
}
return &pb.PoolUUID{Uuid: uuid}, nil
}
//Info - storage pool information
func (zpool *Pool) Info(ctx context.Context, uuid *pb.PoolUUID) (*pb.PoolStateResponse, error) {
poolConn, err := zpool.agent.connectionPool.Acquire()
ensure.Nil(zpool.agent, err)
defer zpool.agent.connectionPool.Release(poolConn)
conn := poolConn.(*libvirtConnWrapper).conn
pool, err := conn.LookupStoragePoolByUUIDString(uuid.Uuid)
if err != nil {
return nil, err
}
defer pool.Free()
info, err := pool.GetInfo()
if err != nil {
return nil, err
}
return &pb.PoolStateResponse{State: pb.PoolState(info.State)}, nil
}
//Start start a (previously defined) inactive pool
func (zpool *Pool) Start(ctx context.Context, poolUUID *pb.PoolUUID) (*pb.PoolStateResponse, error) {
poolConn, err := zpool.agent.connectionPool.Acquire()
ensure.Nil(zpool.agent, err)
defer zpool.agent.connectionPool.Release(poolConn)
conn := poolConn.(*libvirtConnWrapper).conn
pool, err := conn.LookupStoragePoolByUUIDString(poolUUID.Uuid)
if err != nil {
return nil, err
}
defer pool.Free()
err = pool.Create(libvirt.STORAGE_POOL_CREATE_NORMAL)
if err != nil {
return nil, err
}
return &pb.PoolStateResponse{State: pb.PoolState_STORAGE_POOL_RUNNING}, nil
}
//Destroy destroy (stop) a pool
func (zpool *Pool) Destroy(ctx context.Context, poolUUID *pb.PoolUUID) (*pb.PoolStateResponse, error) {
poolConn, err := zpool.agent.connectionPool.Acquire()
ensure.Nil(zpool.agent, err)
defer zpool.agent.connectionPool.Release(poolConn)
conn := poolConn.(*libvirtConnWrapper).conn
pool, err := conn.LookupStoragePoolByUUIDString(poolUUID.Uuid)
if err != nil {
return nil, err
}
defer pool.Free()
err = pool.Destroy()
if err != nil {
return nil, err
}
return &pb.PoolStateResponse{State: pb.PoolState_STORAGE_POOL_INACCESSIBLE}, nil
}