diff --git a/go-client/go.mod b/go-client/go.mod index fde56be916..03833ac9a8 100644 --- a/go-client/go.mod +++ b/go-client/go.mod @@ -2,7 +2,10 @@ module github.com/windmill-labs/windmill-go-client go 1.19 -require github.com/deepmap/oapi-codegen v1.15.0 +require ( + github.com/deepmap/oapi-codegen v1.15.0 + github.com/stretchr/testify v1.8.4 +) require ( github.com/BurntSushi/toml v1.3.2 // indirect @@ -16,6 +19,7 @@ require ( github.com/bytedance/sonic v1.10.0-rc3 // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/iasm v0.9.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/fatih/structs v1.1.0 // indirect github.com/flosch/pongo2/v4 v4.0.2 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect @@ -51,6 +55,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.9 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/schollz/closestmatch v2.1.0+incompatible // indirect github.com/sirupsen/logrus v1.8.1 // indirect diff --git a/go-client/windmill.go b/go-client/windmill.go index eefedd48f2..fd0446e14b 100644 --- a/go-client/windmill.go +++ b/go-client/windmill.go @@ -42,12 +42,12 @@ func GetVariable(path string) (string, error) { return "", err } res, err := client.Client.GetVariableValueWithResponse(context.Background(), client.Workspace, path) - if res.StatusCode()/100 != 2 { - return "", errors.New(string(res.Body)) - } if err != nil { return "", err } + if res.StatusCode()/100 != 2 { + return "", errors.New(string(res.Body)) + } return *res.JSON200, nil } @@ -58,28 +58,50 @@ func GetResource(path string) (interface{}, error) { } params := api.GetResourceValueInterpolatedParams{} res, err := client.Client.GetResourceValueInterpolatedWithResponse(context.Background(), client.Workspace, path, ¶ms) - if res.StatusCode()/100 != 2 { - return nil, errors.New(string(res.Body)) - } if err != nil { return nil, err } + if res.StatusCode()/100 != 2 { + return nil, errors.New(string(res.Body)) + } return *res.JSON200, nil } -func SetResource(path string, value interface{}) error { +func SetResource(path string, value interface{}, resourceTypeOpt ...string) error { client, err := GetClient() - res, err := client.Client.CreateResourceWithResponse(context.Background(), client.Workspace, &api.CreateResourceParams{ - UpdateIfExists: newBool(true), - }, api.CreateResource{Value: &value, Path: path}) - if err != nil { return err } - if res.StatusCode()/100 != 2 { - return errors.New(string(res.Body)) + params := api.GetResourceValueInterpolatedParams{} + getRes, getErr := client.Client.GetResourceValueInterpolatedWithResponse(context.Background(), client.Workspace, path, ¶ms) + if getErr != nil { + return getErr + } + if getRes.StatusCode() == 404 { + resourceType := "any" + if len(resourceTypeOpt) > 0 { + resourceType = resourceTypeOpt[0] + } + res, err := client.Client.CreateResourceWithResponse(context.Background(), client.Workspace, &api.CreateResourceParams{ + UpdateIfExists: newBool(true), + }, api.CreateResource{Value: &value, Path: path, ResourceType: resourceType}) + if err != nil { + return err + } + if res.StatusCode()/100 != 2 { + return errors.New(string(res.Body)) + } + } else { + res, err := client.Client.UpdateResourceValueWithResponse(context.Background(), client.Workspace, path, api.UpdateResourceValueJSONRequestBody{ + Value: &value, + }) + if err != nil { + return err + } + if res.StatusCode()/100 != 2 { + return errors.New(string(res.Body)) + } } - return nil } @@ -96,20 +118,20 @@ func SetVariable(path string, value string) error { if res.StatusCode()/100 != 2 { f = true } - if f == true { - res, err := client.Client.CreateVariableWithResponse(context.Background(), client.Workspace, &api.CreateVariableParams{}, - api.CreateVariableJSONRequestBody{ - Path: path, - Value: value, - }) + if f { + res, err := client.Client.CreateVariableWithResponse(context.Background(), client.Workspace, &api.CreateVariableParams{}, + api.CreateVariableJSONRequestBody{ + Path: path, + Value: value, + }) - if err != nil { - return err - } - if res.StatusCode()/100 != 2 { - return errors.New(string(res.Body)) - } - } + if err != nil { + return err + } + if res.StatusCode()/100 != 2 { + return errors.New(string(res.Body)) + } + } return nil } diff --git a/go-client/windmill_test.go b/go-client/windmill_test.go new file mode 100644 index 0000000000..c7e1afe6cf --- /dev/null +++ b/go-client/windmill_test.go @@ -0,0 +1,59 @@ +package windmill + +import ( + "encoding/json" + "os" + "testing" + + "github.com/stretchr/testify/require" +) + +func SetUp() error { + err := os.Setenv("BASE_INTERNAL_URL", "http://localhost:8000") + if err != nil { + return err + } + err = os.Setenv("WM_WORKSPACE", "storage") + if err != nil { + return err + } + err = os.Setenv("WM_TOKEN", "") + if err != nil { + return err + } + return nil +} + +func TestGetResource(t *testing.T) { + t.Skip("skipping") // uncomment to test + if err := SetUp(); err != nil { + t.Error(err) + } + res, err := GetResource("u/admin/test_res") + if err != nil { + t.Error(err) + } + serialized, err := json.Marshal(res) + if err != nil { + t.Error(err) + } + require.Equal(t, "{\"test\":\"test\"}", string(serialized)) +} + +func TestSetResource(t *testing.T) { + t.Skip("skipping") // uncomment to test + if err := SetUp(); err != nil { + t.Error(err) + } + path := "u/admin/test_res" + + type ResourceValue struct { + Test string `json:"test"` + } + value := ResourceValue{ + Test: "test3", + } + if err := SetResource(path, value); err != nil { + t.Error(err) + } +}