Repository navigation
feat: trigger a building block run from the CLI #11
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
JohannesRudolph
wants to merge
1
commit into
feat/cli-bb-run-logs
from
feat/buildingblock-trigger-run
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ func New() *cobra.Command { | |
| } | ||
|
|
||
| cmd.AddCommand(newList()) | ||
| cmd.AddCommand(newTriggerRun()) | ||
|
|
||
| return cmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package buildingblock | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log/slog" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/meshcloud/meshstack-cli/client" | ||
| "github.com/meshcloud/meshstack-cli/cmd/internal" | ||
| ) | ||
|
|
||
| func newTriggerRun() *cobra.Command { | ||
| var ( | ||
| output internal.OutputFlag | ||
| dryRun bool | ||
| ) | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "trigger-run <building-block-uuid>", | ||
| Short: "Ask meshStack to run a building block", | ||
| Long: `Ask meshStack to run a building block, and write the building block meshStack answers with. | ||
|
|
||
| --dry-run asks for a dry (DETECT) run, which plans the block without changing it. | ||
|
|
||
| The command does not wait for the run. meshStack starts it asynchronously, and holds it until a | ||
| person approves it in meshPanel if the definition asks for approval. The status.latestRunUuid and | ||
| status.latestDryRunUuid of the answer may therefore still name an older run.`, | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx := cmd.Context() | ||
| meshStack, err := internal.ResolveClient(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| buildingBlockUuid := args[0] | ||
| buildingBlock, err := meshStack.RunTrigger.TriggerBuildingBlockRun(ctx, buildingBlockUuid, client.MeshBuildingBlockV2TriggerRunRequest{DryRun: dryRun}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := internal.WriteItem(cmd.OutOrStdout(), output.Format, buildingBlock); err != nil { | ||
| return err | ||
| } | ||
| slog.InfoContext(ctx, fmt.Sprintf("meshStack accepted a run of building block %s. Follow it with `meshstack buildingblockrun list --building-block %s`", | ||
| buildingBlockUuid, buildingBlockUuid)) | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().BoolVar(&dryRun, "dry-run", false, "ask for a dry (DETECT) run that changes nothing") | ||
| output.Register(cmd.Flags()) | ||
|
|
||
| return cmd | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package buildingblock_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "io" | ||
| gohttp "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/meshcloud/meshstack-cli/cmd/buildingblock" | ||
| "github.com/meshcloud/meshstack-cli/pkg/setting" | ||
| ) | ||
|
|
||
| const ( | ||
| buildingBlockUuid = "b1d2c3e4-0000-4000-8000-000000000001" | ||
| triggerRunPath = "/api/meshobjects/meshbuildingblocks/" + buildingBlockUuid + "/trigger-run" | ||
| // unexpiringJwt is an unsigned JWT that expires in 2100, so the CLI sends it rather than asking for a new one. | ||
| unexpiringJwt = "eyJhbGciOiJub25lIn0.eyJleHAiOjQxMDI0NDQ4MDB9." | ||
| ) | ||
|
|
||
| func TestTriggerRunSendsDryRunAndWritesTheAnsweredBuildingBlock(t *testing.T) { | ||
| for _, tt := range []struct { | ||
| args []string | ||
| wantBody string | ||
| }{ | ||
| {args: nil, wantBody: `"dryRun":false`}, | ||
| {args: []string{"--dry-run"}, wantBody: `"dryRun":true`}, | ||
| } { | ||
| t.Run(tt.wantBody, func(t *testing.T) { | ||
| var requests []string | ||
| meshStack := httptest.NewServer(gohttp.HandlerFunc(func(w gohttp.ResponseWriter, r *gohttp.Request) { | ||
| body, err := io.ReadAll(r.Body) | ||
| assert.NoError(t, err) | ||
| requests = append(requests, r.Method+" "+r.URL.Path+" "+string(body)) | ||
| if r.Method != gohttp.MethodPost || r.URL.Path != triggerRunPath { | ||
| gohttp.NotFound(w, r) | ||
| return | ||
| } | ||
| w.WriteHeader(gohttp.StatusAccepted) | ||
| _, _ = io.WriteString(w, `{"metadata":{"uuid":"`+buildingBlockUuid+`"},"status":{"status":"PENDING","latestRunUuid":"r1"}}`) | ||
| })) | ||
| t.Cleanup(meshStack.Close) | ||
| useFakeMeshStack(t, meshStack.URL) | ||
|
|
||
| var stdout bytes.Buffer | ||
| cmd := buildingblock.New() | ||
| cmd.SetArgs(append([]string{"trigger-run", buildingBlockUuid, "-o", "ndjson"}, tt.args...)) | ||
| cmd.SetOut(&stdout) | ||
| require.NoError(t, cmd.ExecuteContext(t.Context())) | ||
|
|
||
| require.Len(t, requests, 1, "the command triggers the run and does nothing else, such as waiting for it") | ||
| assert.Contains(t, requests[0], "POST "+triggerRunPath) | ||
| assert.Contains(t, requests[0], tt.wantBody) | ||
| assert.Contains(t, stdout.String(), `"latestRunUuid":"r1"`) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func useFakeMeshStack(t *testing.T, endpoint string) { | ||
| t.Helper() | ||
| t.Setenv("MESHSTACK_CONFIG_DIR", t.TempDir()) | ||
| t.Setenv(setting.Endpoint.EnvKey(), endpoint) | ||
| t.Setenv(setting.SkipVersionCheck.EnvKey(), "true") | ||
| t.Setenv(setting.Profile.EnvKey(), "") | ||
| t.Setenv(setting.Workspace.EnvKey(), "") | ||
| t.Setenv(setting.ApiKeyClientId.EnvKey(), "") | ||
| t.Setenv(setting.ApiKeyClientSecret.EnvKey(), "") | ||
| t.Setenv(setting.ApiToken.EnvKey(), unexpiringJwt) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove useless comment, these are already covered by API docs authoritatively and should not be restated in the clinet code