fix: allow setting workspace deadline as early as now plus 30 minutes (#2328)

This PR makes the following changes:

- coderd: /api/v2/workspaces/:workspace/extend now accepts any time at least 30 minutes in the future.
- coder bump command also allows the above. Some small copy changes to command.
- coder bump now actually enforces template-level maxima.
This commit is contained in:
Cian Johnston
2022-06-14 22:39:15 +01:00
committed by GitHub
parent 4734636b17
commit 02ad60fd75
4 changed files with 88 additions and 40 deletions

View File

@ -8,19 +8,26 @@ import (
"github.com/spf13/cobra"
"golang.org/x/xerrors"
"github.com/coder/coder/coderd/util/tz"
"github.com/coder/coder/codersdk"
)
const (
bumpDescriptionLong = `To extend the autostop deadline for a workspace.`
bumpDescriptionShort = `Shut your workspace down after a given duration has passed.`
bumpDescriptionLong = `Modify the time at which your workspace will shut down automatically.
* Provide a duration from now (for example, 1h30m).
* The minimum duration is 30 minutes.
* If the workspace template restricts the maximum runtime of a workspace, this will be enforced here.
* If the workspace does not already have a shutdown scheduled, this does nothing.
`
)
func bump() *cobra.Command {
bumpCmd := &cobra.Command{
Args: cobra.RangeArgs(1, 2),
Annotations: workspaceCommand,
Use: "bump <workspace-name> <duration>",
Short: "Extend the autostop deadline for a workspace.",
Use: "bump <workspace-name> <duration from now>",
Short: bumpDescriptionShort,
Long: bumpDescriptionLong,
Example: "coder bump my-workspace 90m",
RunE: func(cmd *cobra.Command, args []string) error {
@ -39,17 +46,20 @@ func bump() *cobra.Command {
return xerrors.Errorf("get workspace: %w", err)
}
newDeadline := time.Now().Add(bumpDuration)
loc, err := tz.TimezoneIANA()
if err != nil {
loc = time.UTC // best effort
}
if newDeadline.Before(workspace.LatestBuild.Deadline) {
if bumpDuration < 29*time.Minute {
_, _ = fmt.Fprintf(
cmd.OutOrStdout(),
"The proposed deadline is %s before the current deadline.\n",
workspace.LatestBuild.Deadline.Sub(newDeadline).Round(time.Minute),
"Please specify a duration of at least 30 minutes.\n",
)
return nil
}
newDeadline := time.Now().In(loc).Add(bumpDuration)
if err := client.PutExtendWorkspace(cmd.Context(), workspace.ID, codersdk.PutExtendWorkspaceRequest{
Deadline: newDeadline,
}); err != nil {
@ -62,7 +72,6 @@ func bump() *cobra.Command {
newDeadline.Format(timeFormat),
newDeadline.Format(dateFormat),
)
return nil
},
}

View File

@ -124,8 +124,8 @@ func TestBump(t *testing.T) {
workspace, err = client.Workspace(ctx, workspace.ID)
require.NoError(t, err)
// TODO(cian): need to stop and start the workspace as we do not update the deadline yet
// see: https://github.com/coder/coder/issues/1783
// NOTE(cian): need to stop and start the workspace as we do not update the deadline
// see: https://github.com/coder/coder/issues/2224
coderdtest.MustTransitionWorkspace(t, client, workspace.ID, database.WorkspaceTransitionStart, database.WorkspaceTransitionStop)
coderdtest.MustTransitionWorkspace(t, client, workspace.ID, database.WorkspaceTransitionStop, database.WorkspaceTransitionStart)