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

feat: improve error message for invalid extra atlantis dependencies #367

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions cmd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,28 @@ func TestExtraDeclaredDependencies(t *testing.T) {
})
}

func TestNonStringErrorOnExtraDeclaredDependencies(t *testing.T) {
err := resetForRun()
if err != nil {
t.Error("Failed to reset default flags")
return
}

rootCmd.SetArgs([]string{
"generate",
"--root",
filepath.Join("..", "test_examples_errors", "extra_dependency_error"),
})
err = rootCmd.Execute()

expectedError := "extra_atlantis_dependencies contains non-string value at position 4"
if err == nil || err.Error() != expectedError {
t.Errorf("Expected error '%s', got '%v'", expectedError, err)
return
}
return
}

func TestLocalTerraformModuleSource(t *testing.T) {
runTest(t, filepath.Join("golden", "local_terraform_module.yaml"), []string{
"--root",
Expand Down
22 changes: 15 additions & 7 deletions cmd/parse_locals.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/zclconf/go-cty/cty"


"fmt"
"path/filepath"
)

Expand Down Expand Up @@ -126,17 +127,19 @@ func parseLocals(path string, terragruntOptions *options.TerragruntOptions, incl
mergedParentLocals = mergeResolvedLocals(mergedParentLocals, parentLocals)
}
}
childLocals := resolveLocals(*localsAsCty)

childLocals, err := resolveLocals(*localsAsCty)
if err != nil {
return ResolvedLocals{}, err
}
return mergeResolvedLocals(mergedParentLocals, childLocals), nil
}

func resolveLocals(localsAsCty cty.Value) ResolvedLocals {
func resolveLocals(localsAsCty cty.Value) (ResolvedLocals,error) {
resolved := ResolvedLocals{}

// Return an empty set of locals if no `locals` block was present
if localsAsCty == cty.NilVal {
return resolved
return resolved, nil
}
rawLocals := localsAsCty.AsValueMap()

Expand Down Expand Up @@ -182,13 +185,18 @@ func resolveLocals(localsAsCty cty.Value) ResolvedLocals {
if ok {
it := extraDependenciesAsCty.ElementIterator()
for it.Next() {
_, val := it.Element()
pos, val := it.Element()
if !val.Type().Equals(cty.String) {
posInt, _ := pos.AsBigFloat().Int64()
return resolved, fmt.Errorf("extra_atlantis_dependencies contains non-string value at position %d", posInt)
}

resolved.ExtraAtlantisDependencies = append(
resolved.ExtraAtlantisDependencies,
filepath.ToSlash(val.AsString()),
)
}
}

return resolved
return resolved, nil
}
15 changes: 15 additions & 0 deletions test_examples_errors/extra_dependency_error/child/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
terraform {
source = "git::git@github.com:transcend-io/terraform-aws-fargate-container?ref=v0.0.4"
}

locals {
tg_config = read_terragrunt_config(find_in_parent_folders("config.hcl"))

extra_atlantis_dependencies = [
"some_extra_dep0",
"some_extra_dep1",
"some_extra_dep2",
"some_extra_dep3",
local.tg_config
]
}
3 changes: 3 additions & 0 deletions test_examples_errors/extra_dependency_error/config.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
locals {
aws_account_id = "111131111219"
}
Loading