Commit b5fe99a3 authored by Christian Jürges's avatar Christian Jürges
Browse files

added DataSetExists

parent 0c235b6b
.PHONY: all lint test help
all: test lint
# build: ## Build application
# go build .
lint: ## Lint the project
golangci-lint run ./...
test: ## Run all tests with race and cover
go test -race -cover -v ./...
help: ## Print all possible targets
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z0-9_-]+:.*?## / {gsub("\\\\n",sprintf("\n%22c",""), $$2);printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
\ No newline at end of file
......@@ -49,13 +49,11 @@ func (h *httpClient) Get(url string) ([]byte, error) {
case http.StatusOK:
i = maxRetries
err = nil
break
case http.StatusNotFound:
err = fmt.Errorf("%q: %w", url, ErrNotFound)
err = fmt.Errorf("%w: %v", ErrNotFound, url)
i = maxRetries
break
default:
err = fmt.Errorf("%q: %w", url, ErrGeneral)
err = fmt.Errorf("%w: %v", ErrGeneral, url)
}
}
// do not proceed if any error occurred before
......@@ -116,21 +114,17 @@ func (h *httpClient) Post(url string, body []byte) ([]byte, error) {
log.Debugf("response status code: %d, %v", response.StatusCode, response.Body)
err = nil
i = maxRetries
break
case http.StatusConflict:
log.Debugf("response status code: %d, %v", response.StatusCode, response.Body)
err = fmt.Errorf("%q, %q", url, ErrConflict)
err = fmt.Errorf("%w: %v", ErrConflict, url)
i = maxRetries
break
case http.StatusNotFound:
log.Debugf("response status code: %d, %v", response.StatusCode, response.Body)
err = fmt.Errorf("%q, %q", url, ErrNotFound)
err = fmt.Errorf("%w: %v", ErrNotFound, url)
i = maxRetries
break
default:
log.Debugf("response status code: %d, %v", response.StatusCode, response.Body)
err = fmt.Errorf("%q, %q", url, ErrGeneral)
err = fmt.Errorf("%w, %v", ErrGeneral, url)
time.Sleep(time.Second)
}
}
......@@ -190,15 +184,13 @@ func (h *httpClient) Delete(url string) ([]byte, error) {
case http.StatusNoContent:
i = maxRetries
err = nil
break
case http.StatusNotFound:
log.Debugf("response status code: %d", response.StatusCode)
err = fmt.Errorf("%q, %q", url, ErrNotFound)
err = fmt.Errorf("%w, %v", ErrNotFound, url)
i = maxRetries
break
default:
log.Errorf("error try %d getting %s, http status: %d", i, url, response.StatusCode)
err = fmt.Errorf("%q, %q", url, ErrGeneral)
err = fmt.Errorf("%w, %v", ErrGeneral, url)
time.Sleep(time.Second)
}
}
......
......@@ -25,8 +25,33 @@ func New(endpoint string) *zfsRestClient {
return &c
}
// DataSetExists verifies if a dataset exists or not.
// Returns false if err is not nil!
func (c *zfsRestClient) DataSetExists(pool, parentPath string) (
exists bool,
err error,
) {
var (
u string // URL
p = path.Join(consts.APIBasePath, consts.PoolsIdent, pool, parentPath, consts.FileSystemIdent)
)
u, err = c.createURL(p, 1, 0)
if err != nil {
return exists, err
}
_, err = c.HTTPClient.Get(u)
if err == nil {
exists = true
}
return exists, err
}
// ListFileSystem for a given path
func (c *zfsRestClient) ListFilesSystem(pool string, parentPath string, limit int, startToken string) (
func (c *zfsRestClient) ListFilesSystem(pool, parentPath string, limit int, startToken string) (
ds dataset.DataSets,
nextToken string,
err error,
......@@ -85,12 +110,10 @@ func (c *zfsRestClient) ListFilesSystem(pool string, parentPath string, limit in
}
// CreateFileSystem
func (c *zfsRestClient) CreateFileSystem(pool string, parentPath string, fsName string) (*filesystem.FileSystem, error) {
func (c *zfsRestClient) CreateFileSystem(pool string, parentPath string, fsName string) (fs *filesystem.FileSystem, err error, ) {
var (
fs *filesystem.FileSystem
fsPostProps filesystem.Props
body, response []byte
err error
u string
)
......@@ -100,6 +123,10 @@ func (c *zfsRestClient) CreateFileSystem(pool string, parentPath string, fsName
u, err = c.createURL(p, 0, 0)
if err != nil {
return nil, err
}
body, err = json.Marshal(fsPostProps)
if err != nil {
return nil, err
......@@ -119,7 +146,7 @@ func (c *zfsRestClient) CreateFileSystem(pool string, parentPath string, fsName
return fs, nil
}
// CreateFileSystem
// DestroyFileSystem
func (c *zfsRestClient) DestroyFileSystem(pool string, parentPath string, fsName string) error {
var body []byte
......
package client_test
import (
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"gitlab.eqipe.ch/chris/zfsrestclient/client"
......@@ -49,6 +50,47 @@ func TestZfsRestClient_CreateFileSystem(t *testing.T) {
}
}
// TestZfsRestClient_DataSetExists depends on TestZfsRestClient_CreateFileSystem
func TestZfsRestClient_DataSetExists(t *testing.T) {
var tests = []struct {
pool string
parentPath string
expect bool
expectedError error
}{
{pool: "test1", parentPath: "testFolder1", expect: true, expectedError: nil},
{pool: "test1", parentPath: "testFolder2", expect: true, expectedError: nil},
{pool: "test1", parentPath: "testFolder3", expect: true, expectedError: nil},
{pool: "test1", parentPath: "testFolder4", expect: true, expectedError: nil},
{pool: "test1", parentPath: "testFolder5", expect: true, expectedError: nil},
{pool: "test1", parentPath: "testFolder1/testSubFolder1", expect: true, expectedError: nil},
{pool: "test1", parentPath: "testFolder1/testSubFolder2", expect: true, expectedError: nil},
{pool: "test_does_not_exists", parentPath: "testFolder1", expect: false, expectedError: client.ErrNotFound},
{pool: "test1", parentPath: "folder_does_not_exists/testSubFolder2", expect: false, expectedError: client.ErrNotFound},
{pool: "test1", parentPath: "", expect: true},
}
var c = client.New(endPoint)
if c == nil {
t.Error("could not create zfs rest client.")
return
}
for _, tt := range tests {
t.Logf("testing %s", path.Join(tt.pool, tt.parentPath))
exists, err := c.DataSetExists(tt.pool, tt.parentPath)
if err != nil {
if !errors.Is(err, client.ErrNotFound) {
t.Error(err)
}
}
if exists != tt.expect {
t.Errorf("expected %t, got %t", tt.expect, exists)
}
}
}
func TestZfsRestClient_ListFilesSystem(t *testing.T) {
var tests = []struct {
pool string
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment