initial commit
This commit is contained in:
commit
501d579a90
35 changed files with 11357 additions and 0 deletions
74
pool.go
Normal file
74
pool.go
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
// 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue