-
Notifications
You must be signed in to change notification settings - Fork 894
feat: add cli support for workspace automatic updates #10438
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 1 commit
Commits
Show all changes
4 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
Next
Next commit
feat: add cli support for workspace automatic updates
- Loading branch information
commit cd7e9883e91cbff5dd8d2d301af97bbb1f1bdd1c
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/v2/cli/clibase" | ||
"github.com/coder/coder/v2/cli/cliui" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
|
||
func (r *RootCmd) autoupdate() *clibase.Cmd { | ||
client := new(codersdk.Client) | ||
cmd := &clibase.Cmd{ | ||
Annotations: workspaceCommand, | ||
Use: "autoupdate <workspace> <always|never>", | ||
Short: "Toggle auto-update policy for a workspace", | ||
Middleware: clibase.Chain( | ||
clibase.RequireNArgs(2), | ||
r.InitClient(client), | ||
), | ||
Handler: func(inv *clibase.Invocation) error { | ||
policy := strings.ToLower(inv.Args[1]) | ||
err := validateAutoUpdatePolicy(policy) | ||
if err != nil { | ||
return xerrors.Errorf("validate policy: %w", err) | ||
} | ||
|
||
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0]) | ||
if err != nil { | ||
return xerrors.Errorf("get workspace: %w", err) | ||
} | ||
|
||
err = client.UpdateWorkspaceAutomaticUpdates(inv.Context(), workspace.ID, codersdk.UpdateWorkspaceAutomaticUpdatesRequest{ | ||
AutomaticUpdates: codersdk.AutomaticUpdates(policy), | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("update workspace automatic updates policy: %w", err) | ||
} | ||
_, _ = fmt.Fprintf(inv.Stdout, "Updated workspace %q auto-update policy to %q\n", workspace.Name, policy) | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.Options = append(cmd.Options, cliui.SkipPromptOption()) | ||
return cmd | ||
} | ||
|
||
func validateAutoUpdatePolicy(arg string) error { | ||
switch codersdk.AutomaticUpdates(arg) { | ||
case codersdk.AutomaticUpdatesAlways, codersdk.AutomaticUpdatesNever: | ||
return nil | ||
default: | ||
return xerrors.Errorf("invalid option %q must be either of %q or %q", arg, codersdk.AutomaticUpdatesAlways, codersdk.AutomaticUpdatesNever) | ||
} | ||
} |
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,79 @@ | ||
package cli_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/v2/cli/clitest" | ||
"github.com/coder/coder/v2/coderd/coderdtest" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
|
||
func TestAutoUpdate(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("OK", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true}) | ||
owner := coderdtest.CreateFirstUser(t, client) | ||
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID) | ||
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil) | ||
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID) | ||
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID) | ||
workspace := coderdtest.CreateWorkspace(t, member, owner.OrganizationID, template.ID) | ||
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID) | ||
require.Equal(t, codersdk.AutomaticUpdatesNever, workspace.AutomaticUpdates) | ||
|
||
expectedPolicy := codersdk.AutomaticUpdatesAlways | ||
inv, root := clitest.New(t, "autoupdate", workspace.Name, string(expectedPolicy)) | ||
clitest.SetupConfig(t, member, root) | ||
var buf bytes.Buffer | ||
inv.Stdout = &buf | ||
err := inv.Run() | ||
require.NoError(t, err) | ||
require.Contains(t, buf.String(), fmt.Sprintf("Updated workspace %q auto-update policy to %q", workspace.Name, expectedPolicy)) | ||
|
||
workspace = coderdtest.MustWorkspace(t, client, workspace.ID) | ||
require.Equal(t, expectedPolicy, workspace.AutomaticUpdates) | ||
}) | ||
|
||
t.Run("InvalidArgs", func(t *testing.T) { | ||
type testcase struct { | ||
Name string | ||
Args []string | ||
ErrorContains string | ||
} | ||
|
||
cases := []testcase{ | ||
{ | ||
Name: "NoPolicy", | ||
Args: []string{"autoupdate", "ws"}, | ||
ErrorContains: "wanted 2 args but got 1", | ||
}, | ||
{ | ||
Name: "InvalidPolicy", | ||
Args: []string{"autoupdate", "ws", "sometimes"}, | ||
ErrorContains: fmt.Sprintf("invalid option %q must be either of", "sometimes"), | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
c := c | ||
t.Run(c.Name, func(t *testing.T) { | ||
t.Parallel() | ||
client := coderdtest.New(t, nil) | ||
_ = coderdtest.CreateFirstUser(t, client) | ||
|
||
inv, root := clitest.New(t, c.Args...) | ||
clitest.SetupConfig(t, client, root) | ||
err := inv.Run() | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), c.ErrorContains) | ||
}) | ||
} | ||
}) | ||
} |
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
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
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.