Skip to content

Commit

Permalink
dry mode
Browse files Browse the repository at this point in the history
  • Loading branch information
strokyl committed Mar 4, 2024
1 parent 62dac70 commit a981ed0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
27 changes: 22 additions & 5 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,40 @@ func MakeFromEnv(debug bool) Client {
return Make(token, baseUrl, debug)
}

func (client *Client) Apply(resource *resource.Resource) (string, error) {
type UpsertResponse struct {
UpsertResult string
}

func (client *Client) Apply(resource *resource.Resource, dryMode bool) (string, error) {
url := client.baseUrl + "/" + resource.Kind
resp, err := client.client.R().SetBody(resource.Json).Put(url)
builder := client.client.R().SetBody(resource.Json)
if dryMode {
builder = builder.SetQueryParam("dryMode", "true")
}
resp, err := builder.Put(url)
if err != nil {
return "", err
}
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()))
}
bodyBytes := resp.Body()
var upsertResult string
err = json.Unmarshal(bodyBytes, &upsertResult)
var upsertResponse UpsertResponse
err = json.Unmarshal(bodyBytes, &upsertResponse)
//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
if dryMode && upsertResponse.UpsertResult == "Created" {
return "To be created", nil
}
if dryMode && upsertResponse.UpsertResult == "Updated" {
return "To be updated", nil
}
if dryMode && upsertResponse.UpsertResult == "NotChanged" {
return "Nothing to do", nil
}
return upsertResponse.UpsertResult, nil
}

func printResponseAsYaml(bytes []byte) error {
Expand Down
44 changes: 38 additions & 6 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ func TestApplyShouldWork(t *testing.T) {
httpmock.ActivateNonDefault(
client.client.GetClient(),
)
responder, err := httpmock.NewJsonResponder(200, `NotChanged`)
if err != nil {
panic(err)
}
responder := httpmock.NewStringResponder(200, `{"upsertResult": "NotChanged"}`)

topic := resource.Resource{
Json: []byte(`{"yolo": "data"}`),
Expand All @@ -35,7 +32,7 @@ func TestApplyShouldWork(t *testing.T) {
responder,
)

body, err := client.Apply(&topic)
body, err := client.Apply(&topic, false)
if err != nil {
t.Error(err)
}
Expand All @@ -44,6 +41,41 @@ func TestApplyShouldWork(t *testing.T) {
}
}

func TestApplyWithDryModeShouldWork(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl/api"
token := "aToken"
client := Make(token, baseUrl, false)
httpmock.ActivateNonDefault(
client.client.GetClient(),
)
responder := httpmock.NewStringResponder(200, `{"upsertResult": "NotChanged"}`)

topic := resource.Resource{
Json: []byte(`{"yolo": "data"}`),
Kind: "topic",
Name: "toto",
Version: "v1",
}

httpmock.RegisterMatcherResponderWithQuery(
"PUT",
"http://baseUrl/api/topic",
"dryMode=true",
httpmock.HeaderIs("Authorization", "Bearer "+token).
And(httpmock.BodyContainsBytes(topic.Json)),
responder,
)

body, err := client.Apply(&topic, true)
if err != nil {
t.Error(err)
}
if body != "Nothing to do" {
t.Errorf("Bad result expected NotChanged got: %s", body)
}
}

func TestApplyShouldFailIfNo2xx(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl/api"
Expand Down Expand Up @@ -73,7 +105,7 @@ func TestApplyShouldFailIfNo2xx(t *testing.T) {
responder,
)

_, err = client.Apply(&topic)
_, err = client.Apply(&topic, false)
if err == nil {
t.Failed()
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
)

var filePath *[]string
var dryRun *bool

// applyCmd represents the apply command
var applyCmd = &cobra.Command{
Use: "apply",
Short: "upsert a resource on Conduktor",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
var resources []resource.Resource = make([]resource.Resource, 0)
var resources = make([]resource.Resource, 0)
for _, path := range *filePath {
r, err := resourceForPath(path)
if err != nil {
Expand All @@ -27,7 +28,7 @@ var applyCmd = &cobra.Command{
}
client := client.MakeFromEnv(*debug)
for _, resource := range resources {
upsertResult, err := client.Apply(&resource)
upsertResult, err := client.Apply(&resource, *dryRun)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not apply resource %s/%s: %s\n", resource.Kind, resource.Name, err)
os.Exit(1)
Expand Down Expand Up @@ -58,6 +59,9 @@ func init() {
filePath = applyCmd.
PersistentFlags().StringArrayP("file", "f", make([]string, 0, 0), "Specify the files to apply")

dryRun = applyCmd.
PersistentFlags().Bool("dry-mode", false, "Don't really apply change but check on backend the effect if applied")

applyCmd.MarkPersistentFlagRequired("file")
}

Expand Down

0 comments on commit a981ed0

Please sign in to comment.