Skip to content
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

Warn about potential errors when output interpolation expressions use a counted resource like a singleton #16735

Merged
merged 2 commits into from
Nov 28, 2017
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
47 changes: 26 additions & 21 deletions backend/local/backend_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package local

import (
"errors"
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform/command/format"

"github.com/hashicorp/terraform/tfdiags"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/state"
"github.com/hashicorp/terraform/terraform"
Expand Down Expand Up @@ -91,27 +92,31 @@ func (b *Local) context(op *backend.Operation) (*terraform.Context, state.State,

// If validation is enabled, validate
if b.OpValidation {
// We ignore warnings here on purpose. We expect users to be listening
// to the terraform.Hook called after a validation.
ws, es := tfCtx.Validate()
if len(ws) > 0 {
// Log just in case the CLI isn't enabled
log.Printf("[WARN] backend/local: %d warnings: %v", len(ws), ws)

// If we have a CLI, output the warnings
if b.CLI != nil {
b.CLI.Warn(strings.TrimSpace(validateWarnHeader) + "\n")
for _, w := range ws {
b.CLI.Warn(fmt.Sprintf(" * %s", w))
}
diags := tfCtx.Validate()
if len(diags) > 0 {
if diags.HasErrors() {
// If there are warnings _and_ errors then we'll take this
// path and return them all together in this error.
return nil, nil, diags.Err()
}

// Make a newline before continuing
b.CLI.Output("")
// For now we can't propagate warnings any further without
// printing them directly to the UI, so we'll need to
// format them here ourselves.
for _, diag := range diags {
if diag.Severity() != tfdiags.Warning {
continue
}
if b.CLI != nil {
b.CLI.Warn(format.Diagnostic(diag, b.Colorize(), 72))
} else {
desc := diag.Description()
log.Printf("[WARN] backend/local: %s", desc.Summary)
}
}
}

if len(es) > 0 {
return nil, nil, multierror.Append(nil, es...)
// Make a newline before continuing
b.CLI.Output("")
}
}
}
Expand Down
36 changes: 7 additions & 29 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"runtime"

"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
)

// Set to true when we're testing
Expand Down Expand Up @@ -82,39 +81,18 @@ func ModulePath(args []string) (string, error) {
return args[0], nil
}

func validateContext(ctx *terraform.Context, ui cli.Ui) bool {
func (m *Meta) validateContext(ctx *terraform.Context) bool {
log.Println("[INFO] Validating the context...")
ws, es := ctx.Validate()
log.Printf("[INFO] Validation result: %d warnings, %d errors", len(ws), len(es))
diags := ctx.Validate()
log.Printf("[INFO] Validation result: %d diagnostics", len(diags))

if len(ws) > 0 || len(es) > 0 {
ui.Output(
if len(diags) > 0 {
m.Ui.Output(
"There are warnings and/or errors related to your configuration. Please\n" +
"fix these before continuing.\n")

if len(ws) > 0 {
ui.Warn("Warnings:\n")
for _, w := range ws {
ui.Warn(fmt.Sprintf(" * %s", w))
}

if len(es) > 0 {
ui.Output("")
}
}

if len(es) > 0 {
ui.Error("Errors:\n")
for _, e := range es {
ui.Error(fmt.Sprintf(" * %s", e))
}
return false
} else {
ui.Warn(fmt.Sprintf("\n"+
"No errors found. Continuing with %d warning(s).\n", len(ws)))
return true
}
m.showDiagnostics(diags)
}

return true
return !diags.HasErrors()
}
3 changes: 2 additions & 1 deletion command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ func (c *InitCommand) getProviders(path string, state *terraform.State, upgrade
return err
}

if err := mod.Validate(); err != nil {
if diags := mod.Validate(); diags.HasErrors() {
err := diags.Err()
c.Ui.Error(fmt.Sprintf("Error getting plugins: %s", err))
return err
}
Expand Down
9 changes: 5 additions & 4 deletions command/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ func (c *ProvidersCommand) Run(args []string) int {
}

// Validate the config (to ensure the version constraints are valid)
err = root.Validate()
if err != nil {
c.Ui.Error(err.Error())
return 1
if diags := root.Validate(); len(diags) != 0 {
c.showDiagnostics(diags)
if diags.HasErrors() {
return 1
}
}

// Load the backend
Expand Down
11 changes: 6 additions & 5 deletions command/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ func (c *ValidateCommand) validate(dir string, checkVars bool) int {
c.showDiagnostics(err)
return 1
}
err = cfg.Validate()
if err != nil {
c.showDiagnostics(err)
return 1
if diags := cfg.Validate(); len(diags) != 0 {
c.showDiagnostics(diags)
if diags.HasErrors() {
return 1
}
}

if checkVars {
Expand All @@ -122,7 +123,7 @@ func (c *ValidateCommand) validate(dir string, checkVars bool) int {
return 1
}

if !validateContext(tfCtx, c.Ui) {
if !c.validateContext(tfCtx) {
return 1
}
}
Expand Down
12 changes: 6 additions & 6 deletions command/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestValidateFailingCommandMissingVariable(t *testing.T) {
if code != 1 {
t.Fatalf("Should have failed: %d\n\n%s", code, ui.ErrorWriter.String())
}
if !strings.HasSuffix(strings.TrimSpace(ui.ErrorWriter.String()), "config: unknown variable referenced: 'description'. define it with 'variable' blocks") {
if !strings.HasSuffix(strings.TrimSpace(ui.ErrorWriter.String()), "config: unknown variable referenced: 'description'; define it with a 'variable' block") {
t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
}
}
Expand All @@ -84,7 +84,7 @@ func TestSameProviderMutipleTimesShouldFail(t *testing.T) {
if code != 1 {
t.Fatalf("Should have failed: %d\n\n%s", code, ui.ErrorWriter.String())
}
if !strings.HasSuffix(strings.TrimSpace(ui.ErrorWriter.String()), "provider.aws: declared multiple times, you can only declare a provider once") {
if !strings.HasSuffix(strings.TrimSpace(ui.ErrorWriter.String()), "provider.aws: multiple configurations present; only one configuration is allowed per provider") {
t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
}
}
Expand All @@ -94,7 +94,7 @@ func TestSameModuleMultipleTimesShouldFail(t *testing.T) {
if code != 1 {
t.Fatalf("Should have failed: %d\n\n%s", code, ui.ErrorWriter.String())
}
if !strings.HasSuffix(strings.TrimSpace(ui.ErrorWriter.String()), "multi_module: module repeated multiple times") {
if !strings.HasSuffix(strings.TrimSpace(ui.ErrorWriter.String()), "module \"multi_module\": module repeated multiple times") {
t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
}
}
Expand All @@ -114,7 +114,7 @@ func TestOutputWithoutValueShouldFail(t *testing.T) {
if code != 1 {
t.Fatalf("Should have failed: %d\n\n%s", code, ui.ErrorWriter.String())
}
if !strings.HasSuffix(strings.TrimSpace(ui.ErrorWriter.String()), "output is missing required 'value' key") {
if !strings.HasSuffix(strings.TrimSpace(ui.ErrorWriter.String()), "output \"myvalue\": missing required 'value' argument") {
t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
}
}
Expand All @@ -125,7 +125,7 @@ func TestModuleWithIncorrectNameShouldFail(t *testing.T) {
t.Fatalf("Should have failed: %d\n\n%s", code, ui.ErrorWriter.String())
}

if !strings.Contains(ui.ErrorWriter.String(), "module name can only contain letters, numbers, dashes, and underscores") {
if !strings.Contains(ui.ErrorWriter.String(), "module name must be a letter or underscore followed by only letters, numbers, dashes, and underscores") {
t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
}
if !strings.Contains(ui.ErrorWriter.String(), "module source cannot contain interpolations") {
Expand All @@ -142,7 +142,7 @@ func TestWronglyUsedInterpolationShouldFail(t *testing.T) {
if !strings.Contains(ui.ErrorWriter.String(), "depends on value cannot contain interpolations") {
t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
}
if !strings.Contains(ui.ErrorWriter.String(), "Variable 'vairable_with_interpolation': cannot contain interpolations") {
if !strings.Contains(ui.ErrorWriter.String(), "variable \"vairable_with_interpolation\": default may not contain interpolations") {
t.Fatalf("Should have failed: %d\n\n'%s'", code, ui.ErrorWriter.String())
}
}
Expand Down
Loading