feat(cli): add CLI support for listing presets (#18910)

## Description 

This PR introduces a new `list presets` command to display the presets
associated with a given template.
By default, it displays the presets for the template's active version,
unless a `--template-version` flag is provided.

## Changes

* Added a new `list presets` command under `coder templates presets` to
display presets associated with a template.
* By default, the command lists presets from the template’s active
version.
* Users can override the default behavior by providing the
`--template-version` flag to target a specific version.

```
> coder templates versions presets list --help

USAGE:
  coder templates presets list [flags] <template>

  List all presets of the specified template. Defaults to the active template version.

OPTIONS:
  -O, --org string, $CODER_ORGANIZATION
          Select which organization (uuid or name) to use.

  -c, --column [name|parameters|default|desired prebuild instances] (default: name,parameters,default,desired prebuild instances)
          Columns to display in table output.

  -o, --output table|json (default: table)
          Output format.

      --template-version string
          Specify a template version to list presets for. Defaults to the active version.
```

Related PR: https://github.com/coder/coder/pull/18912 - please consider
both PRs together as they’re part of the same workflow
Relates to issue: https://github.com/coder/coder/issues/16594

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Added CLI commands to manage and list presets for specific template
versions, supporting tabular and JSON output.
* Introduced a new CLI subcommand group for template version presets,
including detailed help and documentation.
* Added support for displaying and managing the desired number of
prebuild instances for presets in CLI, API, and UI.

* **Documentation**
* Updated and expanded CLI and API documentation to describe new
commands, options, and the desired prebuild instances field in presets.
* Added new help output and reference files for template version presets
commands.

* **Bug Fixes**
* Ensured correct handling and display of the desired prebuild instances
property for presets across CLI, API, and UI.

* **Tests**
* Introduced end-to-end tests for listing template version presets,
covering scenarios with and without presets.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Susana Ferreira
2025-07-24 16:44:36 +01:00
committed by GitHub
parent 070178c454
commit 931b97caab
18 changed files with 584 additions and 22 deletions
+168
View File
@@ -0,0 +1,168 @@
package cli
import (
"fmt"
"strconv"
"strings"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/serpent"
)
func (r *RootCmd) templatePresets() *serpent.Command {
cmd := &serpent.Command{
Use: "presets",
Short: "Manage presets of the specified template",
Aliases: []string{"preset"},
Long: FormatExamples(
Example{
Description: "List presets for the active version of a template",
Command: "coder templates presets list my-template",
},
Example{
Description: "List presets for a specific version of a template",
Command: "coder templates presets list my-template --template-version my-template-version",
},
),
Handler: func(inv *serpent.Invocation) error {
return inv.Command.HelpHandler(inv)
},
Children: []*serpent.Command{
r.templatePresetsList(),
},
}
return cmd
}
func (r *RootCmd) templatePresetsList() *serpent.Command {
defaultColumns := []string{
"name",
"parameters",
"default",
"desired prebuild instances",
}
formatter := cliui.NewOutputFormatter(
cliui.TableFormat([]templatePresetRow{}, defaultColumns),
cliui.JSONFormat(),
)
client := new(codersdk.Client)
orgContext := NewOrganizationContext()
var templateVersion string
cmd := &serpent.Command{
Use: "list <template>",
Middleware: serpent.Chain(
serpent.RequireNArgs(1),
r.InitClient(client),
),
Short: "List all presets of the specified template. Defaults to the active template version.",
Options: serpent.OptionSet{
{
Name: "template-version",
Description: "Specify a template version to list presets for. Defaults to the active version.",
Flag: "template-version",
Value: serpent.StringOf(&templateVersion),
},
},
Handler: func(inv *serpent.Invocation) error {
organization, err := orgContext.Selected(inv, client)
if err != nil {
return xerrors.Errorf("get current organization: %w", err)
}
template, err := client.TemplateByName(inv.Context(), organization.ID, inv.Args[0])
if err != nil {
return xerrors.Errorf("get template by name: %w", err)
}
// If a template version is specified via flag, fetch that version by name
var version codersdk.TemplateVersion
if len(templateVersion) > 0 {
version, err = client.TemplateVersionByName(inv.Context(), template.ID, templateVersion)
if err != nil {
return xerrors.Errorf("get template version by name: %w", err)
}
} else {
// Otherwise, use the template's active version
version, err = client.TemplateVersion(inv.Context(), template.ActiveVersionID)
if err != nil {
return xerrors.Errorf("get active template version: %w", err)
}
}
presets, err := client.TemplateVersionPresets(inv.Context(), version.ID)
if err != nil {
return xerrors.Errorf("get template versions presets by template version: %w", err)
}
if len(presets) == 0 {
cliui.Infof(
inv.Stdout,
"No presets found for template %q and template-version %q.\n", template.Name, version.Name,
)
return nil
}
cliui.Infof(
inv.Stdout,
"Showing presets for template %q and template version %q.\n", template.Name, version.Name,
)
rows := templatePresetsToRows(presets...)
out, err := formatter.Format(inv.Context(), rows)
if err != nil {
return xerrors.Errorf("render table: %w", err)
}
_, err = fmt.Fprintln(inv.Stdout, out)
return err
},
}
orgContext.AttachOptions(cmd)
formatter.AttachOptions(&cmd.Options)
return cmd
}
type templatePresetRow struct {
// For json format:
TemplatePreset codersdk.Preset `table:"-"`
// For table format:
Name string `json:"-" table:"name,default_sort"`
Parameters string `json:"-" table:"parameters"`
Default bool `json:"-" table:"default"`
DesiredPrebuildInstances string `json:"-" table:"desired prebuild instances"`
}
func formatPresetParameters(params []codersdk.PresetParameter) string {
var paramsStr []string
for _, p := range params {
paramsStr = append(paramsStr, fmt.Sprintf("%s=%s", p.Name, p.Value))
}
return strings.Join(paramsStr, ",")
}
// templatePresetsToRows converts a list of presets to a list of rows
// for outputting.
func templatePresetsToRows(presets ...codersdk.Preset) []templatePresetRow {
rows := make([]templatePresetRow, len(presets))
for i, preset := range presets {
prebuildInstances := "-"
if preset.DesiredPrebuildInstances != nil {
prebuildInstances = strconv.Itoa(*preset.DesiredPrebuildInstances)
}
rows[i] = templatePresetRow{
Name: preset.Name,
Parameters: formatPresetParameters(preset.Parameters),
Default: preset.Default,
DesiredPrebuildInstances: prebuildInstances,
}
}
return rows
}
+228
View File
@@ -0,0 +1,228 @@
package cli_test
import (
"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"
"github.com/coder/coder/v2/provisioner/echo"
"github.com/coder/coder/v2/provisionersdk/proto"
"github.com/coder/coder/v2/pty/ptytest"
"github.com/coder/coder/v2/testutil"
)
func TestTemplatePresets(t *testing.T) {
t.Parallel()
t.Run("NoPresets", 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)
// Given: a template version without presets
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, templateWithPresets([]*proto.Preset{}))
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
// When: listing presets for that template
inv, root := clitest.New(t, "templates", "presets", "list", template.Name)
clitest.SetupConfig(t, member, root)
pty := ptytest.New(t).Attach(inv)
doneChan := make(chan struct{})
var runErr error
go func() {
defer close(doneChan)
runErr = inv.Run()
}()
<-doneChan
require.NoError(t, runErr)
// Should return a message when no presets are found for the given template and version.
notFoundMessage := fmt.Sprintf("No presets found for template %q and template-version %q.", template.Name, version.Name)
pty.ExpectRegexMatch(notFoundMessage)
})
t.Run("ListsPresetsForDefaultTemplateVersion", 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)
// Given: an active template version that includes presets
presets := []*proto.Preset{
{
Name: "preset-multiple-params",
Parameters: []*proto.PresetParameter{
{
Name: "k1",
Value: "v1",
}, {
Name: "k2",
Value: "v2",
},
},
},
{
Name: "preset-default",
Default: true,
Parameters: []*proto.PresetParameter{
{
Name: "k1",
Value: "v2",
},
},
Prebuild: &proto.Prebuild{
Instances: 0,
},
},
{
Name: "preset-prebuilds",
Parameters: []*proto.PresetParameter{},
Prebuild: &proto.Prebuild{
Instances: 2,
},
},
}
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, templateWithPresets(presets))
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
require.Equal(t, version.ID, template.ActiveVersionID)
// When: listing presets for that template
inv, root := clitest.New(t, "templates", "presets", "list", template.Name)
clitest.SetupConfig(t, member, root)
pty := ptytest.New(t).Attach(inv)
doneChan := make(chan struct{})
var runErr error
go func() {
defer close(doneChan)
runErr = inv.Run()
}()
<-doneChan
require.NoError(t, runErr)
// Should: return the active version's presets sorted by name
message := fmt.Sprintf("Showing presets for template %q and template version %q.", template.Name, version.Name)
pty.ExpectMatch(message)
pty.ExpectRegexMatch(`preset-default\s+k1=v2\s+true\s+0`)
// The parameter order is not guaranteed in the output, so we match both possible orders
pty.ExpectRegexMatch(`preset-multiple-params\s+(k1=v1,k2=v2)|(k2=v2,k1=v1)\s+false\s+-`)
pty.ExpectRegexMatch(`preset-prebuilds\s+\s+false\s+2`)
})
t.Run("ListsPresetsForSpecifiedTemplateVersion", func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitMedium)
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
owner := coderdtest.CreateFirstUser(t, client)
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
// Given: a template with an active version that has no presets,
// and another template version that includes presets
presets := []*proto.Preset{
{
Name: "preset-multiple-params",
Parameters: []*proto.PresetParameter{
{
Name: "k1",
Value: "v1",
}, {
Name: "k2",
Value: "v2",
},
},
},
{
Name: "preset-default",
Default: true,
Parameters: []*proto.PresetParameter{
{
Name: "k1",
Value: "v2",
},
},
Prebuild: &proto.Prebuild{
Instances: 0,
},
},
{
Name: "preset-prebuilds",
Parameters: []*proto.PresetParameter{},
Prebuild: &proto.Prebuild{
Instances: 2,
},
},
}
// Given: first template version with presets
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, templateWithPresets(presets))
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
// Given: second template version without presets
activeVersion := coderdtest.UpdateTemplateVersion(t, client, owner.OrganizationID, templateWithPresets([]*proto.Preset{}), template.ID)
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, activeVersion.ID)
// Given: second template version is the active version
err := client.UpdateActiveTemplateVersion(ctx, template.ID, codersdk.UpdateActiveTemplateVersion{
ID: activeVersion.ID,
})
require.NoError(t, err)
updatedTemplate, err := client.Template(ctx, template.ID)
require.NoError(t, err)
require.Equal(t, activeVersion.ID, updatedTemplate.ActiveVersionID)
// Given: template has two versions
templateVersions, err := client.TemplateVersionsByTemplate(ctx, codersdk.TemplateVersionsByTemplateRequest{
TemplateID: updatedTemplate.ID,
})
require.NoError(t, err)
require.Len(t, templateVersions, 2)
// When: listing presets for a specific template and its specified version
inv, root := clitest.New(t, "templates", "presets", "list", updatedTemplate.Name, "--template-version", version.Name)
clitest.SetupConfig(t, member, root)
pty := ptytest.New(t).Attach(inv)
doneChan := make(chan struct{})
var runErr error
go func() {
defer close(doneChan)
runErr = inv.Run()
}()
<-doneChan
require.NoError(t, runErr)
// Should: return the specified version's presets sorted by name
message := fmt.Sprintf("Showing presets for template %q and template version %q.", template.Name, version.Name)
pty.ExpectMatch(message)
pty.ExpectRegexMatch(`preset-default\s+k1=v2\s+true\s+0`)
// The parameter order is not guaranteed in the output, so we match both possible orders
pty.ExpectRegexMatch(`preset-multiple-params\s+(k1=v1,k2=v2)|(k2=v2,k1=v1)\s+false\s+-`)
pty.ExpectRegexMatch(`preset-prebuilds\s+\s+false\s+2`)
})
}
func templateWithPresets(presets []*proto.Preset) *echo.Responses {
return &echo.Responses{
Parse: echo.ParseComplete,
ProvisionPlan: []*proto.Response{
{
Type: &proto.Response_Plan{
Plan: &proto.PlanComplete{
Presets: presets,
},
},
},
},
}
}
+1
View File
@@ -33,6 +33,7 @@ func (r *RootCmd) templates() *serpent.Command {
r.templateList(),
r.templatePush(),
r.templateVersions(),
r.templatePresets(),
r.templateDelete(),
r.templatePull(),
r.archiveTemplateVersions(),
+1
View File
@@ -23,6 +23,7 @@ SUBCOMMANDS:
edit Edit the metadata of a template by name.
init Get started with a templated template.
list List all the templates available for the organization
presets Manage presets of the specified template
pull Download the active, latest, or specified version of a template
to a path.
push Create or update a template from the current directory or as
+24
View File
@@ -0,0 +1,24 @@
coder v0.0.0-devel
USAGE:
coder templates presets
Manage presets of the specified template
Aliases: preset
- List presets for the active version of a template:
$ coder templates presets list my-template
- List presets for a specific version of a template:
$ coder templates presets list my-template --template-version
my-template-version
SUBCOMMANDS:
list List all presets of the specified template. Defaults to the active
template version.
———
Run `coder --help` for a list of global options.
+24
View File
@@ -0,0 +1,24 @@
coder v0.0.0-devel
USAGE:
coder templates presets list [flags] <template>
List all presets of the specified template. Defaults to the active template
version.
OPTIONS:
-O, --org string, $CODER_ORGANIZATION
Select which organization (uuid or name) to use.
-c, --column [name|parameters|default|desired prebuild instances] (default: name,parameters,default,desired prebuild instances)
Columns to display in table output.
-o, --output table|json (default: table)
Output format.
--template-version string
Specify a template version to list presets for. Defaults to the active
version.
———
Run `coder --help` for a list of global options.
+3
View File
@@ -14880,6 +14880,9 @@ const docTemplate = `{
"default": {
"type": "boolean"
},
"desiredPrebuildInstances": {
"type": "integer"
},
"id": {
"type": "string"
},
+3
View File
@@ -13487,6 +13487,9 @@
"default": {
"type": "boolean"
},
"desiredPrebuildInstances": {
"type": "integer"
},
"id": {
"type": "string"
},
+13 -3
View File
@@ -1,6 +1,7 @@
package coderd
import (
"database/sql"
"net/http"
"github.com/coder/coder/v2/coderd/httpapi"
@@ -38,12 +39,21 @@ func (api *API) templateVersionPresets(rw http.ResponseWriter, r *http.Request)
return
}
convertPrebuildInstances := func(desiredInstances sql.NullInt32) *int {
if desiredInstances.Valid {
value := int(desiredInstances.Int32)
return &value
}
return nil
}
var res []codersdk.Preset
for _, preset := range presets {
sdkPreset := codersdk.Preset{
ID: preset.ID,
Name: preset.Name,
Default: preset.IsDefault,
ID: preset.ID,
Name: preset.Name,
Default: preset.IsDefault,
DesiredPrebuildInstances: convertPrebuildInstances(preset.DesiredInstances),
}
for _, presetParam := range presetParams {
if presetParam.TemplateVersionPresetID != preset.ID {
+5 -4
View File
@@ -11,10 +11,11 @@ import (
)
type Preset struct {
ID uuid.UUID
Name string
Parameters []PresetParameter
Default bool
ID uuid.UUID
Name string
Parameters []PresetParameter
Default bool
DesiredPrebuildInstances *int
}
type PresetParameter struct {
+10
View File
@@ -1651,6 +1651,16 @@
"description": "List all the templates available for the organization",
"path": "reference/cli/templates_list.md"
},
{
"title": "templates presets",
"description": "Manage presets of the specified template",
"path": "reference/cli/templates_presets.md"
},
{
"title": "templates presets list",
"description": "List all presets of the specified template. Defaults to the active template version.",
"path": "reference/cli/templates_presets_list.md"
},
{
"title": "templates pull",
"description": "Download the active, latest, or specified version of a template to a path.",
+8 -6
View File
@@ -5513,6 +5513,7 @@ Only certain features set these fields: - FeatureManagedAgentLimit|
```json
{
"default": true,
"desiredPrebuildInstances": 0,
"id": "string",
"name": "string",
"parameters": [
@@ -5526,12 +5527,13 @@ Only certain features set these fields: - FeatureManagedAgentLimit|
### Properties
| Name | Type | Required | Restrictions | Description |
|--------------|---------------------------------------------------------------|----------|--------------|-------------|
| `default` | boolean | false | | |
| `id` | string | false | | |
| `name` | string | false | | |
| `parameters` | array of [codersdk.PresetParameter](#codersdkpresetparameter) | false | | |
| Name | Type | Required | Restrictions | Description |
|----------------------------|---------------------------------------------------------------|----------|--------------|-------------|
| `default` | boolean | false | | |
| `desiredPrebuildInstances` | integer | false | | |
| `id` | string | false | | |
| `name` | string | false | | |
| `parameters` | array of [codersdk.PresetParameter](#codersdkpresetparameter) | false | | |
## codersdk.PresetParameter
+11 -9
View File
@@ -2914,6 +2914,7 @@ curl -X GET http://coder-server:8080/api/v2/templateversions/{templateversion}/p
[
{
"default": true,
"desiredPrebuildInstances": 0,
"id": "string",
"name": "string",
"parameters": [
@@ -2936,15 +2937,16 @@ curl -X GET http://coder-server:8080/api/v2/templateversions/{templateversion}/p
Status Code **200**
| Name | Type | Required | Restrictions | Description |
|----------------|---------|----------|--------------|-------------|
| `[array item]` | array | false | | |
| `» default` | boolean | false | | |
| id` | string | false | | |
| name` | string | false | | |
| parameters` | array | false | | |
| » name` | string | false | | |
| `»» value` | string | false | | |
| Name | Type | Required | Restrictions | Description |
|------------------------------|---------|----------|--------------|-------------|
| `[array item]` | array | false | | |
| `» default` | boolean | false | | |
| desiredPrebuildInstances` | integer | false | | |
| id` | string | false | | |
| name` | string | false | | |
| parameters` | array | false | | |
| `»» name` | string | false | | |
| `»» value` | string | false | | |
To perform this operation, you must be authenticated. [Learn more](authentication.md).
+1
View File
@@ -33,6 +33,7 @@ workspaces:
| [<code>list</code>](./templates_list.md) | List all the templates available for the organization |
| [<code>push</code>](./templates_push.md) | Create or update a template from the current directory or as specified by flag |
| [<code>versions</code>](./templates_versions.md) | Manage different versions of the specified template |
| [<code>presets</code>](./templates_presets.md) | Manage presets of the specified template |
| [<code>delete</code>](./templates_delete.md) | Delete templates |
| [<code>pull</code>](./templates_pull.md) | Download the active, latest, or specified version of a template to a path. |
| [<code>archive</code>](./templates_archive.md) | Archive unused or failed template versions from a given template(s) |
+32
View File
@@ -0,0 +1,32 @@
<!-- DO NOT EDIT | GENERATED CONTENT -->
# templates presets
Manage presets of the specified template
Aliases:
* preset
## Usage
```console
coder templates presets
```
## Description
```console
- List presets for the active version of a template:
$ coder templates presets list my-template
- List presets for a specific version of a template:
$ coder templates presets list my-template --template-version my-template-version
```
## Subcommands
| Name | Purpose |
|--------------------------------------------------|--------------------------------------------------------------------------------------|
| [<code>list</code>](./templates_presets_list.md) | List all presets of the specified template. Defaults to the active template version. |
+47
View File
@@ -0,0 +1,47 @@
<!-- DO NOT EDIT | GENERATED CONTENT -->
# templates presets list
List all presets of the specified template. Defaults to the active template version.
## Usage
```console
coder templates presets list [flags] <template>
```
## Options
### --template-version
| | |
|------|---------------------|
| Type | <code>string</code> |
Specify a template version to list presets for. Defaults to the active version.
### -O, --org
| | |
|-------------|----------------------------------|
| Type | <code>string</code> |
| Environment | <code>$CODER_ORGANIZATION</code> |
Select which organization (uuid or name) to use.
### -c, --column
| | |
|---------|----------------------------------------------------------------------|
| Type | <code>[name\|parameters\|default\|desired prebuild instances]</code> |
| Default | <code>name,parameters,default,desired prebuild instances</code> |
Columns to display in table output.
### -o, --output
| | |
|---------|--------------------------|
| Type | <code>table\|json</code> |
| Default | <code>table</code> |
Output format.
+1
View File
@@ -1997,6 +1997,7 @@ export interface Preset {
readonly Name: string;
readonly Parameters: readonly PresetParameter[];
readonly Default: boolean;
readonly DesiredPrebuildInstances: number | null;
}
// From codersdk/presets.go
@@ -133,6 +133,7 @@ export const PresetsButNoneSelected: Story = {
Value: "preset 1 override",
},
],
DesiredPrebuildInstances: null,
},
{
ID: "preset-2",
@@ -144,6 +145,7 @@ export const PresetsButNoneSelected: Story = {
Value: "42",
},
],
DesiredPrebuildInstances: null,
},
],
parameters: [
@@ -257,6 +259,7 @@ export const PresetsWithDefault: Story = {
Value: "preset 1 override",
},
],
DesiredPrebuildInstances: null,
},
{
ID: "preset-2",
@@ -268,6 +271,7 @@ export const PresetsWithDefault: Story = {
Value: "150189",
},
],
DesiredPrebuildInstances: null,
},
],
parameters: [