Skip to content

Commit

Permalink
add info about upsert result
Browse files Browse the repository at this point in the history
  • Loading branch information
strokyl committed Feb 26, 2024
1 parent f991acf commit c45b542
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
13 changes: 10 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 6 additions & 3 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) {
Expand Down Expand Up @@ -70,7 +73,7 @@ func TestApplyShouldFailIfNo2xx(t *testing.T) {
responder,
)

err = client.Apply(&topic)
_, err = client.Apply(&topic)
if err == nil {
t.Failed()
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion docker/initializer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"httpResponse": {
"statusCode": 200,
"body": "{}",
"body": "\"Created\"",
"headers": {
"Content-Type": ["application/json"]
}
Expand Down

0 comments on commit c45b542

Please sign in to comment.