-
Notifications
You must be signed in to change notification settings - Fork 7
feat(controls): add 'kosli update control' command #988
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
|
|
||
| "github.com/kosli-dev/cli/internal/requests" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const updateControlShortDesc = `Update a Kosli control.` | ||
|
|
||
| const updateControlLongDesc = updateControlShortDesc + ` | ||
|
|
||
| Only the flags you provide are changed; omitted fields are left untouched. | ||
| Providing ^--link^ replaces all of the control's existing links.` | ||
|
|
||
| const updateControlExample = ` | ||
| # update a control's name: | ||
| kosli update control yourControlIdentifier \ | ||
| --name "New control name" \ | ||
| --api-token yourAPIToken \ | ||
| --org yourOrgName | ||
|
|
||
| # update a control's description and links: | ||
| kosli update control yourControlIdentifier \ | ||
| --description "what this control checks" \ | ||
| --link runbook=https://example.com/runbook \ | ||
| --api-token yourAPIToken \ | ||
| --org yourOrgName | ||
| ` | ||
|
|
||
| type updateControlOptions struct { | ||
| name string | ||
| description string | ||
| links map[string]string | ||
| } | ||
|
|
||
| func newUpdateControlCmd(out io.Writer) *cobra.Command { | ||
| o := new(updateControlOptions) | ||
| cmd := &cobra.Command{ | ||
| Use: "control CONTROL-IDENTIFIER", | ||
| Short: updateControlShortDesc, | ||
| Long: updateControlLongDesc, | ||
| Example: updateControlExample, | ||
| Args: cobra.ExactArgs(1), | ||
| Annotations: map[string]string{betaCLIAnnotation: ""}, | ||
| PreRunE: func(cmd *cobra.Command, args []string) error { | ||
| if err := RequireGlobalFlags(global, []string{"Org", "ApiToken"}); err != nil { | ||
| return ErrorBeforePrintingUsage(cmd, err.Error()) | ||
| } | ||
| return RequireAtLeastOneOfFlags(cmd, []string{"name", "description", "link"}) | ||
| }, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return o.run(cmd, args) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVarP(&o.name, "name", "n", "", updateControlNameFlag) | ||
| cmd.Flags().StringVarP(&o.description, "description", "d", "", controlDescriptionFlag) | ||
| cmd.Flags().StringToStringVar(&o.links, "link", map[string]string{}, controlLinkFlag) | ||
|
|
||
| addDryRunFlag(cmd) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func (o *updateControlOptions) run(cmd *cobra.Command, args []string) error { | ||
| url, err := url.JoinPath(global.Host, "api/v2/controls", global.Org, args[0]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Only send the fields the user explicitly set, so unset flags leave the | ||
| // corresponding values unchanged (the server treats an omitted field as | ||
| // "no change"). | ||
| payload := map[string]interface{}{} | ||
| if cmd.Flags().Changed("name") { | ||
| payload["name"] = o.name | ||
| } | ||
| if cmd.Flags().Changed("description") { | ||
| payload["description"] = o.description | ||
| } | ||
| if cmd.Flags().Changed("link") { | ||
| payload["links"] = o.links | ||
| } | ||
|
|
||
| reqParams := &requests.RequestParams{ | ||
| Method: http.MethodPut, | ||
| URL: url, | ||
| Payload: payload, | ||
| DryRun: global.DryRun, | ||
| Token: global.ApiToken, | ||
| } | ||
| _, err = kosliClient.Do(reqParams) | ||
| if err == nil && !global.DryRun { | ||
| logger.Info("control %s was updated", args[0]) | ||
| } | ||
| return err | ||
| } |
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,80 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
||
| type UpdateControlCommandTestSuite struct { | ||
| suite.Suite | ||
| defaultKosliArguments string | ||
| } | ||
|
|
||
| func (suite *UpdateControlCommandTestSuite) SetupTest() { | ||
| global = &GlobalOpts{ | ||
| ApiToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImNkNzg4OTg5In0.e8i_lA_QrEhFncb05Xw6E_tkCHU9QfcY4OLTVUCHffY", | ||
| Org: "docs-cmd-test-user", | ||
| Host: "http://localhost:8001", | ||
| } | ||
| suite.defaultKosliArguments = fmt.Sprintf(" --host %s --org %s --api-token %s", global.Host, global.Org, global.ApiToken) | ||
| CreateControl(global.Org, "update-me", "Update me", suite.T()) | ||
| } | ||
|
|
||
| func (suite *UpdateControlCommandTestSuite) TestUpdateControlCmd() { | ||
| tests := []cmdTestCase{ | ||
| { | ||
| wantError: true, | ||
| name: "fails when no identifier argument is provided", | ||
| cmd: "update control --name 'New name'" + suite.defaultKosliArguments, | ||
| golden: "Error: accepts 1 arg(s), received 0\n", | ||
| }, | ||
| { | ||
| wantError: true, | ||
| name: "fails when no updatable flags are provided", | ||
| cmd: "update control update-me" + suite.defaultKosliArguments, | ||
| golden: "Error: at least one of --name, --description, --link is required\n", | ||
| }, | ||
| { | ||
| name: "updating a control's name works", | ||
| cmd: "update control update-me --name 'Updated name'" + suite.defaultKosliArguments, | ||
| golden: "control update-me was updated\n", | ||
| }, | ||
| { | ||
| name: "updating a control's description works", | ||
| cmd: "update control update-me --description 'checks something new'" + suite.defaultKosliArguments, | ||
| golden: "control update-me was updated\n", | ||
| }, | ||
| { | ||
| name: "updating a control's links works", | ||
| cmd: "update control update-me --link runbook=https://example.com/runbook" + suite.defaultKosliArguments, | ||
| golden: "control update-me was updated\n", | ||
| }, | ||
| { | ||
| name: "updating multiple fields in one call works", | ||
| cmd: "update control update-me --name 'Another name' --description 'and a description'" + suite.defaultKosliArguments, | ||
| golden: "control update-me was updated\n", | ||
| }, | ||
| { | ||
| // Guards the core behaviour: only the flags the user set land in the | ||
| // PUT body. A name-only update must send exactly {"name": ...} — no | ||
| // stray empty description or links. | ||
| name: "sends only the fields that were set (dry-run)", | ||
| cmd: "update control update-me --name 'Only name' --dry-run" + suite.defaultKosliArguments, | ||
| goldenRegex: `(?s)controls/docs-cmd-test-user/update-me.*\{\s*"name": "Only name"\s*\}`, | ||
| }, | ||
| { | ||
| wantError: true, | ||
| name: "updating a non-existing control gives a clear error", | ||
| cmd: "update control no-such-control --name 'New name'" + suite.defaultKosliArguments, | ||
| goldenRegex: "^Error: Control 'no-such-control' does not exist in org", | ||
| }, | ||
| } | ||
|
|
||
| runTestCmd(suite.T(), tests) | ||
| } | ||
|
|
||
| func TestUpdateControlCommandTestSuite(t *testing.T) { | ||
| suite.Run(t, new(UpdateControlCommandTestSuite)) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.