Files
coder/coderd/presets.go
T
Susana Ferreira 931b97caab 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 -->
2025-07-24 16:44:36 +01:00

73 lines
2.0 KiB
Go

package coderd
import (
"database/sql"
"net/http"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
"github.com/coder/coder/v2/codersdk"
)
// @Summary Get template version presets
// @ID get-template-version-presets
// @Security CoderSessionToken
// @Produce json
// @Tags Templates
// @Param templateversion path string true "Template version ID" format(uuid)
// @Success 200 {array} codersdk.Preset
// @Router /templateversions/{templateversion}/presets [get]
func (api *API) templateVersionPresets(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
templateVersion := httpmw.TemplateVersionParam(r)
presets, err := api.Database.GetPresetsByTemplateVersionID(ctx, templateVersion.ID)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching template version presets.",
Detail: err.Error(),
})
return
}
presetParams, err := api.Database.GetPresetParametersByTemplateVersionID(ctx, templateVersion.ID)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching template version presets.",
Detail: err.Error(),
})
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,
DesiredPrebuildInstances: convertPrebuildInstances(preset.DesiredInstances),
}
for _, presetParam := range presetParams {
if presetParam.TemplateVersionPresetID != preset.ID {
continue
}
sdkPreset.Parameters = append(sdkPreset.Parameters, codersdk.PresetParameter{
Name: presetParam.Name,
Value: presetParam.Value,
})
}
res = append(res, sdkPreset)
}
httpapi.Write(ctx, rw, http.StatusOK, res)
}