Skip to content

Commit

Permalink
#300: allow square brackets syntax and pass aliases map to graph (#1892)
Browse files Browse the repository at this point in the history
Signed-off-by: ollevche <[email protected]>
Co-authored-by: Christian Mesh <[email protected]>
Signed-off-by: Christian Mesh <[email protected]>
  • Loading branch information
ollevche and cam72cam committed Aug 22, 2024
1 parent 709054b commit 7ad64a0
Show file tree
Hide file tree
Showing 5 changed files with 318 additions and 26 deletions.
3 changes: 1 addition & 2 deletions internal/configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,7 @@ func (c *Config) addProviderRequirements(reqs getproviders.Requirements, recurse
})
continue
}
// TODO Ronny: Fix to run this validation with multiple providers
if i.ProviderConfigRef.Name != target.ProviderConfigRef.Name || i.ProviderConfigRef.Alias != target.ProviderConfigRef.Aliases[addrs.NoKey] {
if i.ProviderConfigRef.Name != target.ProviderConfigRef.Name || i.ProviderConfigRef.Alias != target.ProviderConfigRef.GetNoKeyAlias() {
// This means we have a provider specified in both the
// import block and the resource block, and they disagree.
// This is bad as OpenTofu now has different instructions
Expand Down
10 changes: 10 additions & 0 deletions internal/configs/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ func NewModule(primaryFiles, overrideFiles []*File, call StaticModuleCall, sourc
diags = append(diags, mDiags...)
}

// Process all resources now that we have the static context
for _, res := range mod.ManagedResources {
mDiags := res.decodeStaticFields(mod.StaticEvaluator)
diags = append(diags, mDiags...)
}
for _, res := range mod.DataResources {
mDiags := res.decodeStaticFields(mod.StaticEvaluator)
diags = append(diags, mDiags...)
}

diags = append(diags, checkModuleExperiments(mod)...)

// Generate the FQN -> LocalProviderName map
Expand Down
31 changes: 27 additions & 4 deletions internal/configs/module_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ func (mc *ModuleCall) decodeStaticFields(eval *StaticEvaluator) hcl.Diagnostics
var diags hcl.Diagnostics
diags = diags.Extend(mc.decodeStaticSource(eval))
diags = diags.Extend(mc.decodeStaticVersion(eval))
diags = diags.Extend(mc.decodeStaticProviderAliases(eval))

return diags
}

func (mc *ModuleCall) decodeStaticProviderAliases(eval *StaticEvaluator) hcl.Diagnostics {
var diags hcl.Diagnostics

for _, p := range mc.Providers {
diags = diags.Extend(p.InParentMapping.decodeStaticAlias(eval, mc.ForEach))
}

return diags
}

Expand Down Expand Up @@ -297,10 +309,21 @@ type PassedProviderConfig struct {

// TODO/Oleksandr: get rid of this function and make a proper call via InParent
func (c *PassedProviderConfig) InParentTODO() *ProviderConfigRef {
return c.InParent(addrs.NoKey)
if len(c.InParentMapping.Aliases) == 0 {
return c.InParent(addrs.NoKey)
}

if _, ok := c.InParentMapping.Aliases[addrs.NoKey]; ok {
return c.InParent(addrs.NoKey)
}

for k := range c.InParentMapping.Aliases {
return c.InParent(k)
}

return nil
}

// TODO/Oleksandr: check InParent calls to ensure it expects functions instead of a field
func (c *PassedProviderConfig) InParent(k addrs.InstanceKey) *ProviderConfigRef {
if c.InParentMapping == nil {
return nil
Expand Down Expand Up @@ -333,7 +356,7 @@ func decodePassedProviderConfigs(attr *hcl.Attribute) ([]PassedProviderConfig, h
for _, pair := range pairs {
key, keyDiags := decodeProviderConfigRef(pair.Key, "providers")
diags = append(diags, keyDiags...)
value, valueDiags := decodeProviderConfigRef(pair.Value, "providers")
value, valueDiags := decodeProviderConfigRefMapping(pair.Value, "providers")
diags = append(diags, valueDiags...)
if keyDiags.HasErrors() || valueDiags.HasErrors() {
continue
Expand All @@ -355,7 +378,7 @@ func decodePassedProviderConfigs(attr *hcl.Attribute) ([]PassedProviderConfig, h

providers = append(providers, PassedProviderConfig{
InChild: key,
InParentMapping: NewProviderConfigMappingFromRef(value),
InParentMapping: value,
})
}
return providers, diags
Expand Down
2 changes: 1 addition & 1 deletion internal/configs/provider_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ func validateProviderConfigs(parentCall *ModuleCall, cfg *Config, noProviderConf
Summary: errSummary,
Detail: fmt.Sprintf(
"The local name %q in %s represents provider %q, but %q in %s represents %q.\n\nEach provider has its own distinct configuration schema and provider types, so this module's %q can be assigned only a configuration for %s, which is not required by %s.",
passed.InParent, parentModuleText, parentAddr.Provider.ForDisplay(),
passed.InParentTODO(), parentModuleText, parentAddr.Provider.ForDisplay(),
passed.InChild, moduleText, providerAddr.Provider.ForDisplay(),
passed.InChild, providerAddr.Provider.ForDisplay(),
moduleText,
Expand Down
Loading

0 comments on commit 7ad64a0

Please sign in to comment.