Skip to content

Commit

Permalink
Merge pull request #81 from luthermonson/pools-refactor
Browse files Browse the repository at this point in the history
refactor pools to drop PoolAPI and add pool resource filters
  • Loading branch information
luthermonson authored Aug 26, 2023
2 parents c38a376 + 973de5c commit ab42668
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 41 deletions.
47 changes: 29 additions & 18 deletions pools.go
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
21 changes: 10 additions & 11 deletions pools_test.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
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) {
mocks.On(mockConfig)
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)
}
}

Expand All @@ -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)
}

Expand All @@ -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)
Expand All @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion tests/mocks/pve7x/pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
}`)
Expand Down
13 changes: 2 additions & 11 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,10 +916,6 @@ type Snapshot struct {
Snapstate string
}

type PoolAPI struct {
client *Client
}

type Pools []*Pool
type Pool struct {
client *Client
Expand All @@ -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"`
}

Expand Down

0 comments on commit ab42668

Please sign in to comment.