Skip to content

Commit

Permalink
resource pool
Browse files Browse the repository at this point in the history
  • Loading branch information
sp-yduck committed Nov 3, 2023
1 parent af4d21c commit 1d00af3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
5 changes: 3 additions & 2 deletions api/pool_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ type ResourcePool struct {
Comment string `json:"comment"`
}

type ResourcePoolMember struct {
Members []map[string]interface{} `json:"members"`
type ResourcePoolConfig struct {
Comment string `json:"comment"`
Members []*map[string]interface{} `json:"members"`
}

type UpdateResourcePoolOption struct {
Expand Down
37 changes: 33 additions & 4 deletions proxmox/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package proxmox
import (
"context"
"fmt"
"strings"

"github.com/sp-yduck/proxmox-go/api"
)
Expand Down Expand Up @@ -35,12 +36,23 @@ func (s *Service) DeletePool(ctx context.Context, id string) error {
return s.restclient.DeleteResourcePool(ctx, id)
}

func IsAlreadyAPoolMemberErr(err error) bool {
return strings.Contains(err.Error(), "is already a pool member")
}

func IsNotAPoolMemberErr(err error) bool {
return strings.Contains(err.Error(), "is not a pool member")
}

func (p *Pool) AddVMs(ctx context.Context, vmids []int) error {
opts := api.UpdateResourcePoolOption{
PoolID: p.Pool.PoolID,
VMs: itoaSlice(vmids),
}
return p.service.restclient.UpdateResourcePool(ctx, opts)
if err := p.service.restclient.UpdateResourcePool(ctx, opts); err != nil && !IsAlreadyAPoolMemberErr(err) {
return err
}
return nil
}

func (p *Pool) RemoveVMs(ctx context.Context, vmids []int) error {
Expand All @@ -49,15 +61,21 @@ func (p *Pool) RemoveVMs(ctx context.Context, vmids []int) error {
VMs: itoaSlice(vmids),
Delete: true,
}
return p.service.restclient.UpdateResourcePool(ctx, opts)
if err := p.service.restclient.UpdateResourcePool(ctx, opts); err != nil && !IsNotAPoolMemberErr(err) {
return err
}
return nil
}

func (p *Pool) AddStorages(ctx context.Context, storageNames []string) error {
opts := api.UpdateResourcePoolOption{
PoolID: p.Pool.PoolID,
Storage: storageNames,
}
return p.service.restclient.UpdateResourcePool(ctx, opts)
if err := p.service.restclient.UpdateResourcePool(ctx, opts); err != nil && !IsAlreadyAPoolMemberErr(err) {
return err
}
return nil
}

func (p *Pool) RemoveStorages(ctx context.Context, storageNames []string) error {
Expand All @@ -66,7 +84,18 @@ func (p *Pool) RemoveStorages(ctx context.Context, storageNames []string) error
Storage: storageNames,
Delete: true,
}
return p.service.restclient.UpdateResourcePool(ctx, opts)
if err := p.service.restclient.UpdateResourcePool(ctx, opts); err != nil && !IsNotAPoolMemberErr(err) {
return err
}
return nil
}

func (p *Pool) GetMembers(ctx context.Context) ([]*map[string]interface{}, error) {
config, err := p.service.restclient.GetResourcePoolConfig(ctx, p.Pool.PoolID)
if err != nil {
return nil, err
}
return config.Members, nil
}

func itoaSlice(i []int) []string {
Expand Down
9 changes: 9 additions & 0 deletions rest/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@ func (c *RESTClient) UpdateResourcePool(ctx context.Context, option api.UpdateRe
path := fmt.Sprintf("/pools/%s", option.PoolID)
return c.Put(ctx, path, option, nil)
}

func (c *RESTClient) GetResourcePoolConfig(ctx context.Context, id string) (*api.ResourcePoolConfig, error) {
path := fmt.Sprintf("/pools/%s", id)
var config *api.ResourcePoolConfig
if err := c.Get(ctx, path, &config); err != nil {
return nil, err
}
return config, nil
}
11 changes: 11 additions & 0 deletions rest/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,14 @@ func (s *TestSuite) TestUpdateResourcePool() {
s.T().Fatalf("failed to update pool: %v", err)
}
}

func (s *TestSuite) TestGetResourcePoolConfig() {
config, err := s.restclient.GetResourcePoolConfig(context.Background(), "proxmox-go-test")
if err != nil {
s.T().Fatalf("failed to get pool config: %v", err)
}
s.T().Logf("get pool config: %v", *config)
// for _, m := range config.Members {
// s.T().Logf("get members: %v", m)
// }
}

0 comments on commit 1d00af3

Please sign in to comment.