Skip to content

Commit 9009960

Browse files
committed
storage/task
1 parent 18c6a8d commit 9009960

File tree

3 files changed

+124
-4
lines changed

3 files changed

+124
-4
lines changed

proxmox/qemu.go

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
)
1313

1414
type VirtualMachine struct {
15+
service *Service
1516
restclient *rest.RESTClient
1617
Node string
1718
VM *api.VirtualMachine
@@ -52,11 +53,22 @@ func (s *Service) VirtualMachine(ctx context.Context, vmid int) (*VirtualMachine
5253
}
5354
return nil, err
5455
}
55-
return &VirtualMachine{restclient: s.restclient, VM: vm, Node: node.Node}, nil
56+
return &VirtualMachine{service: s, restclient: s.restclient, VM: vm, Node: node.Node}, nil
5657
}
5758
return nil, rest.NotFoundErr
5859
}
5960

61+
func (s *Service) CreateVirtualMachine(ctx context.Context, node string, vmid int, option api.VirtualMachineCreateOptions) (*VirtualMachine, error) {
62+
taskid, err := s.restclient.CreateVirtualMachine(ctx, node, vmid, option)
63+
if err != nil {
64+
return nil, err
65+
}
66+
if err := s.EnsureTaskDone(ctx, node, *taskid); err != nil {
67+
return nil, err
68+
}
69+
return s.VirtualMachine(ctx, vmid)
70+
}
71+
6072
func (s *Service) VirtualMachineFromUUID(ctx context.Context, uuid string) (*VirtualMachine, error) {
6173
nodes, err := s.Nodes(ctx)
6274
if err != nil {
@@ -72,19 +84,19 @@ func (s *Service) VirtualMachineFromUUID(ctx context.Context, uuid string) (*Vir
7284
if err != nil {
7385
return nil, err
7486
}
75-
vmuuid, err := convertSMBiosToUUID(config.SMBios1)
87+
vmuuid, err := ConvertSMBiosToUUID(config.SMBios1)
7688
if err != nil {
7789
return nil, err
7890
}
7991
if vmuuid == uuid {
80-
return &VirtualMachine{restclient: s.restclient, VM: vm, Node: node.Node, config: config}, nil
92+
return &VirtualMachine{service: s, restclient: s.restclient, VM: vm, Node: node.Node, config: config}, nil
8193
}
8294
}
8395
}
8496
return nil, rest.NotFoundErr
8597
}
8698

87-
func convertSMBiosToUUID(smbios string) (string, error) {
99+
func ConvertSMBiosToUUID(smbios string) (string, error) {
88100
re := regexp.MustCompile(fmt.Sprintf("uuid=%s", UUIDFormat))
89101
match := re.FindString(smbios)
90102
if match == "" {
@@ -94,6 +106,15 @@ func convertSMBiosToUUID(smbios string) (string, error) {
94106
return strings.Split(match, "=")[1], nil
95107
}
96108

109+
func (c *VirtualMachine) Delete(ctx context.Context) error {
110+
path := fmt.Sprintf("/nodes/%s/qemu/%d", c.Node, c.VM.VMID)
111+
var upid string
112+
if err := c.restclient.Delete(ctx, path, nil, &upid); err != nil {
113+
return err
114+
}
115+
return c.service.EnsureTaskDone(ctx, c.Node, upid)
116+
}
117+
97118
func (c *VirtualMachine) GetConfig(ctx context.Context) (*api.VirtualMachineConfig, error) {
98119
if c.config != nil {
99120
return c.config, nil
@@ -114,3 +135,46 @@ func (c *VirtualMachine) GetOSInfo(ctx context.Context) (*api.OSInfo, error) {
114135
}
115136
return osInfo, nil
116137
}
138+
139+
// size : The new size. With the `+` sign the value is added to the actual size of the volume
140+
// and without it, the value is taken as an absolute one.
141+
// Shrinking disk size is not supported.
142+
// size format : \+?\d+(\.\d+)?[KMGT]?j
143+
func (c *VirtualMachine) ResizeVolume(ctx context.Context, disk, size string) error {
144+
path := fmt.Sprintf("/nodes/%s/qemu/%d/resize", c.Node, c.VM.VMID)
145+
request := make(map[string]interface{})
146+
request["disk"] = disk
147+
request["size"] = size
148+
var v interface{}
149+
if err := c.restclient.Put(ctx, path, request, &v); err != nil {
150+
return err
151+
}
152+
return nil
153+
}
154+
155+
func (c *VirtualMachine) Start(ctx context.Context) error {
156+
path := fmt.Sprintf("/nodes/%s/qemu/%d/status/start", c.Node, c.VM.VMID)
157+
var upid string
158+
if err := c.restclient.Post(ctx, path, nil, &upid); err != nil {
159+
return err
160+
}
161+
return c.service.EnsureTaskDone(ctx, c.Node, upid)
162+
}
163+
164+
func (c *VirtualMachine) Stop(ctx context.Context) error {
165+
path := fmt.Sprintf("/nodes/%s/qemu/%d/status/stop", c.Node, c.VM.VMID)
166+
var upid string
167+
if err := c.restclient.Post(ctx, path, nil, &upid); err != nil {
168+
return err
169+
}
170+
return c.service.EnsureTaskDone(ctx, c.Node, upid)
171+
}
172+
173+
func (c *VirtualMachine) Resume(ctx context.Context) error {
174+
path := fmt.Sprintf("/nodes/%s/qemu/%d/status/resume", c.Node, c.VM.VMID)
175+
var upid string
176+
if err := c.restclient.Post(ctx, path, nil, &upid); err != nil {
177+
return err
178+
}
179+
return c.service.EnsureTaskDone(ctx, c.Node, upid)
180+
}

proxmox/storage.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,22 @@ func (s *Storage) GetContent(ctx context.Context, volumeID string) (*api.Storage
6161
}
6262
return nil, rest.NotFoundErr
6363
}
64+
65+
func (s *Storage) GetVolume(ctx context.Context, volumeID string) (*api.StorageVolume, error) {
66+
path := fmt.Sprintf("/nodes/%s/storage/%s/content/%s", s.Node, s.Storage.Storage, volumeID)
67+
var volume *api.StorageVolume
68+
if err := s.restclient.Get(ctx, path, &volume); err != nil {
69+
return nil, err
70+
}
71+
return volume, nil
72+
}
73+
74+
// to do : taskid
75+
func (s *Storage) DeleteVolume(ctx context.Context, volumeID string) error {
76+
path := fmt.Sprintf("/nodes/%s/storage/%s/content/%s", s.Node, s.Storage.Storage, volumeID)
77+
var taskid string
78+
if err := s.restclient.Delete(ctx, path, nil, &taskid); err != nil {
79+
return err
80+
}
81+
return nil
82+
}

proxmox/task.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package proxmox
2+
3+
import (
4+
"context"
5+
"errors"
6+
"time"
7+
8+
"github.com/sp-yduck/proxmox-go/api"
9+
"github.com/sp-yduck/proxmox-go/rest"
10+
)
11+
12+
const (
13+
TaskStatusOK = "OK"
14+
)
15+
16+
func (s *Service) MustGetTask(ctx context.Context, node string, upid string) (*api.Task, error) {
17+
for i := 0; i < 10; i++ {
18+
task, err := s.restclient.GetTask(ctx, node, upid)
19+
if rest.IsNotFound(err) {
20+
time.Sleep(time.Second * 1)
21+
continue
22+
}
23+
return task, nil
24+
}
25+
return nil, errors.New("task wait deadline exceeded")
26+
}
27+
28+
func (s *Service) EnsureTaskDone(ctx context.Context, node, upid string) error {
29+
task, err := s.MustGetTask(ctx, node, upid)
30+
if err != nil {
31+
return err
32+
}
33+
if task.Status != TaskStatusOK {
34+
return errors.New(task.Status)
35+
}
36+
return nil
37+
}

0 commit comments

Comments
 (0)