added snippet upload
This commit is contained in:
parent
349afa60e1
commit
861ca4edf7
7 changed files with 281 additions and 81 deletions
103
snippet.go
Normal file
103
snippet.go
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
// Copyright (C) 2020 Marius Schellenberger
|
||||
|
||||
package pve
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNoSnippetNode = errors.New("missing snippet target node")
|
||||
ErrNoSnippetStorage = errors.New("missing snippet target storage")
|
||||
ErrNoSnippetName = errors.New("missing snippet name")
|
||||
)
|
||||
|
||||
type CopyBuffer struct {
|
||||
*bytes.Buffer
|
||||
}
|
||||
|
||||
func (c *CopyBuffer) Copy() *bytes.Buffer {
|
||||
buf := make([]byte, c.Len())
|
||||
copy(buf, c.Bytes())
|
||||
return bytes.NewBuffer(buf)
|
||||
}
|
||||
|
||||
type SnippetClient struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
func (c *SnippetClient) Create(ctx context.Context, node, storage, name string, data []byte) (t *Task, err error) {
|
||||
if node == "" {
|
||||
err = ErrNoSnippetNode
|
||||
return
|
||||
}
|
||||
if storage == "" {
|
||||
err = ErrNoSnippetStorage
|
||||
return
|
||||
}
|
||||
if name == "" {
|
||||
err = ErrNoSnippetName
|
||||
return
|
||||
}
|
||||
var b bytes.Buffer
|
||||
w := multipart.NewWriter(&b)
|
||||
err = w.WriteField("content", "snippets")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fw, err := w.CreateFormFile("filename", name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = fw.Write(data); err != nil {
|
||||
return
|
||||
}
|
||||
w.Close()
|
||||
cb := CopyBuffer{&b}
|
||||
ct := w.FormDataContentType()
|
||||
var taskid string
|
||||
var req *http.Request
|
||||
var resp *http.Response
|
||||
for i := 0; i < 3; i++ {
|
||||
buf := cb.Copy()
|
||||
req, err = c.client.NewRequest(ctx, "POST", fmt.Sprintf("/nodes/%s/storage/%s/upload", node, storage), buf)
|
||||
req.Header.Set("Content-Type", ct)
|
||||
resp, err = c.client.Do(req, &taskid)
|
||||
if err != nil {
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
// TODO log error
|
||||
continue
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
// TODO log error
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
t = c.client.Task.MustGet(ctx, taskid)
|
||||
return
|
||||
}
|
||||
|
||||
func SnippetVolume(storage, name string) string {
|
||||
return fmt.Sprintf("%s:snippets/%s", storage, name)
|
||||
}
|
||||
|
||||
func (c *SnippetClient) Delete(ctx context.Context, node, storage, name string) (t *Task, err error) {
|
||||
//volume := fmt.Sprintf("/%s:snippets/%s", storage, name)
|
||||
volume := SnippetVolume(storage, name)
|
||||
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/nodes/%s/storage/%s/content/%s", node, storage, volume), nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var taskid string
|
||||
_, err = c.client.Do(req, &taskid)
|
||||
t = c.client.Task.MustGet(ctx, taskid)
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue