From c45b5422f995f0bdf9b72c1564c3029f1d8d466f Mon Sep 17 00:00:00 2001 From: Luc DUZAN Date: Mon, 26 Feb 2024 15:00:16 +0100 Subject: [PATCH] add info about upsert result --- client/client.go | 13 ++++++++++--- client/client_test.go | 9 ++++++--- cmd/apply.go | 4 ++-- docker/initializer.json | 2 +- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/client/client.go b/client/client.go index f1d28de..7b897f7 100644 --- a/client/client.go +++ b/client/client.go @@ -38,13 +38,20 @@ func MakeFromEnv(debug bool) Client { return Make(token, baseUrl, debug) } -func (client *Client) Apply(resource *resource.Resource) error { +func (client *Client) Apply(resource *resource.Resource) (string, error) { url := client.baseUrl + "/" + resource.Kind resp, err := client.client.R().SetBody(resource.Json).Put(url) if resp.IsError() { - return fmt.Errorf("Error applying resource %s/%s, got status code: %d:\n %s", resource.Kind, resource.Name, resp.StatusCode(), string(resp.Body())) + return "", fmt.Errorf("Error applying resource %s/%s, got status code: %d:\n %s", resource.Kind, resource.Name, resp.StatusCode(), string(resp.Body())) } - return err + bodyBytes := resp.Body() + var upsertResult string + err = json.Unmarshal(bodyBytes, &upsertResult) + //in case backend format change (not json string anymore). Let not fail the client for that + if err != nil { + return resp.String(), nil + } + return upsertResult, nil } func printResponseAsYaml(bytes []byte) error { diff --git a/client/client_test.go b/client/client_test.go index ab74310..a0b0e04 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -14,7 +14,7 @@ func TestApplyShouldWork(t *testing.T) { httpmock.ActivateNonDefault( client.client.GetClient(), ) - responder, err := httpmock.NewJsonResponder(200, "") + responder, err := httpmock.NewJsonResponder(200, `NotChanged`) if err != nil { panic(err) } @@ -35,10 +35,13 @@ func TestApplyShouldWork(t *testing.T) { responder, ) - err = client.Apply(&topic) + body, err := client.Apply(&topic) if err != nil { t.Error(err) } + if body != "NotChanged" { + t.Errorf("Bad result expected NotChanged got: %s", body) + } } func TestApplyShouldFailIfNo2xx(t *testing.T) { @@ -70,7 +73,7 @@ func TestApplyShouldFailIfNo2xx(t *testing.T) { responder, ) - err = client.Apply(&topic) + _, err = client.Apply(&topic) if err == nil { t.Failed() } diff --git a/cmd/apply.go b/cmd/apply.go index 044ee42..5317df6 100644 --- a/cmd/apply.go +++ b/cmd/apply.go @@ -23,12 +23,12 @@ var applyCmd = &cobra.Command{ } client := client.MakeFromEnv(*debug) for _, resource := range resources { - err := client.Apply(&resource) + upsertResult, err := client.Apply(&resource) if err != nil { fmt.Fprintf(os.Stderr, "Could not apply resource %s/%s: %s\n", resource.Kind, resource.Name, err) os.Exit(1) } else { - fmt.Printf("%s/%s: ok\n", resource.Kind, resource.Name) + fmt.Printf("%s/%s: %s\n", resource.Kind, resource.Name, upsertResult) } } }, diff --git a/docker/initializer.json b/docker/initializer.json index 8b20b6b..36fb2e0 100644 --- a/docker/initializer.json +++ b/docker/initializer.json @@ -9,7 +9,7 @@ }, "httpResponse": { "statusCode": 200, - "body": "{}", + "body": "\"Created\"", "headers": { "Content-Type": ["application/json"] }