Skip to content

feat: support partial parameter files #5392

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 2 commits into from
Dec 14, 2022
Merged
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
34 changes: 29 additions & 5 deletions cli/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,16 @@ func TestCreate(t *testing.T) {
ProvisionApply: echo.ProvisionComplete,
ProvisionPlan: echo.ProvisionComplete,
})

coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
_ = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

tempDir := t.TempDir()
removeTmpDirUntilSuccessAfterTest(t, tempDir)
parameterFile, _ := os.CreateTemp(tempDir, "testParameterFile*.yaml")
_, _ = parameterFile.WriteString("zone: \"bananas\"")
cmd, root := clitest.New(t, "create", "my-workspace", "--template", template.Name, "--parameter-file", parameterFile.Name())
_, _ = parameterFile.WriteString("username: \"boingo\"")

cmd, root := clitest.New(t, "create", "", "--parameter-file", parameterFile.Name())
clitest.SetupConfig(t, client, root)
doneChan := make(chan struct{})
pty := ptytest.New(t)
Expand All @@ -247,11 +250,32 @@ func TestCreate(t *testing.T) {
go func() {
defer close(doneChan)
err := cmd.Execute()
assert.EqualError(t, err, "Parameter value absent in parameter file for \"region\"!")
assert.NoError(t, err)
}()
matches := []struct {
match string
write string
}{
{
match: "Specify a name",
write: "my-workspace",
},
{
match: fmt.Sprintf("Enter a value (default: %q):", defaultValue),
write: "bingo",
},
{
match: "Confirm create?",
write: "yes",
},
}

for _, m := range matches {
pty.ExpectMatch(m.match)
pty.WriteLine(m.write)
}
<-doneChan
})

t.Run("FailedDryRun", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
Expand Down
11 changes: 7 additions & 4 deletions cli/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,21 @@ func createParameterMapFromFile(parameterFile string) (map[string]string, error)
return nil, xerrors.Errorf("Parameter file name is not specified")
}

// 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.
// Returns a parameter value from a given map, if the map does not exist or does not contain the item, it takes input from the user.
// Throws an error if there are any errors with the users input.
func getParameterValueFromMapOrInput(cmd *cobra.Command, parameterMap map[string]string, parameterSchema codersdk.ParameterSchema) (string, error) {
var parameterValue string
var err error
if parameterMap != nil {
var ok bool
parameterValue, ok = parameterMap[parameterSchema.Name]
if !ok {
return "", xerrors.Errorf("Parameter value absent in parameter file for %q!", parameterSchema.Name)
parameterValue, err = cliui.ParameterSchema(cmd, parameterSchema)
if err != nil {
return "", err
}
}
} else {
var err error
parameterValue, err = cliui.ParameterSchema(cmd, parameterSchema)
if err != nil {
return "", err
Expand Down
15 changes: 13 additions & 2 deletions cli/templatecreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,25 @@ func TestTemplateCreate(t *testing.T) {
match string
write string
}{
{match: "Create and upload", write: "yes"},
{
match: "Create and upload",
write: "yes",
},
{
match: "Enter a value:",
write: "bingo",
},
{
match: "Confirm create?",
write: "yes",
},
}
for _, m := range matches {
pty.ExpectMatch(m.match)
pty.WriteLine(m.write)
}

require.EqualError(t, <-execDone, "Parameter value absent in parameter file for \"region\"!")
require.NoError(t, <-execDone)
})

t.Run("Recreate template with same name (create, delete, create)", func(t *testing.T) {
Expand Down