Skip to content

feat: add rich parameters #4311

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

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 cli/cliui/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/coder/coder/codersdk"
)

func ParameterSchema(cmd *cobra.Command, parameterSchema codersdk.ParameterSchema) (string, error) {
func DeprecatedParameterSchema(cmd *cobra.Command, parameterSchema codersdk.DeprecatedParameterSchema) (string, error) {
_, _ = fmt.Fprintln(cmd.OutOrStdout(), Styles.Bold.Render("var."+parameterSchema.Name))
if parameterSchema.Description != "" {
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+strings.TrimSpace(strings.Join(strings.Split(parameterSchema.Description, "\n"), "\n "))+"\n")
Expand Down
51 changes: 26 additions & 25 deletions cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ func create() *cobra.Command {
}

parameters, err := prepWorkspaceBuild(cmd, client, prepWorkspaceBuildArgs{
Template: template,
ExistingParams: []codersdk.Parameter{},
ParameterFile: parameterFile,
NewWorkspaceName: workspaceName,
Template: template,
ExistingDeprecatedParams: []codersdk.DeprecatedParameter{},
ParameterFile: parameterFile,
NewWorkspaceName: workspaceName,
})
if err != nil {
return err
Expand All @@ -140,11 +140,11 @@ func create() *cobra.Command {
}

workspace, err := client.CreateWorkspace(cmd.Context(), organization.ID, codersdk.Me, codersdk.CreateWorkspaceRequest{
TemplateID: template.ID,
Name: workspaceName,
AutostartSchedule: schedSpec,
TTLMillis: ptr.Ref(stopAfter.Milliseconds()),
ParameterValues: parameters,
TemplateID: template.ID,
Name: workspaceName,
AutostartSchedule: schedSpec,
TTLMillis: ptr.Ref(stopAfter.Milliseconds()),
DeprecatedParameterValues: parameters,
})
if err != nil {
return err
Expand All @@ -169,21 +169,22 @@ func create() *cobra.Command {
}

type prepWorkspaceBuildArgs struct {
Template codersdk.Template
ExistingParams []codersdk.Parameter
ParameterFile string
NewWorkspaceName string
Template codersdk.Template
ExistingParams []codersdk.WorkspaceBuildParameter
ExistingDeprecatedParams []codersdk.DeprecatedParameter
ParameterFile string
NewWorkspaceName string
}

// prepWorkspaceBuild will ensure a workspace build will succeed on the latest template version.
// Any missing params will be prompted to the user.
func prepWorkspaceBuild(cmd *cobra.Command, client *codersdk.Client, args prepWorkspaceBuildArgs) ([]codersdk.CreateParameterRequest, error) {
func prepWorkspaceBuild(cmd *cobra.Command, client *codersdk.Client, args prepWorkspaceBuildArgs) ([]codersdk.DeprecatedCreateParameterRequest, error) {
ctx := cmd.Context()
templateVersion, err := client.TemplateVersion(ctx, args.Template.ActiveVersionID)
if err != nil {
return nil, err
}
parameterSchemas, err := client.TemplateVersionSchema(ctx, templateVersion.ID)
deprecatedParameterSchemas, err := client.DeprecatedTemplateVersionSchema(ctx, templateVersion.ID)
if err != nil {
return nil, err
}
Expand All @@ -200,9 +201,9 @@ func prepWorkspaceBuild(cmd *cobra.Command, client *codersdk.Client, args prepWo
}
}
disclaimerPrinted := false
parameters := make([]codersdk.CreateParameterRequest, 0)
PromptParamLoop:
for _, parameterSchema := range parameterSchemas {
deprecatedParameters := make([]codersdk.DeprecatedCreateParameterRequest, 0)
PromptDeprecatedParamLoop:
for _, parameterSchema := range deprecatedParameterSchemas {
if !parameterSchema.AllowOverrideSource {
continue
}
Expand All @@ -213,24 +214,24 @@ PromptParamLoop:

// Param file is all or nothing
if !useParamFile {
for _, e := range args.ExistingParams {
for _, e := range args.ExistingDeprecatedParams {
if e.Name == parameterSchema.Name {
// If the param already exists, we do not need to prompt it again.
// The workspace scope will reuse params for each build.
continue PromptParamLoop
continue PromptDeprecatedParamLoop
}
}
}

parameterValue, err := getParameterValueFromMapOrInput(cmd, parameterMapFromFile, parameterSchema)
parameterValue, err := getDeprecatedParameterValueFromMapOrInput(cmd, parameterMapFromFile, parameterSchema)
if err != nil {
return nil, err
}

parameters = append(parameters, codersdk.CreateParameterRequest{
deprecatedParameters = append(deprecatedParameters, codersdk.DeprecatedCreateParameterRequest{
Name: parameterSchema.Name,
SourceValue: parameterValue,
SourceScheme: codersdk.ParameterSourceSchemeData,
SourceScheme: codersdk.DeprecatedParameterSourceSchemeData,
DestinationScheme: parameterSchema.DefaultDestinationScheme,
})
}
Expand All @@ -239,7 +240,7 @@ PromptParamLoop:
// Run a dry-run with the given parameters to check correctness
dryRun, err := client.CreateTemplateVersionDryRun(cmd.Context(), templateVersion.ID, codersdk.CreateTemplateVersionDryRunRequest{
WorkspaceName: args.NewWorkspaceName,
ParameterValues: parameters,
ParameterValues: deprecatedParameters,
})
if err != nil {
return nil, xerrors.Errorf("begin workspace dry-run: %w", err)
Expand Down Expand Up @@ -279,5 +280,5 @@ PromptParamLoop:
return nil, err
}

return parameters, nil
return deprecatedParameters, nil
}
30 changes: 12 additions & 18 deletions cli/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ func TestCreate(t *testing.T) {
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
Parse: []*proto.Parse_Response{{
Type: &proto.Parse_Response_Complete{
Complete: &proto.Parse_Complete{
Parse: []*proto.DeprecatedParse_Response{{
Type: &proto.DeprecatedParse_Response_Complete{
Complete: &proto.DeprecatedParse_Complete{
ParameterSchemas: echo.ParameterSuccess,
},
},
Expand Down Expand Up @@ -297,35 +297,29 @@ func TestCreate(t *testing.T) {
})
}

func createTestParseResponseWithDefault(defaultValue string) []*proto.Parse_Response {
return []*proto.Parse_Response{{
Type: &proto.Parse_Response_Complete{
Complete: &proto.Parse_Complete{
ParameterSchemas: []*proto.ParameterSchema{
func createTestParseResponseWithDefault(defaultValue string) []*proto.DeprecatedParse_Response {
return []*proto.DeprecatedParse_Response{{
Type: &proto.DeprecatedParse_Response_Complete{
Complete: &proto.DeprecatedParse_Complete{
ParameterSchemas: []*proto.DeprecatedParameterSchema{
{
AllowOverrideSource: true,
Name: "region",
Description: "description 1",
DefaultSource: &proto.ParameterSource{
Scheme: proto.ParameterSource_DATA,
DefaultSource: &proto.DeprecatedParameterSource{
Scheme: proto.DeprecatedParameterSource_DATA,
Value: defaultValue,
},
DefaultDestination: &proto.ParameterDestination{
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
},
},
{
AllowOverrideSource: true,
Name: "username",
Description: "description 2",
DefaultSource: &proto.ParameterSource{
Scheme: proto.ParameterSource_DATA,
DefaultSource: &proto.DeprecatedParameterSource{
Scheme: proto.DeprecatedParameterSource_DATA,
// No default value
Value: "",
},
DefaultDestination: &proto.ParameterDestination{
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
},
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions cli/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func createParameterMapFromFile(parameterFile string) (map[string]string, error)

// Returns a parameter value from a given map, if the map exists, else takes input from the user.
// Throws an error if the map exists but does not include a value for the parameter.
func getParameterValueFromMapOrInput(cmd *cobra.Command, parameterMap map[string]string, parameterSchema codersdk.ParameterSchema) (string, error) {
func getDeprecatedParameterValueFromMapOrInput(cmd *cobra.Command, parameterMap map[string]string, parameterSchema codersdk.DeprecatedParameterSchema) (string, error) {
var parameterValue string
if parameterMap != nil {
var ok bool
Expand All @@ -48,7 +48,7 @@ func getParameterValueFromMapOrInput(cmd *cobra.Command, parameterMap map[string
}
} else {
var err error
parameterValue, err = cliui.ParameterSchema(cmd, parameterSchema)
parameterValue, err = cliui.DeprecatedParameterSchema(cmd, parameterSchema)
if err != nil {
return "", err
}
Expand Down
16 changes: 8 additions & 8 deletions cli/parameterslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ func parameterList() *cobra.Command {
}

var scopeID uuid.UUID
switch codersdk.ParameterScope(scope) {
case codersdk.ParameterWorkspace:
switch codersdk.DeprecatedParameterScope(scope) {
case codersdk.DeprecatedParameterWorkspace:
workspace, err := namedWorkspace(cmd, client, name)
if err != nil {
return err
}
scopeID = workspace.ID
case codersdk.ParameterTemplate:
case codersdk.DeprecatedParameterTemplate:
template, err := client.TemplateByName(cmd.Context(), organization.ID, name)
if err != nil {
return xerrors.Errorf("get workspace template: %w", err)
}
scopeID = template.ID
case codersdk.ParameterImportJob, "template_version":
scope = string(codersdk.ParameterImportJob)
case codersdk.DeprecatedParameterImportJob, "template_version":
scope = string(codersdk.DeprecatedParameterImportJob)
scopeID, err = uuid.Parse(name)
if err != nil {
return xerrors.Errorf("%q must be a uuid for this scope type", name)
Expand All @@ -61,12 +61,12 @@ func parameterList() *cobra.Command {
}

default:
return xerrors.Errorf("%q is an unsupported scope, use %v", scope, []codersdk.ParameterScope{
codersdk.ParameterWorkspace, codersdk.ParameterTemplate, codersdk.ParameterImportJob,
return xerrors.Errorf("%q is an unsupported scope, use %v", scope, []codersdk.DeprecatedParameterScope{
codersdk.DeprecatedParameterWorkspace, codersdk.DeprecatedParameterTemplate, codersdk.DeprecatedParameterImportJob,
})
}

params, err := client.Parameters(cmd.Context(), codersdk.ParameterScope(scope), scopeID)
params, err := client.DeprecatedParameters(cmd.Context(), codersdk.DeprecatedParameterScope(scope), scopeID)
if err != nil {
return xerrors.Errorf("fetch params: %w", err)
}
Expand Down
22 changes: 11 additions & 11 deletions cli/templatecreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ type createValidTemplateVersionArgs struct {
ProvisionerTags map[string]string
}

func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVersionArgs, parameters ...codersdk.CreateParameterRequest) (*codersdk.TemplateVersion, []codersdk.CreateParameterRequest, error) {
func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVersionArgs, parameters ...codersdk.DeprecatedCreateParameterRequest) (*codersdk.TemplateVersion, []codersdk.DeprecatedCreateParameterRequest, error) {
client := args.Client

req := codersdk.CreateTemplateVersionRequest{
Expand Down Expand Up @@ -205,27 +205,27 @@ func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVers
if err != nil {
return nil, nil, err
}
parameterSchemas, err := client.TemplateVersionSchema(cmd.Context(), version.ID)
parameterSchemas, err := client.DeprecatedTemplateVersionSchema(cmd.Context(), version.ID)
if err != nil {
return nil, nil, err
}
parameterValues, err := client.TemplateVersionParameters(cmd.Context(), version.ID)
parameterValues, err := client.DeprecatedTemplateVersionParameters(cmd.Context(), version.ID)
if err != nil {
return nil, nil, err
}

// lastParameterValues are pulled from the current active template version if
// templateID is provided. This allows pulling params from the last
// version instead of prompting if we are updating template versions.
lastParameterValues := make(map[string]codersdk.Parameter)
lastParameterValues := make(map[string]codersdk.DeprecatedParameter)
if args.ReuseParameters && args.Template != nil {
activeVersion, err := client.TemplateVersion(cmd.Context(), args.Template.ActiveVersionID)
if err != nil {
return nil, nil, xerrors.Errorf("Fetch current active template version: %w", err)
}

// We don't want to compute the params, we only want to copy from this scope
values, err := client.Parameters(cmd.Context(), codersdk.ParameterImportJob, activeVersion.Job.ID)
values, err := client.DeprecatedParameters(cmd.Context(), codersdk.DeprecatedParameterImportJob, activeVersion.Job.ID)
if err != nil {
return nil, nil, xerrors.Errorf("Fetch previous version parameters: %w", err)
}
Expand All @@ -235,7 +235,7 @@ func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVers
}

if provisionerd.IsMissingParameterError(version.Job.Error) {
valuesBySchemaID := map[string]codersdk.ComputedParameter{}
valuesBySchemaID := map[string]codersdk.DeprecatedComputedParameter{}
for _, parameterValue := range parameterValues {
valuesBySchemaID[parameterValue.SchemaID.String()] = parameterValue
}
Expand All @@ -252,7 +252,7 @@ func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVers

// pulled params come from the last template version
pulled := make([]string, 0)
missingSchemas := make([]codersdk.ParameterSchema, 0)
missingSchemas := make([]codersdk.DeprecatedParameterSchema, 0)
for _, parameterSchema := range parameterSchemas {
_, ok := valuesBySchemaID[parameterSchema.ID.String()]
if ok {
Expand All @@ -265,7 +265,7 @@ func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVers
if inherit, ok := lastParameterValues[parameterSchema.Name]; ok && !fileOk {
// If the value is not in the param file, and can be pulled from the last template version,
// then don't mark it as missing.
parameters = append(parameters, codersdk.CreateParameterRequest{
parameters = append(parameters, codersdk.DeprecatedCreateParameterRequest{
CloneID: inherit.ID,
})
pulled = append(pulled, fmt.Sprintf("%q", parameterSchema.Name))
Expand All @@ -282,14 +282,14 @@ func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVers
_, _ = fmt.Fprint(cmd.OutOrStdout(), "\r\n")

for _, parameterSchema := range missingSchemas {
parameterValue, err := getParameterValueFromMapOrInput(cmd, parameterMapFromFile, parameterSchema)
parameterValue, err := getDeprecatedParameterValueFromMapOrInput(cmd, parameterMapFromFile, parameterSchema)
if err != nil {
return nil, nil, err
}
parameters = append(parameters, codersdk.CreateParameterRequest{
parameters = append(parameters, codersdk.DeprecatedCreateParameterRequest{
Name: parameterSchema.Name,
SourceValue: parameterValue,
SourceScheme: codersdk.ParameterSourceSchemeData,
SourceScheme: codersdk.DeprecatedParameterSourceSchemeData,
DestinationScheme: parameterSchema.DefaultDestinationScheme,
})
_, _ = fmt.Fprintln(cmd.OutOrStdout())
Expand Down
13 changes: 5 additions & 8 deletions cli/templatecreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,14 @@ func TestTemplateCreate(t *testing.T) {
})
}

func createTestParseResponse() []*proto.Parse_Response {
return []*proto.Parse_Response{{
Type: &proto.Parse_Response_Complete{
Complete: &proto.Parse_Complete{
ParameterSchemas: []*proto.ParameterSchema{{
func createTestParseResponse() []*proto.DeprecatedParse_Response {
return []*proto.DeprecatedParse_Response{{
Type: &proto.DeprecatedParse_Response_Complete{
Complete: &proto.DeprecatedParse_Complete{
ParameterSchemas: []*proto.DeprecatedParameterSchema{{
AllowOverrideSource: true,
Name: "region",
Description: "description",
DefaultDestination: &proto.ParameterDestination{
Scheme: proto.ParameterDestination_PROVISIONER_VARIABLE,
},
}},
},
},
Expand Down
8 changes: 4 additions & 4 deletions cli/templatepull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,18 @@ func TestTemplatePull(t *testing.T) {
// a template version source.
func genTemplateVersionSource() *echo.Responses {
return &echo.Responses{
Parse: []*proto.Parse_Response{
Parse: []*proto.DeprecatedParse_Response{
{
Type: &proto.Parse_Response_Log{
Type: &proto.DeprecatedParse_Response_Log{
Log: &proto.Log{
Output: uuid.NewString(),
},
},
},

{
Type: &proto.Parse_Response_Complete{
Complete: &proto.Parse_Complete{},
Type: &proto.DeprecatedParse_Response_Complete{
Complete: &proto.DeprecatedParse_Complete{},
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions cli/templatepush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,15 @@ func TestTemplatePush(t *testing.T) {
})
}

func latestTemplateVersion(t *testing.T, client *codersdk.Client, templateID uuid.UUID) (codersdk.TemplateVersion, []codersdk.Parameter) {
func latestTemplateVersion(t *testing.T, client *codersdk.Client, templateID uuid.UUID) (codersdk.TemplateVersion, []codersdk.DeprecatedParameter) {
t.Helper()

ctx := context.Background()
newTemplate, err := client.Template(ctx, templateID)
require.NoError(t, err)
tv, err := client.TemplateVersion(ctx, newTemplate.ActiveVersionID)
require.NoError(t, err)
params, err := client.Parameters(ctx, codersdk.ParameterImportJob, tv.Job.ID)
params, err := client.DeprecatedParameters(ctx, codersdk.DeprecatedParameterImportJob, tv.Job.ID)
require.NoError(t, err)

return tv, params
Expand Down
Loading