Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dry mode #14

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
strokyl marked this conversation as resolved.
Show resolved Hide resolved

applyCmd.MarkPersistentFlagRequired("file")
}

Expand Down
Loading