Skip to content

Commit

Permalink
Merge #13233
Browse files Browse the repository at this point in the history
13233: Don't save empty config values to the config file in new r=Frassle a=Frassle


<!--- 
Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation.
-->

# Description

<!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. -->

Fixes #4081

Also plumbs the prompt function down to `promptForConfig` so this can be tested.


## Checklist

- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
  - [ ] I have formatted my code using `gofumpt`

<!--- Please provide details if the checkbox below is to be left unchecked. -->
- [x] I have added tests that prove my fix is effective or that my feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud,
then the service should honor older versions of the CLI where this change would not exist.
You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version
  <!-- `@Pulumi` employees: If yes, you must submit corresponding changes in the service repo. -->


Co-authored-by: Fraser Waters <fraser@pulumi.com>
  • Loading branch information
bors[bot] and Frassle committed Jun 22, 2023
2 parents 3d59a8f + 042f271 commit e8b7a9b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: cli/new
description: Fix empty config values being added to the config file as part of `new`.
17 changes: 13 additions & 4 deletions pkg/cmd/pulumi/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ func runNew(ctx context.Context, args newArgs) error {

// Prompt for config values (if needed) and save.
if !args.generateOnly {
err = handleConfig(ctx, proj, s, args.templateNameOrURL, template, args.configArray, args.yes, args.configPath, opts)
err = handleConfig(
ctx, args.prompt, proj, s,
args.templateNameOrURL, template, args.configArray,
args.yes, args.configPath, opts)
if err != nil {
return err
}
Expand Down Expand Up @@ -848,6 +851,7 @@ func parseConfig(configArray []string, path bool) (config.Map, error) {
// value when prompting instead of the default value specified in templateConfig.
func promptForConfig(
ctx context.Context,
prompt promptForValueFunc,
project *workspace.Project,
stack backend.Stack,
templateConfig map[string]workspace.ProjectTemplateConfigValue,
Expand Down Expand Up @@ -932,17 +936,22 @@ func promptForConfig(
}

// Prepare the prompt.
prompt := prettyKey(k)
promptText := prettyKey(k)
if templateConfigValue.Description != "" {
prompt = prompt + ": " + templateConfigValue.Description
promptText = promptText + ": " + templateConfigValue.Description
}

// Prompt.
value, err := promptForValue(yes, prompt, defaultValue, secret, nil, opts)
value, err := prompt(yes, promptText, defaultValue, secret, nil, opts)
if err != nil {
return nil, err
}

if value == "" {
// Don't add empty values to the config.
continue
}

// Encrypt the value if needed.
var v config.Value
if secret {
Expand Down
42 changes: 42 additions & 0 deletions pkg/cmd/pulumi/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/pulumi/pulumi/pkg/v3/backend"
"github.com/pulumi/pulumi/pkg/v3/backend/display"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/config"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -296,6 +299,45 @@ func TestGeneratingProjectWithExistingPromptedNameSucceeds(t *testing.T) {
assert.Equal(t, projectName, proj.Name.String())
}

//nolint:paralleltest // changes directory for process
func TestCreatingProjectWithEmptyConfig(t *testing.T) {
// Regression test for https://github.com/pulumi/pulumi/issues/4081
skipIfShortOrNoPulumiAccessToken(t)

tempdir := t.TempDir()
chdir(t, tempdir)
uniqueProjectName := filepath.Base(tempdir) + "test"

prompt := func(yes bool, valueType string, defaultValue string, secret bool,
isValidFn func(value string) error, opts display.Options,
) (string, error) {
if strings.HasPrefix(valueType, "aws:region:") {
return "", nil
}
return defaultValue, nil
}

args := newArgs{
name: uniqueProjectName,
stack: stackName,
interactive: true,
prompt: prompt,
secretsProvider: "default",
templateNameOrURL: "aws-typescript",
}

err := runNew(context.Background(), args)
require.NoError(t, err)

proj := loadProject(t, tempdir)
projStack, err := workspace.LoadProjectStack(proj, filepath.Join(tempdir, "Pulumi."+stackName+".yaml"))
require.NoError(t, err)

assert.NotContains(t, projStack.Config, config.MustMakeKey("aws", "region"))

removeStack(t, tempdir, stackName)
}

//nolint:paralleltest // changes directory for process
func TestGeneratingProjectWithInvalidArgsSpecifiedNameFails(t *testing.T) {
skipIfShortOrNoPulumiAccessToken(t)
Expand Down
8 changes: 6 additions & 2 deletions pkg/cmd/pulumi/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,10 @@ func newUpCmd() *cobra.Command {
}

// Prompt for config values (if needed) and save.
if err = handleConfig(ctx, proj, s, templateNameOrURL, template, configArray, yes, path, opts.Display); err != nil {
if err = handleConfig(
ctx, promptForValue, proj, s,
templateNameOrURL, template, configArray,
yes, path, opts.Display); err != nil {
return result.FromError(err)
}

Expand Down Expand Up @@ -637,6 +640,7 @@ func validatePolicyPackConfig(policyPackPaths []string, policyPackConfigPaths []
// handleConfig handles prompting for config values (as needed) and saving config.
func handleConfig(
ctx context.Context,
prompt promptForValueFunc,
project *workspace.Project,
s backend.Stack,
templateNameOrURL string,
Expand Down Expand Up @@ -676,7 +680,7 @@ func handleConfig(
}

// Prompt for config as needed.
c, err = promptForConfig(ctx, project, s, template.Config, commandLineConfig, stackConfig, yes, opts)
c, err = promptForConfig(ctx, prompt, project, s, template.Config, commandLineConfig, stackConfig, yes, opts)
if err != nil {
return err
}
Expand Down

0 comments on commit e8b7a9b

Please sign in to comment.