Skip to content

feat: Update CLI to handle managed variables #6220

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 63 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
58fc371
WIP
mtojek Feb 9, 2023
84f6fe5
hcl
mtojek Feb 9, 2023
b29ab11
useManagedVariables
mtojek Feb 9, 2023
2124c54
fix
mtojek Feb 9, 2023
9f89e17
Fix
mtojek Feb 9, 2023
fc583ae
Fix
mtojek Feb 9, 2023
86a1c1c
Merge branch 'main' into 5980-manage-temp-variables
mtojek Feb 9, 2023
183fdb7
fix
mtojek Feb 9, 2023
3e9dd7e
go:build
mtojek Feb 9, 2023
42f8d41
Fix
mtojek Feb 9, 2023
c194087
fix: bool flag
mtojek Feb 10, 2023
f84fbf6
Insert template variables
mtojek Feb 10, 2023
33e75d9
API
mtojek Feb 10, 2023
85bc72f
fix
mtojek Feb 10, 2023
99c5fde
Expose via API
mtojek Feb 10, 2023
e4ee7f9
More wiring
mtojek Feb 10, 2023
2a60174
CLI for testing purposes
mtojek Feb 10, 2023
ffb0b94
WIP
mtojek Feb 13, 2023
c0ba41b
Delete FIXME
mtojek Feb 13, 2023
9e23196
planVars
mtojek Feb 13, 2023
80fdf07
Merge branch 'main' into 5980-manage-temp-variables
mtojek Feb 13, 2023
3a510ea
WIP
mtojek Feb 13, 2023
72f76b1
WIP
mtojek Feb 13, 2023
78d7252
UserVariableValues
mtojek Feb 13, 2023
845dd92
no dry run
mtojek Feb 13, 2023
e021e52
Dry run
mtojek Feb 13, 2023
54dc685
Done FIXME
mtojek Feb 13, 2023
81490a2
Fix
mtojek Feb 14, 2023
6aa97ab
Fix: CLI
mtojek Feb 14, 2023
920bf7c
Merge branch 'main' into 5980-manage-temp-variables
mtojek Feb 14, 2023
56f08f9
Fix: migration
mtojek Feb 14, 2023
2c57c96
API tests
mtojek Feb 14, 2023
bc18624
Test info
mtojek Feb 14, 2023
ae6f072
Tests
mtojek Feb 14, 2023
f9b4349
More tests
mtojek Feb 14, 2023
af4adec
fix: lint
mtojek Feb 14, 2023
864052c
Merge branch 'main' into 5980-manage-temp-variables
mtojek Feb 14, 2023
4bac517
Fix: authz
mtojek Feb 14, 2023
930624a
Merge branch 'main' into 5980-manage-temp-variables
mtojek Feb 14, 2023
23226fa
Address PR comments
mtojek Feb 15, 2023
ca23476
Fix
mtojek Feb 15, 2023
953b9a7
Merge branch 'main' into 5980-manage-temp-variables
mtojek Feb 15, 2023
bb8b500
fix
mtojek Feb 15, 2023
78ac8f5
fix
mtojek Feb 15, 2023
3b54e52
CLI: create
mtojek Feb 15, 2023
a3cc673
unit tests: create templates with variables
mtojek Feb 15, 2023
0ff8cd5
Use last variables
mtojek Feb 15, 2023
a9a5262
Merge branch 'main' into 5980-cli
mtojek Feb 15, 2023
2c0d885
Fix
mtojek Feb 15, 2023
84a6048
Fix
mtojek Feb 15, 2023
c10a755
Fix
mtojek Feb 15, 2023
3662fae
Push tests
mtojek Feb 16, 2023
9d85768
fix: variable is required if Default is nil
mtojek Feb 16, 2023
3dd894a
WIP
mtojek Feb 16, 2023
6c10c98
Redact sensitive values
mtojek Feb 16, 2023
d107c83
Fixes
mtojek Feb 16, 2023
b466aab
Fixes
mtojek Feb 16, 2023
c82da7f
Fix: arg description
mtojek Feb 16, 2023
1803d53
Fix
mtojek Feb 16, 2023
f16103d
Variable param
mtojek Feb 16, 2023
8771ecb
Fix: gen
mtojek Feb 16, 2023
33a173a
Fix
mtojek Feb 16, 2023
c1076db
Fix: goldens
mtojek Feb 16, 2023
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
21 changes: 17 additions & 4 deletions cli/templatecreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func templateCreate() *cobra.Command {
provisioner string
provisionerTags []string
parameterFile string
variablesFile string
variables []string
defaultTTL time.Duration

uploadFlags templateUploadFlags
Expand Down Expand Up @@ -76,6 +78,8 @@ func templateCreate() *cobra.Command {
FileID: resp.ID,
ParameterFile: parameterFile,
ProvisionerTags: tags,
VariablesFile: variablesFile,
Variables: variables,
})
if err != nil {
return err
Expand Down Expand Up @@ -113,6 +117,8 @@ func templateCreate() *cobra.Command {
},
}
cmd.Flags().StringVarP(&parameterFile, "parameter-file", "", "", "Specify a file path with parameter values.")
cmd.Flags().StringVarP(&variablesFile, "variables-file", "", "", "Specify a file path with values for Terraform-managed variables.")
cmd.Flags().StringArrayVarP(&variables, "variable", "", []string{}, "Specify a set of values for Terraform-managed variables.")
cmd.Flags().StringArrayVarP(&provisionerTags, "provisioner-tag", "", []string{}, "Specify a set of tags to target provisioner daemons.")
cmd.Flags().DurationVarP(&defaultTTL, "default-ttl", "", 24*time.Hour, "Specify a default TTL for workspaces created from this template.")
uploadFlags.register(cmd.Flags())
Expand All @@ -133,7 +139,10 @@ type createValidTemplateVersionArgs struct {
Provisioner database.ProvisionerType
FileID uuid.UUID
ParameterFile string
ValuesFile string

VariablesFile string
Variables []string

// Template is only required if updating a template's active version.
Template *codersdk.Template
// ReuseParameters will attempt to reuse params from the Template field
Expand All @@ -146,12 +155,16 @@ type createValidTemplateVersionArgs struct {
func createValidTemplateVersion(cmd *cobra.Command, args createValidTemplateVersionArgs, parameters ...codersdk.CreateParameterRequest) (*codersdk.TemplateVersion, []codersdk.CreateParameterRequest, error) {
client := args.Client

// FIXME(mtojek): I will iterate on CLI experience in the follow-up.
// see: https://github.com/coder/coder/issues/5980
variableValues, err := loadVariableValues(args.ValuesFile)
variableValues, err := loadVariableValuesFromFile(args.VariablesFile)
if err != nil {
return nil, nil, err
}

variableValuesFromKeyValues, err := loadVariableValuesFromOptions(args.Variables)
if err != nil {
return nil, nil, err
}
variableValues = append(variableValues, variableValuesFromKeyValues...)

req := codersdk.CreateTemplateVersionRequest{
Name: args.Name,
Expand Down
155 changes: 155 additions & 0 deletions cli/templatecreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,161 @@ func TestTemplateCreate(t *testing.T) {

require.EqualError(t, <-execDone, "Template name must be less than 32 characters")
})

t.Run("WithVariablesFileWithoutRequiredValue", func(t *testing.T) {
t.Parallel()

client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
coderdtest.CreateFirstUser(t, client)

templateVariables := []*proto.TemplateVariable{
{
Name: "first_variable",
Description: "This is the first variable",
Type: "string",
Required: true,
Sensitive: true,
},
{
Name: "second_variable",
Description: "This is the first variable",
Type: "string",
DefaultValue: "abc",
Required: false,
Sensitive: true,
},
}
source := clitest.CreateTemplateVersionSource(t,
createEchoResponsesWithTemplateVariables(templateVariables))
tempDir := t.TempDir()
removeTmpDirUntilSuccessAfterTest(t, tempDir)
variablesFile, _ := os.CreateTemp(tempDir, "variables*.yaml")
_, _ = variablesFile.WriteString(`second_variable: foobar`)
cmd, root := clitest.New(t, "templates", "create", "my-template", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--variables-file", variablesFile.Name())
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())

execDone := make(chan error)
go func() {
execDone <- cmd.Execute()
}()

matches := []struct {
match string
write string
}{
{match: "Upload", write: "yes"},
}
for _, m := range matches {
pty.ExpectMatch(m.match)
if len(m.write) > 0 {
pty.WriteLine(m.write)
}
}

require.Error(t, <-execDone)
})

t.Run("WithVariablesFileWithTheRequiredValue", func(t *testing.T) {
t.Parallel()

client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
coderdtest.CreateFirstUser(t, client)

templateVariables := []*proto.TemplateVariable{
{
Name: "first_variable",
Description: "This is the first variable",
Type: "string",
Required: true,
Sensitive: true,
},
{
Name: "second_variable",
Description: "This is the second variable",
Type: "string",
DefaultValue: "abc",
Required: false,
Sensitive: true,
},
}
source := clitest.CreateTemplateVersionSource(t,
createEchoResponsesWithTemplateVariables(templateVariables))
tempDir := t.TempDir()
removeTmpDirUntilSuccessAfterTest(t, tempDir)
variablesFile, _ := os.CreateTemp(tempDir, "variables*.yaml")
_, _ = variablesFile.WriteString(`first_variable: foobar`)
cmd, root := clitest.New(t, "templates", "create", "my-template", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--variables-file", variablesFile.Name())
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())

execDone := make(chan error)
go func() {
execDone <- cmd.Execute()
}()

matches := []struct {
match string
write string
}{
{match: "Upload", write: "yes"},
{match: "Confirm create?", write: "yes"},
}
for _, m := range matches {
pty.ExpectMatch(m.match)
if len(m.write) > 0 {
pty.WriteLine(m.write)
}
}

require.NoError(t, <-execDone)
})
t.Run("WithVariableOption", func(t *testing.T) {
t.Parallel()

client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
coderdtest.CreateFirstUser(t, client)

templateVariables := []*proto.TemplateVariable{
{
Name: "first_variable",
Description: "This is the first variable",
Type: "string",
Required: true,
Sensitive: true,
},
}
source := clitest.CreateTemplateVersionSource(t,
createEchoResponsesWithTemplateVariables(templateVariables))
cmd, root := clitest.New(t, "templates", "create", "my-template", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--variable", "first_variable=foobar")
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())

execDone := make(chan error)
go func() {
execDone <- cmd.Execute()
}()

matches := []struct {
match string
write string
}{
{match: "Upload", write: "yes"},
{match: "Confirm create?", write: "yes"},
}
for _, m := range matches {
pty.ExpectMatch(m.match)
pty.WriteLine(m.write)
}

require.NoError(t, <-execDone)
})
}

func createTestParseResponse() []*proto.Parse_Response {
Expand Down
9 changes: 6 additions & 3 deletions cli/templatepush.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func templatePush() *cobra.Command {
versionName string
provisioner string
parameterFile string
valuesFile string
variablesFile string
variables []string
alwaysPrompt bool
provisionerTags []string
uploadFlags templateUploadFlags
Expand Down Expand Up @@ -140,7 +141,8 @@ func templatePush() *cobra.Command {
Provisioner: database.ProvisionerType(provisioner),
FileID: resp.ID,
ParameterFile: parameterFile,
ValuesFile: valuesFile,
VariablesFile: variablesFile,
Variables: variables,
Template: &template,
ReuseParameters: !alwaysPrompt,
ProvisionerTags: tags,
Expand All @@ -167,7 +169,8 @@ func templatePush() *cobra.Command {

cmd.Flags().StringVarP(&provisioner, "test.provisioner", "", "terraform", "Customize the provisioner backend")
cmd.Flags().StringVarP(&parameterFile, "parameter-file", "", "", "Specify a file path with parameter values.")
cmd.Flags().StringVarP(&valuesFile, "values-file", "", "", "Specify a file path with values for managed variables.")
cmd.Flags().StringVarP(&variablesFile, "variables-file", "", "", "Specify a file path with values for Terraform-managed variables.")
cmd.Flags().StringArrayVarP(&variables, "variable", "", []string{}, "Specify a set of values for Terraform-managed variables.")
cmd.Flags().StringVarP(&versionName, "name", "", "", "Specify a name for the new template version. It will be automatically generated if not provided.")
cmd.Flags().StringArrayVarP(&provisionerTags, "provisioner-tag", "", []string{}, "Specify a set of tags to target provisioner daemons.")
cmd.Flags().BoolVar(&alwaysPrompt, "always-prompt", false, "Always prompt all parameters. Does not pull parameter values from active template version")
Expand Down
Loading