Skip to content

Commit af4d21c

Browse files
committed
resource pools
1 parent ef366c6 commit af4d21c

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed

api/pool_type.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package api
2+
3+
type ResourcePool struct {
4+
PoolID string `json:"poolid"`
5+
Comment string `json:"comment"`
6+
}
7+
8+
type ResourcePoolMember struct {
9+
Members []map[string]interface{} `json:"members"`
10+
}
11+
12+
type UpdateResourcePoolOption struct {
13+
PoolID string `json:"poolid"`
14+
Comment string `json:"comment,omitempty"`
15+
16+
// set true for removing object from resource pool
17+
Delete bool `json:"delete,omitempty"`
18+
19+
// array of storage name
20+
Storage []string `json:"storage,omitempty"`
21+
22+
// array of vmid
23+
VMs []string `json:"vms,omitempty"`
24+
}

proxmox/pool.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package proxmox
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/sp-yduck/proxmox-go/api"
8+
)
9+
10+
type Pool struct {
11+
service *Service
12+
Pool *api.ResourcePool
13+
}
14+
15+
func (s *Service) Pools(ctx context.Context) ([]*api.ResourcePool, error) {
16+
return s.restclient.GetResourcePools(ctx)
17+
}
18+
19+
func (s *Service) Pool(ctx context.Context, id string) (*Pool, error) {
20+
pool, err := s.restclient.GetResourcPool(ctx, id)
21+
if err != nil {
22+
return nil, err
23+
}
24+
return &Pool{service: s, Pool: pool}, nil
25+
}
26+
27+
func (s *Service) CreatePool(ctx context.Context, pool api.ResourcePool) (*Pool, error) {
28+
if err := s.restclient.CreateResourcePool(ctx, pool); err != nil {
29+
return nil, err
30+
}
31+
return s.Pool(ctx, pool.PoolID)
32+
}
33+
34+
func (s *Service) DeletePool(ctx context.Context, id string) error {
35+
return s.restclient.DeleteResourcePool(ctx, id)
36+
}
37+
38+
func (p *Pool) AddVMs(ctx context.Context, vmids []int) error {
39+
opts := api.UpdateResourcePoolOption{
40+
PoolID: p.Pool.PoolID,
41+
VMs: itoaSlice(vmids),
42+
}
43+
return p.service.restclient.UpdateResourcePool(ctx, opts)
44+
}
45+
46+
func (p *Pool) RemoveVMs(ctx context.Context, vmids []int) error {
47+
opts := api.UpdateResourcePoolOption{
48+
PoolID: p.Pool.PoolID,
49+
VMs: itoaSlice(vmids),
50+
Delete: true,
51+
}
52+
return p.service.restclient.UpdateResourcePool(ctx, opts)
53+
}
54+
55+
func (p *Pool) AddStorages(ctx context.Context, storageNames []string) error {
56+
opts := api.UpdateResourcePoolOption{
57+
PoolID: p.Pool.PoolID,
58+
Storage: storageNames,
59+
}
60+
return p.service.restclient.UpdateResourcePool(ctx, opts)
61+
}
62+
63+
func (p *Pool) RemoveStorages(ctx context.Context, storageNames []string) error {
64+
opts := api.UpdateResourcePoolOption{
65+
PoolID: p.Pool.PoolID,
66+
Storage: storageNames,
67+
Delete: true,
68+
}
69+
return p.service.restclient.UpdateResourcePool(ctx, opts)
70+
}
71+
72+
func itoaSlice(i []int) []string {
73+
a := []string{}
74+
for _, x := range i {
75+
a = append(a, fmt.Sprintf("%d", x))
76+
}
77+
return a
78+
}

rest/pool.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package rest
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/sp-yduck/proxmox-go/api"
8+
)
9+
10+
func (c *RESTClient) GetResourcePools(ctx context.Context) ([]*api.ResourcePool, error) {
11+
var pools []*api.ResourcePool
12+
if err := c.Get(ctx, "/pools", &pools); err != nil {
13+
return nil, err
14+
}
15+
return pools, nil
16+
}
17+
18+
func (c *RESTClient) GetResourcPool(ctx context.Context, id string) (*api.ResourcePool, error) {
19+
pools, err := c.GetResourcePools(ctx)
20+
if err != nil {
21+
return nil, err
22+
}
23+
for _, p := range pools {
24+
if p.PoolID == id {
25+
return p, nil
26+
}
27+
}
28+
return nil, NotFoundErr
29+
}
30+
31+
func (c *RESTClient) CreateResourcePool(ctx context.Context, pool api.ResourcePool) error {
32+
return c.Post(ctx, "/pools", pool, nil)
33+
}
34+
35+
func (c *RESTClient) DeleteResourcePool(ctx context.Context, id string) error {
36+
var res map[string]interface{}
37+
path := fmt.Sprintf("/pools/%s", id)
38+
return c.Delete(ctx, path, nil, &res)
39+
}
40+
41+
func (c *RESTClient) UpdateResourcePool(ctx context.Context, option api.UpdateResourcePoolOption) error {
42+
path := fmt.Sprintf("/pools/%s", option.PoolID)
43+
return c.Put(ctx, path, option, nil)
44+
}

rest/pool_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package rest
2+
3+
import (
4+
"context"
5+
6+
"github.com/sp-yduck/proxmox-go/api"
7+
)
8+
9+
func (s *TestSuite) TestGetResourcePools() {
10+
pools, err := s.restclient.GetResourcePools(context.Background())
11+
if err != nil {
12+
s.T().Fatalf("failed to get pools: %v", err)
13+
}
14+
s.T().Logf("get pools: %v", pools)
15+
}
16+
17+
func (s *TestSuite) TestCreateResourcePool() {
18+
pool := api.ResourcePool{
19+
PoolID: "proxmox-go-test",
20+
Comment: "proxmox-go-test comment",
21+
}
22+
if err := s.restclient.CreateResourcePool(context.Background(), pool); err != nil {
23+
s.T().Fatalf("failed to create pool: %v", err)
24+
}
25+
}
26+
27+
func (s *TestSuite) TestDeleteResourcePool() {
28+
if err := s.restclient.DeleteResourcePool(context.Background(), "proxmox-go-test"); err != nil {
29+
s.T().Fatalf("failed to delete pool: %v", err)
30+
}
31+
}
32+
33+
func (s *TestSuite) TestUpdateResourcePool() {
34+
opts := api.UpdateResourcePoolOption{
35+
PoolID: "proxmox-go-test",
36+
VMs: []string{"102", "103"},
37+
Delete: true,
38+
}
39+
if err := s.restclient.UpdateResourcePool(context.Background(), opts); err != nil {
40+
s.T().Fatalf("failed to update pool: %v", err)
41+
}
42+
}

0 commit comments

Comments
 (0)