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

Raises warning when pseudo resources are ignored because of named hcl resources. Closes #1328. Closes #1480 #3869

Merged
merged 1 commit into from
Sep 19, 2023
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
11 changes: 7 additions & 4 deletions pkg/steampipeconfig/parse/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,14 @@ func ParseMod(fileData map[string][]byte, pseudoResources []modconfig.MappableRe
}
}

// collect warnings as we parse
var res = &error_helpers.ErrorAndWarnings{}

// add pseudo resources to the mod
addPseudoResourcesToMod(pseudoResources, hclResources, mod)
errorsAndWarnings := addPseudoResourcesToMod(pseudoResources, hclResources, mod)

// merge the warnings generated while adding pseudoresources
res.Merge(errorsAndWarnings)

// add the parsed content to the run context
parseCtx.SetDecodeContent(content, fileData)
Expand All @@ -145,9 +151,6 @@ func ParseMod(fileData map[string][]byte, pseudoResources []modconfig.MappableRe
return nil, error_helpers.NewErrorsAndWarning(plugin.DiagsToError("Failed to add mod to run context", diags))
}

// collect warnings as we parse
var res = &error_helpers.ErrorAndWarnings{}

// we may need to decode more than once as we gather dependencies as we go
// continue decoding as long as the number of unresolved blocks decreases
prevUnresolvedBlocks := 0
Expand Down
14 changes: 6 additions & 8 deletions pkg/steampipeconfig/parse/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/hashicorp/hcl/v2/json"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe/pkg/constants"
"github.com/turbot/steampipe/pkg/error_helpers"
"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
"github.com/turbot/steampipe/pkg/utils"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -127,24 +127,22 @@ func parseYamlFile(filename string) (*hcl.File, hcl.Diagnostics) {
return json.Parse(jsonData, filename)
}

func addPseudoResourcesToMod(pseudoResources []modconfig.MappableResource, hclResources map[string]bool, mod *modconfig.Mod) {
var duplicates []string
func addPseudoResourcesToMod(pseudoResources []modconfig.MappableResource, hclResources map[string]bool, mod *modconfig.Mod) *error_helpers.ErrorAndWarnings {
res := error_helpers.EmptyErrorsAndWarning()
for _, r := range pseudoResources {
// is there a hcl resource with the same name as this pseudo resource - it takes precedence
name := r.GetUnqualifiedName()
if _, ok := hclResources[name]; ok {
duplicates = append(duplicates, r.GetDeclRange().Filename)
res.AddWarning(fmt.Sprintf("%s ignored as hcl resources of same name is already defined", r.GetDeclRange().Filename))
log.Printf("[TRACE] %s ignored as hcl resources of same name is already defined", r.GetDeclRange().Filename)
continue
}
// add pseudo resource to mod
mod.AddResource(r.(modconfig.HclResource))
// add to map of existing resources
hclResources[name] = true
}
numDupes := len(duplicates)
if numDupes > 0 {
log.Printf("[TRACE] %d %s not converted into resources as hcl resources of same name are defined: %v", numDupes, utils.Pluralize("file", numDupes), duplicates)
}
return res
}

// get names of all resources defined in hcl which may also be created as pseudo resources
Expand Down
Loading