Skip to content

feat: implement deprecated flag for templates to prevent new workspaces #10745

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 16 commits into from
Nov 20, 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 deprecated to template table
  • Loading branch information
Emyrk committed Nov 17, 2023
commit f5dc43f3768a81b47f6e1df57ab0d1562ddd0b7a
8 changes: 4 additions & 4 deletions cli/templateedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
allowUserAutostart bool
allowUserAutostop bool
requireActiveVersion bool
deprecatedMessage string
deprecationMessage string
)
client := new(codersdk.Client)

Expand Down Expand Up @@ -126,7 +126,7 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
var deprecated *string
opt := inv.Command.Options.ByName(deprecatedFlagName)
if !(opt.ValueSource == "" || opt.ValueSource == clibase.ValueSourceDefault) {
deprecated = &deprecatedMessage
deprecated = &deprecationMessage
}

// NOTE: coderd will ignore empty fields.
Expand All @@ -150,7 +150,7 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
AllowUserAutostart: allowUserAutostart,
AllowUserAutostop: allowUserAutostop,
RequireActiveVersion: requireActiveVersion,
DeprecatedMessage: deprecated,
DeprecationMessage: deprecated,
}

_, err = client.UpdateTemplateMeta(inv.Context(), template.ID, req)
Expand Down Expand Up @@ -182,7 +182,7 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
Name: deprecatedFlagName,
Flag: "deprecated",
Description: "Sets the template as deprecated. Must be a message explaining why the template is deprecated.",
Value: clibase.StringOf(&deprecatedMessage),
Value: clibase.StringOf(&deprecationMessage),
},
{
Flag: "icon",
Expand Down
6 changes: 6 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,9 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
validErrs = append(validErrs, codersdk.ValidationError{Field: "autostop_requirement.weeks", Detail: fmt.Sprintf("Must be less than %d.", schedule.MaxTemplateAutostopRequirementWeeks)})
}
// Defaults to the existing.
deprecatedMessage := template.Deprecated
if req.DeprecatedMessage != nil {
deprecatedMessage = *req.DeprecatedMessage
deprecationMessage := template.Deprecated
if req.DeprecationMessage != nil {
deprecationMessage = *req.DeprecationMessage
}

// The minimum valid value for a dormant TTL is 1 minute. This is
Expand Down Expand Up @@ -649,7 +649,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
req.TimeTilDormantMillis == time.Duration(template.TimeTilDormant).Milliseconds() &&
req.TimeTilDormantAutoDeleteMillis == time.Duration(template.TimeTilDormantAutoDelete).Milliseconds() &&
req.RequireActiveVersion == template.RequireActiveVersion &&
(deprecatedMessage == template.Deprecated) {
(deprecationMessage == template.Deprecated) {
return nil
}

Expand All @@ -673,10 +673,10 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
return xerrors.Errorf("update template metadata: %w", err)
}

if template.RequireActiveVersion != req.RequireActiveVersion || deprecatedMessage != template.Deprecated {
if template.RequireActiveVersion != req.RequireActiveVersion || deprecationMessage != template.Deprecated {
err = (*api.AccessControlStore.Load()).SetTemplateAccessControl(ctx, tx, template.ID, dbauthz.TemplateAccessControl{
RequireActiveVersion: req.RequireActiveVersion,
Deprecated: deprecatedMessage,
Deprecated: deprecationMessage,
})
if err != nil {
return xerrors.Errorf("set template access control: %w", err)
Expand Down Expand Up @@ -873,6 +873,6 @@ func (api *API) convertTemplate(
// These values depend on entitlements and come from the templateAccessControl
RequireActiveVersion: templateAccessControl.RequireActiveVersion,
Deprecated: templateAccessControl.IsDeprecated(),
DeprecatedMessage: templateAccessControl.Deprecated,
DeprecationMessage: templateAccessControl.Deprecated,
}
}
4 changes: 2 additions & 2 deletions coderd/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ func TestPatchTemplateMeta(t *testing.T) {
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

req := codersdk.UpdateTemplateMeta{
DeprecatedMessage: ptr.Ref("APGL cannot deprecate"),
DeprecationMessage: ptr.Ref("APGL cannot deprecate"),
}

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
Expand All @@ -536,7 +536,7 @@ func TestPatchTemplateMeta(t *testing.T) {
assert.Greater(t, updated.UpdatedAt, template.UpdatedAt)
// AGPL cannot deprecate, expect no change
assert.False(t, updated.Deprecated)
assert.Empty(t, updated.DeprecatedMessage)
assert.Empty(t, updated.DeprecationMessage)
})

t.Run("NoDefaultTTL", func(t *testing.T) {
Expand Down
18 changes: 9 additions & 9 deletions codersdk/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ type Template struct {
Provisioner ProvisionerType `json:"provisioner" enums:"terraform"`
ActiveVersionID uuid.UUID `json:"active_version_id" format:"uuid"`
// ActiveUserCount is set to -1 when loading.
ActiveUserCount int `json:"active_user_count"`
BuildTimeStats TemplateBuildTimeStats `json:"build_time_stats"`
Description string `json:"description"`
Deprecated bool `json:"deprecated"`
DeprecatedMessage string `json:"deprecated_message"`
Icon string `json:"icon"`
DefaultTTLMillis int64 `json:"default_ttl_ms"`
ActiveUserCount int `json:"active_user_count"`
BuildTimeStats TemplateBuildTimeStats `json:"build_time_stats"`
Description string `json:"description"`
Deprecated bool `json:"deprecated"`
DeprecationMessage string `json:"deprecation_message"`
Icon string `json:"icon"`
DefaultTTLMillis int64 `json:"default_ttl_ms"`
// TODO(@dean): remove max_ttl once autostop_requirement is matured
MaxTTLMillis int64 `json:"max_ttl_ms"`
// AutostopRequirement and AutostartRequirement are enterprise features. Its
Expand Down Expand Up @@ -231,11 +231,11 @@ type UpdateTemplateMeta struct {
// use the active version of the template. This option has no
// effect on template admins.
RequireActiveVersion bool `json:"require_active_version"`
// DeprecatedMessage if set, will mark the template as deprecated and block
// DeprecationMessage if set, will mark the template as deprecated and block
// any new workspaces from using this template.
// If passed an empty string, will remove the deprecated message, making
// the template usable for new workspaces again.
DeprecatedMessage *string `json:"deprecated_message"`
DeprecationMessage *string `json:"deprecation_message"`
}

type TemplateExample struct {
Expand Down
Loading