74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
// Copyright (C) 2020 Marius Schellenberger
|
|
|
|
package pve
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
var (
|
|
ErrPoolExists = errors.New("pool already exists")
|
|
ErrPoolNotExists = errors.New("pool does not exist")
|
|
)
|
|
|
|
func MakePoolName(namespace, name string) string {
|
|
return fmt.Sprintf("%s-%s", namespace, name)
|
|
}
|
|
|
|
type PoolClient struct {
|
|
client *Client
|
|
}
|
|
|
|
func (c *PoolClient) exists(ctx context.Context, name string) (status int, err error) {
|
|
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/pools/%s", name), nil)
|
|
if err != nil {
|
|
return
|
|
}
|
|
resp, err := c.client.Do(req, nil)
|
|
if err != nil {
|
|
if !errors.Is(err, HTTPErr) {
|
|
return
|
|
}
|
|
err = nil
|
|
}
|
|
status = resp.StatusCode
|
|
return
|
|
}
|
|
|
|
func (c *PoolClient) Create(ctx context.Context, name string) (err error) {
|
|
status, err := c.exists(ctx, name)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if status == http.StatusOK {
|
|
return ErrPoolExists
|
|
}
|
|
|
|
body := httpbody{"poolid": name}
|
|
req, err := c.client.NewRequest(ctx, "POST", "/pools", body.Reader())
|
|
if err != nil {
|
|
return
|
|
}
|
|
_, err = c.client.Do(req, nil)
|
|
return
|
|
}
|
|
|
|
func (c *PoolClient) Delete(ctx context.Context, name string) (err error) {
|
|
status, err := c.exists(ctx, name)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if status != http.StatusOK {
|
|
return ErrPoolNotExists
|
|
}
|
|
|
|
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/pools/%s", name), nil)
|
|
if err != nil {
|
|
return
|
|
}
|
|
_, err = c.client.Do(req, nil)
|
|
return
|
|
}
|