mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
feat: Expose managed variables via API (#6134)
* WIP * hcl * useManagedVariables * fix * Fix * Fix * fix * go:build * Fix * fix: bool flag * Insert template variables * API * fix * Expose via API * More wiring * CLI for testing purposes * WIP * Delete FIXME * planVars * WIP * WIP * UserVariableValues * no dry run * Dry run * Done FIXME * Fix * Fix: CLI * Fix: migration * API tests * Test info * Tests * More tests * fix: lint * Fix: authz * Address PR comments * Fix * fix * fix
This commit is contained in:
@ -243,6 +243,48 @@ func (api *API) templateVersionRichParameters(rw http.ResponseWriter, r *http.Re
|
||||
httpapi.Write(ctx, rw, http.StatusOK, templateVersionParameters)
|
||||
}
|
||||
|
||||
// @Summary Get template variables by template version
|
||||
// @ID get-template-variables-by-template-version
|
||||
// @Security CoderSessionToken
|
||||
// @Produce json
|
||||
// @Tags Templates
|
||||
// @Param templateversion path string true "Template version ID" format(uuid)
|
||||
// @Success 200 {array} codersdk.TemplateVersionVariable
|
||||
// @Router /templateversions/{templateversion}/variables [get]
|
||||
func (api *API) templateVersionVariables(rw http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
templateVersion := httpmw.TemplateVersionParam(r)
|
||||
template := httpmw.TemplateParam(r)
|
||||
if !api.Authorize(r, rbac.ActionRead, templateVersion.RBACObject(template)) {
|
||||
httpapi.ResourceNotFound(rw)
|
||||
return
|
||||
}
|
||||
job, err := api.Database.GetProvisionerJobByID(ctx, templateVersion.JobID)
|
||||
if err != nil {
|
||||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
||||
Message: "Internal error fetching provisioner job.",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
if !job.CompletedAt.Valid {
|
||||
httpapi.Write(ctx, rw, http.StatusPreconditionFailed, codersdk.Response{
|
||||
Message: "Job hasn't completed!",
|
||||
})
|
||||
return
|
||||
}
|
||||
dbTemplateVersionVariables, err := api.Database.GetTemplateVersionVariables(ctx, templateVersion.ID)
|
||||
if err != nil {
|
||||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
||||
Message: "Internal error fetching template version variables.",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
httpapi.Write(ctx, rw, http.StatusOK, convertTemplateVersionVariables(dbTemplateVersionVariables))
|
||||
}
|
||||
|
||||
// @Summary Get parameters by template version
|
||||
// @ID get-parameters-by-template-version
|
||||
// @Security CoderSessionToken
|
||||
@ -1260,7 +1302,8 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
|
||||
|
||||
templateVersionID := uuid.New()
|
||||
jobInput, err := json.Marshal(provisionerdserver.TemplateVersionImportJob{
|
||||
TemplateVersionID: templateVersionID,
|
||||
TemplateVersionID: templateVersionID,
|
||||
UserVariableValues: req.UserVariableValues,
|
||||
})
|
||||
if err != nil {
|
||||
return xerrors.Errorf("marshal job input: %w", err)
|
||||
@ -1480,6 +1523,26 @@ func convertTemplateVersionParameter(param database.TemplateVersionParameter) (c
|
||||
}, nil
|
||||
}
|
||||
|
||||
func convertTemplateVersionVariables(dbVariables []database.TemplateVersionVariable) []codersdk.TemplateVersionVariable {
|
||||
variables := make([]codersdk.TemplateVersionVariable, 0)
|
||||
for _, dbVariable := range dbVariables {
|
||||
variables = append(variables, convertTemplateVersionVariable(dbVariable))
|
||||
}
|
||||
return variables
|
||||
}
|
||||
|
||||
func convertTemplateVersionVariable(variable database.TemplateVersionVariable) codersdk.TemplateVersionVariable {
|
||||
return codersdk.TemplateVersionVariable{
|
||||
Name: variable.Name,
|
||||
Description: variable.Description,
|
||||
Type: variable.Type,
|
||||
Value: variable.Value,
|
||||
DefaultValue: variable.DefaultValue,
|
||||
Required: variable.Required,
|
||||
Sensitive: variable.Sensitive,
|
||||
}
|
||||
}
|
||||
|
||||
func watchTemplateChannel(id uuid.UUID) string {
|
||||
return fmt.Sprintf("template:%s", id)
|
||||
}
|
||||
|
Reference in New Issue
Block a user