From 973de5c688ce2c8b67680ef9798bfde5b51e1ce7 Mon Sep 17 00:00:00 2001 From: Luther Monson Date: Fri, 25 Aug 2023 17:41:08 -0700 Subject: [PATCH] refactor pools to drop PoolAPI and add pool resource filters --- pools.go | 47 +++++++++++++++++++++++--------------- pools_test.go | 21 ++++++++--------- tests/mocks/pve7x/pools.go | 14 +++++++++++- types.go | 13 ++--------- 4 files changed, 54 insertions(+), 41 deletions(-) diff --git a/pools.go b/pools.go index 31343c5..2caad79 100644 --- a/pools.go +++ b/pools.go @@ -1,36 +1,47 @@ package proxmox -import "fmt" - -func (c *Client) Pools() *PoolAPI { - poolapi := &PoolAPI{ - client: c, - } - return poolapi +import ( + "fmt" + "net/url" + "strings" +) + +func (c *Client) NewPool(poolid, comment string) error { + return c.Post("/pools", map[string]string{ + "poolid": poolid, + "comment": comment, + }, nil) } -func (p *PoolAPI) List() (pools Pools, err error) { - err = p.client.Get("/pools", &pools) +func (c *Client) Pools() (pools Pools, err error) { + err = c.Get("/pools", &pools) for _, pool := range pools { - pool.client = p.client + pool.client = c } return } -func (p *PoolAPI) Get(name string) (pool *Pool, err error) { - if err = p.client.Get(fmt.Sprintf("/pools/%s", name), &pool); err != nil { +// Pool optional filter of cluster resources by type, enum can be "qemu", "lxc", "storage". +func (c *Client) Pool(poolid string, filters ...string) (pool *Pool, err error) { + u := url.URL{Path: fmt.Sprintf("/pools/%s", poolid)} + + // filters are variadic because they're optional, munging everything passed into one big string to make + // a good request and the api will error out if there's an issue + if f := strings.Replace(strings.Join(filters, ""), " ", "", -1); f != "" { + params := url.Values{} + params.Add("type", f) + u.RawQuery = params.Encode() + } + + if err = c.Get(u.String(), &pool); err != nil { return nil, err } - pool.PoolID = name - pool.client = p.client + pool.PoolID = poolid + pool.client = c return } -func (p *PoolAPI) Create(opt *PoolCreateOption) error { - return p.client.Post("/pools", opt, nil) -} - func (p *Pool) Update(opt *PoolUpdateOption) error { return p.client.Put(fmt.Sprintf("/pools/%s", p.PoolID), opt, nil) } diff --git a/pools_test.go b/pools_test.go index cccc5d7..d5c6be4 100644 --- a/pools_test.go +++ b/pools_test.go @@ -1,18 +1,20 @@ package proxmox import ( + "testing" + "github.com/luthermonson/go-proxmox/tests/mocks" "github.com/stretchr/testify/assert" - "testing" ) -func TestPoolList(t *testing.T) { +func TestPools(t *testing.T) { mocks.On(mockConfig) defer mocks.Off() client := mockClient() - _, err := client.Pools().List() + pools, err := client.Pools() assert.Nil(t, err) + assert.Len(t, pools, 1) } func TestPoolGet(t *testing.T) { @@ -20,13 +22,13 @@ func TestPoolGet(t *testing.T) { defer mocks.Off() client := mockClient() - pool, err := client.Pools().Get("test-pool") + pool, err := client.Pool("test-pool") assert.Nil(t, err) assert.NotNil(t, pool) if pool != nil { assert.Equal(t, "test-pool", pool.PoolID) assert.Equal(t, "Test pool", pool.Comment) - assert.Len(t, pool.Members, 2) + assert.Len(t, pool.Members, 3) } } @@ -35,10 +37,7 @@ func TestPoolCreate(t *testing.T) { defer mocks.Off() client := mockClient() - err := client.Pools().Create(&PoolCreateOption{ - PoolID: "test-pool", - Comment: "Test pool", - }) + err := client.NewPool("test-pool", "Test pool") assert.Nil(t, err) } @@ -47,7 +46,7 @@ func TestPoolUpdate(t *testing.T) { defer mocks.Off() client := mockClient() - pool, err := client.Pools().Get("test-pool") + pool, err := client.Pool("test-pool") assert.Nil(t, err) assert.NotNil(t, pool) @@ -67,7 +66,7 @@ func TestPoolDelete(t *testing.T) { defer mocks.Off() client := mockClient() - pool, err := client.Pools().Get("test-pool") + pool, err := client.Pool("test-pool") assert.Nil(t, err) assert.NotNil(t, pool) diff --git a/tests/mocks/pve7x/pools.go b/tests/mocks/pve7x/pools.go index c6a0757..e90f9a0 100644 --- a/tests/mocks/pve7x/pools.go +++ b/tests/mocks/pve7x/pools.go @@ -62,7 +62,19 @@ func pool() { "vmid": 106, "maxdisk": 10737418240, "maxmem": 2147483648 - } + }, + { + "node": "node1", + "maxdisk": 948340654080, + "type": "storage", + "id": "storage/node1/local", + "status": "available", + "storage": "local", + "content": "backup,vztmpl,iso", + "plugintype": "dir", + "disk": 10486939648, + "shared": 0 + } ] } }`) diff --git a/types.go b/types.go index 402a1f4..50f87c4 100644 --- a/types.go +++ b/types.go @@ -916,10 +916,6 @@ type Snapshot struct { Snapstate string } -type PoolAPI struct { - client *Client -} - type Pools []*Pool type Pool struct { client *Client @@ -928,18 +924,13 @@ type Pool struct { Members []ClusterResource `json:"members,omitempty"` } -type PoolCreateOption struct { - PoolID string `json:"poolid"` - Comment string `json:"comment,omitempty"` -} - type PoolUpdateOption struct { Comment string `json:"comment,omitempty"` // Delete objects rather than adding them Delete bool `json:"delete,omitempty"` - // Add or delete storage objects + // Comma separated lists of Storage names to add/delete to the pool Storage string `json:"storage,omitempty"` - // Add or delete virtual machine objects + // Comma separated lists of Virtual Machine IDs to add/delete to the pool VirtualMachines string `json:"vms,omitempty"` }