mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
feat: Expose the values contained in an HCL validation string to the API (#1587)
* feat: Expose the values contained in an HCL validation string to the API This allows the frontend to render inputs displaying these values! * Update codersdk/parameters.go Co-authored-by: Cian Johnston <cian@coder.com> * Call a spade a space * Fix linting errors with type conversion Co-authored-by: Cian Johnston <cian@coder.com>
This commit is contained in:
@ -8,9 +8,11 @@ import (
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/coder/coder/coderd/database"
|
||||
"github.com/coder/coder/coderd/httpapi"
|
||||
"github.com/coder/coder/coderd/parameter"
|
||||
"github.com/coder/coder/codersdk"
|
||||
)
|
||||
|
||||
@ -122,6 +124,37 @@ func (api *api) deleteParameter(rw http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
func convertParameterSchema(parameterSchema database.ParameterSchema) (codersdk.ParameterSchema, error) {
|
||||
contains := []string{}
|
||||
if parameterSchema.ValidationCondition != "" {
|
||||
var err error
|
||||
contains, _, err = parameter.Contains(parameterSchema.ValidationCondition)
|
||||
if err != nil {
|
||||
return codersdk.ParameterSchema{}, xerrors.Errorf("parse validation condition for %q: %w", parameterSchema.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
return codersdk.ParameterSchema{
|
||||
ID: parameterSchema.ID,
|
||||
CreatedAt: parameterSchema.CreatedAt,
|
||||
JobID: parameterSchema.JobID,
|
||||
Name: parameterSchema.Name,
|
||||
Description: parameterSchema.Description,
|
||||
DefaultSourceScheme: string(parameterSchema.DefaultSourceScheme),
|
||||
DefaultSourceValue: parameterSchema.DefaultSourceValue,
|
||||
AllowOverrideSource: parameterSchema.AllowOverrideSource,
|
||||
DefaultDestinationScheme: string(parameterSchema.DefaultDestinationScheme),
|
||||
AllowOverrideDestination: parameterSchema.AllowOverrideDestination,
|
||||
DefaultRefresh: parameterSchema.DefaultRefresh,
|
||||
RedisplayValue: parameterSchema.RedisplayValue,
|
||||
ValidationError: parameterSchema.ValidationError,
|
||||
ValidationCondition: parameterSchema.ValidationCondition,
|
||||
ValidationTypeSystem: string(parameterSchema.ValidationTypeSystem),
|
||||
ValidationValueType: parameterSchema.ValidationValueType,
|
||||
ValidationContains: contains,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func convertParameterValue(parameterValue database.ParameterValue) codersdk.Parameter {
|
||||
return codersdk.Parameter{
|
||||
ID: parameterValue.ID,
|
||||
|
@ -95,11 +95,18 @@ func (api *api) templateVersionSchema(rw http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
return
|
||||
}
|
||||
if schemas == nil {
|
||||
schemas = []database.ParameterSchema{}
|
||||
apiSchemas := make([]codersdk.ParameterSchema, 0)
|
||||
for _, schema := range schemas {
|
||||
apiSchema, err := convertParameterSchema(schema)
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("convert: %s", err),
|
||||
})
|
||||
return
|
||||
}
|
||||
apiSchemas = append(apiSchemas, apiSchema)
|
||||
}
|
||||
|
||||
httpapi.Write(rw, http.StatusOK, schemas)
|
||||
httpapi.Write(rw, http.StatusOK, apiSchemas)
|
||||
}
|
||||
|
||||
func (api *api) templateVersionParameters(rw http.ResponseWriter, r *http.Request) {
|
||||
|
@ -198,6 +198,36 @@ func TestTemplateVersionSchema(t *testing.T) {
|
||||
require.NotNil(t, schemas)
|
||||
require.Len(t, schemas, 1)
|
||||
})
|
||||
t.Run("ListContains", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := coderdtest.New(t, nil)
|
||||
user := coderdtest.CreateFirstUser(t, client)
|
||||
coderdtest.NewProvisionerDaemon(t, client)
|
||||
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
|
||||
Parse: []*proto.Parse_Response{{
|
||||
Type: &proto.Parse_Response_Complete{
|
||||
Complete: &proto.Parse_Complete{
|
||||
ParameterSchemas: []*proto.ParameterSchema{{
|
||||
Name: "example",
|
||||
ValidationTypeSystem: proto.ParameterSchema_HCL,
|
||||
ValidationValueType: "string",
|
||||
ValidationCondition: `contains(["first", "second"], var.example)`,
|
||||
DefaultDestination: &proto.ParameterDestination{
|
||||
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
}},
|
||||
Provision: echo.ProvisionComplete,
|
||||
})
|
||||
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
|
||||
schemas, err := client.TemplateVersionSchema(context.Background(), version.ID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, schemas)
|
||||
require.Len(t, schemas, 1)
|
||||
require.Equal(t, []string{"first", "second"}, schemas[0].ValidationContains)
|
||||
})
|
||||
}
|
||||
|
||||
func TestTemplateVersionParameters(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user