Skip to content

feat(coderd): add support for presets to the coder API #16526

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 12 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ provisioner/terraform/testdata/version:
.PHONY: provisioner/terraform/testdata/version

test:
$(GIT_FLAGS) gotestsum --format standard-quiet -- -v -short -count=1 ./...
$(GIT_FLAGS) gotestsum --format standard-quiet -- -v -short -count=1 ./... $(if $(RUN),-run $(RUN))
.PHONY: test

test-cli:
Expand Down
101 changes: 101 additions & 0 deletions coderd/apidoc/docs.go

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

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

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

3 changes: 3 additions & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,9 @@ func New(options *Options) *API {
r.Get("/rich-parameters", api.templateVersionRichParameters)
r.Get("/external-auth", api.templateVersionExternalAuth)
r.Get("/variables", api.templateVersionVariables)
r.Route("/presets", func(r chi.Router) {
r.Get("/", api.templateVersionPresets)
})
r.Get("/resources", api.templateVersionResources)
r.Get("/logs", api.templateVersionLogs)
r.Route("/dry-run", func(r chi.Router) {
Expand Down
1 change: 0 additions & 1 deletion coderd/database/dbauthz/dbauthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3823,7 +3823,6 @@ func (s *MethodTestSuite) TestSystemFunctions() {
})
require.NoError(s.T(), err)
_, err = db.InsertPresetParameters(ctx, database.InsertPresetParametersParams{
ID: uuid.New(),
TemplateVersionPresetID: preset.ID,
Names: []string{"test"},
Values: []string{"test"},
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/dump.sql

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE template_version_preset_parameters
ALTER COLUMN id DROP DEFAULT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE template_version_preset_parameters
ALTER COLUMN id SET DEFAULT gen_random_uuid();
15 changes: 4 additions & 11 deletions coderd/database/queries.sql.go

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

3 changes: 1 addition & 2 deletions coderd/database/queries/presets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ VALUES

-- name: InsertPresetParameters :many
INSERT INTO
template_version_preset_parameters (id, template_version_preset_id, name, value)
template_version_preset_parameters (template_version_preset_id, name, value)
SELECT
@id,
@template_version_preset_id,
unnest(@names :: TEXT[]),
unnest(@values :: TEXT[])
Expand Down
62 changes: 62 additions & 0 deletions coderd/presets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package coderd

import (
"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
}

// TODO (sasswart): Test case: what if a user tries to read presets or preset parameters from a different org?
// TODO (sasswart): Do a prelim auth check here.

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
}

var res []codersdk.Preset
for _, preset := range presets {
sdkPreset := codersdk.Preset{
ID: preset.ID,
Name: preset.Name,
}
for _, presetParam := range presetParams {
if presetParam.TemplateVersionPresetID == preset.ID {
sdkPreset.Parameters = append(sdkPreset.Parameters, codersdk.PresetParameter{
Name: presetParam.Name,
Value: presetParam.Value,
})
}
}
res = append(res, sdkPreset)
}

httpapi.Write(ctx, rw, http.StatusOK, res)
}
Loading
Loading