Skip to content

Commit edf30ca

Browse files
committed
pr comments
1 parent a89bb9e commit edf30ca

File tree

3 files changed

+164
-9
lines changed

3 files changed

+164
-9
lines changed

cli/cliui/deprecation.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cliui
2+
3+
import (
4+
"github.com/coder/coder/v2/cli/clibase"
5+
"github.com/coder/pretty"
6+
)
7+
8+
func DeprecationWarning(message string) clibase.MiddlewareFunc {
9+
return func(next clibase.HandlerFunc) clibase.HandlerFunc {
10+
return func(i *clibase.Invocation) error {
11+
pretty.Sprint(
12+
DefaultStyles.Warn,
13+
"DEPRECATION WARNING: This command will be removed in a future release. \n"+message+"\n")
14+
return next(i)
15+
}
16+
}
17+
}

cli/templatecreate.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,13 @@ func (r *RootCmd) templateCreate() *clibase.Cmd {
3939
Short: "Create a template from the current directory or as specified by flag",
4040
Middleware: clibase.Chain(
4141
clibase.RequireRangeArgs(0, 1),
42+
cliui.DeprecationWarning(
43+
"Use `coder templates push` command for creating and updating templates. "+
44+
"Use `coder templates edit` command for editing template settings. ",
45+
),
4246
r.InitClient(client),
4347
),
4448
Handler: func(inv *clibase.Invocation) error {
45-
_, _ = fmt.Fprintln(inv.Stdout, "\n"+pretty.Sprint(cliui.DefaultStyles.Wrap,
46-
pretty.Sprint(
47-
cliui.DefaultStyles.Warn,
48-
"DEPRECATION WARNING: Use `coder templates push` command for creating and updating templates. "+
49-
"Use `coder templates edit` command for editing template settings. "+
50-
"This command will be removed in a future release. \n"+
51-
"Waiting 1 second...\n")))
52-
time.Sleep(1 * time.Second)
53-
5449
isTemplateSchedulingOptionsSet := failureTTL != 0 || dormancyThreshold != 0 || dormancyAutoDeletion != 0 || maxTTL != 0
5550

5651
if isTemplateSchedulingOptionsSet || requireActiveVersion {

enterprise/cli/templatecreate_test.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package cli_test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/coder/coder/v2/cli/clitest"
10+
"github.com/coder/coder/v2/coderd/coderdtest"
11+
"github.com/coder/coder/v2/coderd/database"
12+
"github.com/coder/coder/v2/coderd/rbac"
13+
"github.com/coder/coder/v2/codersdk"
14+
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
15+
"github.com/coder/coder/v2/enterprise/coderd/license"
16+
"github.com/coder/coder/v2/provisioner/echo"
17+
"github.com/coder/coder/v2/testutil"
18+
)
19+
20+
func TestTemplateCreate(t *testing.T) {
21+
t.Parallel()
22+
23+
t.Run("RequireActiveVersion", func(t *testing.T) {
24+
t.Parallel()
25+
26+
client, user := coderdenttest.New(t, &coderdenttest.Options{
27+
LicenseOptions: &coderdenttest.LicenseOptions{
28+
Features: license.Features{
29+
codersdk.FeatureAccessControl: 1,
30+
},
31+
},
32+
Options: &coderdtest.Options{
33+
IncludeProvisionerDaemon: true,
34+
},
35+
})
36+
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, user.OrganizationID, rbac.RoleTemplateAdmin())
37+
38+
source := clitest.CreateTemplateVersionSource(t, &echo.Responses{
39+
Parse: echo.ParseComplete,
40+
ProvisionApply: echo.ApplyComplete,
41+
})
42+
43+
inv, conf := newCLI(t, "templates",
44+
"create", "new",
45+
"--directory", source,
46+
"--test.provisioner", string(database.ProvisionerTypeEcho),
47+
"--require-active-version",
48+
"-y",
49+
)
50+
51+
clitest.SetupConfig(t, templateAdmin, conf)
52+
53+
err := inv.Run()
54+
require.NoError(t, err)
55+
56+
ctx := testutil.Context(t, testutil.WaitMedium)
57+
template, err := templateAdmin.TemplateByName(ctx, user.OrganizationID, "new")
58+
require.NoError(t, err)
59+
require.True(t, template.RequireActiveVersion)
60+
})
61+
62+
t.Run("WorkspaceCleanup", func(t *testing.T) {
63+
t.Parallel()
64+
65+
dv := coderdtest.DeploymentValues(t)
66+
dv.Experiments = []string{
67+
string(codersdk.ExperimentWorkspaceActions),
68+
}
69+
70+
client, user := coderdenttest.New(t, &coderdenttest.Options{
71+
LicenseOptions: &coderdenttest.LicenseOptions{
72+
Features: license.Features{
73+
codersdk.FeatureAdvancedTemplateScheduling: 1,
74+
},
75+
},
76+
Options: &coderdtest.Options{
77+
DeploymentValues: dv,
78+
IncludeProvisionerDaemon: true,
79+
},
80+
})
81+
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, user.OrganizationID, rbac.RoleTemplateAdmin())
82+
83+
source := clitest.CreateTemplateVersionSource(t, &echo.Responses{
84+
Parse: echo.ParseComplete,
85+
ProvisionApply: echo.ApplyComplete,
86+
})
87+
88+
const (
89+
expectedFailureTTL = time.Hour * 3
90+
expectedDormancyThreshold = time.Hour * 4
91+
expectedDormancyAutoDeletion = time.Minute * 10
92+
)
93+
94+
inv, conf := newCLI(t, "templates",
95+
"create", "new",
96+
"--directory", source,
97+
"--test.provisioner", string(database.ProvisionerTypeEcho),
98+
"--failure-ttl="+expectedFailureTTL.String(),
99+
"--dormancy-threshold="+expectedDormancyThreshold.String(),
100+
"--dormancy-auto-deletion="+expectedDormancyAutoDeletion.String(),
101+
"-y",
102+
"--",
103+
)
104+
105+
clitest.SetupConfig(t, templateAdmin, conf)
106+
107+
err := inv.Run()
108+
require.NoError(t, err)
109+
110+
ctx := testutil.Context(t, testutil.WaitMedium)
111+
template, err := templateAdmin.TemplateByName(ctx, user.OrganizationID, "new")
112+
require.NoError(t, err)
113+
require.Equal(t, expectedFailureTTL.Milliseconds(), template.FailureTTLMillis)
114+
require.Equal(t, expectedDormancyThreshold.Milliseconds(), template.TimeTilDormantMillis)
115+
require.Equal(t, expectedDormancyAutoDeletion.Milliseconds(), template.TimeTilDormantAutoDeleteMillis)
116+
})
117+
118+
t.Run("NotEntitled", func(t *testing.T) {
119+
t.Parallel()
120+
121+
client, admin := coderdenttest.New(t, &coderdenttest.Options{
122+
LicenseOptions: &coderdenttest.LicenseOptions{
123+
Features: license.Features{},
124+
},
125+
Options: &coderdtest.Options{
126+
IncludeProvisionerDaemon: true,
127+
},
128+
})
129+
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID, rbac.RoleTemplateAdmin())
130+
131+
inv, conf := newCLI(t, "templates",
132+
"create", "new",
133+
"--require-active-version",
134+
"-y",
135+
)
136+
137+
clitest.SetupConfig(t, templateAdmin, conf)
138+
139+
err := inv.Run()
140+
require.Error(t, err)
141+
require.Contains(t, err.Error(), "your license is not entitled to use enterprise access control, so you cannot set --require-active-version")
142+
})
143+
}

0 commit comments

Comments
 (0)