Skip to content

feat: add cli support for --require-active-version #10337

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 10 commits into from
Oct 19, 2023
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
Prev Previous commit
Next Next commit
add support for template edit
  • Loading branch information
sreya committed Oct 19, 2023
commit 86442011c8e6beaee6c43904abebf5051e850131
9 changes: 5 additions & 4 deletions cli/templatecreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func (r *RootCmd) templateCreate() *clibase.Cmd {
}

if requireActiveVersion {
if !entitlements.Features[codersdk.FeatureAccessControl].Enabled {
return xerrors.Errorf("your license is not entitled to use template access control, so you cannot set --require-active-version")
}

experiments, exErr := client.Experiments(inv.Context())
if exErr != nil {
return xerrors.Errorf("get experiments: %w", exErr)
Expand All @@ -72,10 +76,6 @@ func (r *RootCmd) templateCreate() *clibase.Cmd {
if !experiments.Enabled(codersdk.ExperimentTemplateUpdatePolicies) {
return xerrors.Errorf("--require-active-version is an experimental feature, pass 'template_update_policies' to the CODER_EXPERIMENTS env var to use this option")
}

if !entitlements.Features[codersdk.FeatureAccessControl].Enabled {
return xerrors.Errorf("your license is not entitled to use template access control, so you cannot set --require-active-version")
}
}
}

Expand Down Expand Up @@ -229,6 +229,7 @@ func (r *RootCmd) templateCreate() *clibase.Cmd {
Flag: "require-active-version",
Description: "Requires workspace builds to use the active template version. This setting does not apply to template admins. This is an enterprise-only feature.",
Value: clibase.BoolOf(&requireActiveVersion),
Default: "false",
},

cliui.SkipPromptOption(),
Expand Down
37 changes: 32 additions & 5 deletions cli/templateedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
allowUserCancelWorkspaceJobs bool
allowUserAutostart bool
allowUserAutostop bool
requireActiveVersion bool
)
client := new(codersdk.Client)

Expand All @@ -43,7 +44,7 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
Short: "Edit the metadata of a template by name.",
Handler: func(inv *clibase.Invocation) error {
unsetAutostopRequirementDaysOfWeek := len(autostopRequirementDaysOfWeek) == 1 && autostopRequirementDaysOfWeek[0] == "none"
requiresEntitlement := (len(autostopRequirementDaysOfWeek) > 0 && !unsetAutostopRequirementDaysOfWeek) ||
requiresScheduling := (len(autostopRequirementDaysOfWeek) > 0 && !unsetAutostopRequirementDaysOfWeek) ||
autostopRequirementWeeks > 0 ||
!allowUserAutostart ||
!allowUserAutostop ||
Expand All @@ -52,18 +53,37 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
inactivityTTL != 0 ||
len(autostartRequirementDaysOfWeek) > 0

requiresEntitlement := requiresScheduling || requireActiveVersion
if requiresEntitlement {
entitlements, err := client.Entitlements(inv.Context())
var sdkErr *codersdk.Error
if xerrors.As(err, &sdkErr) && sdkErr.StatusCode() == http.StatusNotFound {
return xerrors.Errorf("your deployment appears to be an AGPL deployment, so you cannot set --max-ttl, --failure-ttl, --inactivityTTL, --allow-user-autostart=false or --allow-user-autostop=false")
if cerr, ok := codersdk.AsError(err); ok && cerr.StatusCode() == http.StatusNotFound {
return xerrors.Errorf("your deployment appears to be an AGPL deployment, so you cannot set enterprise-only flags")
} else if err != nil {
return xerrors.Errorf("get entitlements: %w", err)
}

if !entitlements.Features[codersdk.FeatureAdvancedTemplateScheduling].Enabled {
if requiresScheduling && !entitlements.Features[codersdk.FeatureAdvancedTemplateScheduling].Enabled {
return xerrors.Errorf("your license is not entitled to use advanced template scheduling, so you cannot set --max-ttl, --failure-ttl, --inactivityTTL, --allow-user-autostart=false or --allow-user-autostop=false")
}

if requireActiveVersion {
if !entitlements.Features[codersdk.FeatureAccessControl].Enabled {
return xerrors.Errorf("your license is not entitled to use template access control, so you cannot set --require-active-version")
}

experiments, exErr := client.Experiments(inv.Context())
if exErr != nil {
return xerrors.Errorf("get experiments: %w", exErr)
}

if !experiments.Enabled(codersdk.ExperimentTemplateUpdatePolicies) {
return xerrors.Errorf("--require-active-version is an experimental feature, pass 'template_update_policies' to the CODER_EXPERIMENTS env var to use this option")
}
if !entitlements.Features[codersdk.FeatureAccessControl].Enabled {
return xerrors.Errorf("your license is not entitled to use template access control, so you cannot set --require-active-version")

}
}
}

organization, err := CurrentOrganization(inv, client)
Expand Down Expand Up @@ -110,6 +130,7 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
AllowUserCancelWorkspaceJobs: allowUserCancelWorkspaceJobs,
AllowUserAutostart: allowUserAutostart,
AllowUserAutostop: allowUserAutostop,
RequireActiveVersion: requireActiveVersion,
}

_, err = client.UpdateTemplateMeta(inv.Context(), template.ID, req)
Expand Down Expand Up @@ -222,6 +243,12 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
Default: "true",
Value: clibase.BoolOf(&allowUserAutostop),
},
{
Flag: "require-active-version",
Description: "Requires workspace builds to use the active template version. This setting does not apply to template admins. This is an enterprise-only feature.",
Value: clibase.BoolOf(&requireActiveVersion),
Default: "false",
},
cliui.SkipPromptOption(),
}

Expand Down
26 changes: 26 additions & 0 deletions cli/templateedit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,4 +1021,30 @@ func TestTemplateEdit(t *testing.T) {
assert.Equal(t, template.TimeTilDormantMillis, updated.TimeTilDormantMillis)
})
})

t.Run("RequireActiveVersion", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
owner := coderdtest.CreateFirstUser(t, client)

version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {})

// Test the cli command with --allow-user-autostart.
cmdArgs := []string{
"templates",
"edit",
template.Name,
"--require-active-version",
}
inv, root := clitest.New(t, cmdArgs...)
//nolint
clitest.SetupConfig(t, client, root)

ctx := testutil.Context(t, testutil.WaitLong)
err := inv.WithContext(ctx).Run()
require.Error(t, err)
require.ErrorContains(t, err, "appears to be an AGPL deployment")
})
}
2 changes: 1 addition & 1 deletion enterprise/cli/templatecreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestTemplateCreate(t *testing.T) {
require.True(t, template.RequireActiveVersion)
})

t.Run("NoEntitlement", func(t *testing.T) {
t.Run("NotEntitled", func(t *testing.T) {
t.Parallel()

dv := coderdtest.DeploymentValues(t)
Expand Down
97 changes: 97 additions & 0 deletions enterprise/cli/templateedit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cli_test

import (
"testing"

"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
"github.com/coder/coder/v2/enterprise/coderd/license"
"github.com/coder/coder/v2/testutil"
"github.com/stretchr/testify/require"
)

func TestTemplateEdit(t *testing.T) {
t.Parallel()

t.Run("OK", func(t *testing.T) {
t.Parallel()

dv := coderdtest.DeploymentValues(t)
dv.Experiments = []string{
string(codersdk.ExperimentTemplateUpdatePolicies),
}

ownerClient, owner := coderdenttest.New(t, &coderdenttest.Options{
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureAccessControl: 1,
},
},
Options: &coderdtest.Options{
DeploymentValues: dv,
IncludeProvisionerDaemon: true,
},
})

templateAdmin, _ := coderdtest.CreateAnotherUser(t, ownerClient, owner.OrganizationID, rbac.RoleTemplateAdmin())
version := coderdtest.CreateTemplateVersion(t, templateAdmin, owner.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, templateAdmin, version.ID)
template := coderdtest.CreateTemplate(t, templateAdmin, owner.OrganizationID, version.ID)
require.False(t, template.RequireActiveVersion)

inv, conf := newCLI(t, "templates",
"edit", template.Name,
"--require-active-version",
"-y",
)

clitest.SetupConfig(t, templateAdmin, conf)

err := inv.Run()
require.NoError(t, err)

ctx := testutil.Context(t, testutil.WaitMedium)
template, err = templateAdmin.Template(ctx, template.ID)
require.NoError(t, err)
require.True(t, template.RequireActiveVersion)
})

t.Run("NotEntitled", func(t *testing.T) {
t.Parallel()

dv := coderdtest.DeploymentValues(t)
dv.Experiments = []string{
string(codersdk.ExperimentTemplateUpdatePolicies),
}

client, owner := coderdenttest.New(t, &coderdenttest.Options{
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{},
},
Options: &coderdtest.Options{
DeploymentValues: dv,
IncludeProvisionerDaemon: true,
},
})

version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
require.False(t, template.RequireActiveVersion)

inv, conf := newCLI(t, "templates",
"edit", template.Name,
"--require-active-version",
"-y",
)

clitest.SetupConfig(t, client, conf)

err := inv.Run()
require.Error(t, err)
require.Contains(t, err.Error(), "your license is not entitled to use template access control, so you cannot set --require-active-version")
})
}