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

stacks: allow dynamic provider configurations during validation #35109

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ func (c *ComponentConfig) CheckProviders(ctx context.Context, phase EvalPhase) (
}

const errSummary = "Invalid provider configuration"
if actualTy := result.Value.Type(); stackconfigtypes.IsProviderConfigType(actualTy) {
actualTy := result.Value.Type()
// Direct type match
if stackconfigtypes.IsProviderConfigType(actualTy) {
// Then we at least got a provider reference of some kind.
actualTypeAddr := stackconfigtypes.ProviderForProviderConfigType(actualTy)
if actualTypeAddr != typeAddr {
Expand All @@ -336,6 +338,10 @@ func (c *ComponentConfig) CheckProviders(ctx context.Context, phase EvalPhase) (
})
continue
}
} else if actualTy.Equals(cty.DynamicPseudoType) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of a tricky edge in cty that gets us into trouble sometimes. I'm not sure if this is an example of that trouble, but I'm going to try to describe the trouble and hopefully you can decide if it matters. 😀

The null keyword in HCL represents what cty would call cty.NullVal(cty.DynamicPseudotType) -- a null value of an unknown type. That would cause this expression to return true, even though I think we're not intending to accept null as a provider configuration reference here:

  providers = {
    aws = null
  }

To deal with that we've typically inserted an extra check like if result.Value.IsNull() before doing any type checking, so that we can deal with that error case first and just treat nulls of any type as equally invalid.

All of that said then: I don't think it makes sense to allow null here because any provider configuration slot a module declares is always required. If that's true then I'd suggest adding an extra check up at the top like I described above. If there is some reason to allow null then this might be okay, though to make that case more visible in the code I might still suggest handling it as a separate if condition since it's a common mistake to forget about the possibility of nulls when maintaining existing code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I notice below there's a comment saying that component_instance.go is responsible for checking whether the value is null, so maybe just type checking here is sufficient after all. I suggest checking that comment is actually true, though!)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think I'm on the entirely wrong track here, I think your assessment that this is cty.NullVal(cty.DynamicPseudoType) or sth similar is right. I looked into the reference extraction and found that in the EvalScope the provider values are from my point of view correct, it's a map of provider configurations. I couldn't find where the property access is handled / should be handled, maybe this issue is too in the weeds for me right now 🙈

// This is a dynamic type, which means we don't know what it is. This can happen if the for_each
// contains unknown values, for example.
// We can't do any more validation here, so we'll just skip it.
} else {
// We got something that isn't a provider reference at all.
diags = diags.Append(&hcl.Diagnostic{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
required_providers {
testing = {
source = "hashicorp/testing"
version = "0.1.0"
}
}

variable "provider_set" {
type = set(string)
default = ["a", "b"]
}

provider "testing" "configurations" {
for_each = var.provider_set
}

variable "input" {
type = string
}

component "self" {
source = "../"
for_each = var.provider_set

providers = {
testing = provider.testing.configurations[each.value]
}

inputs = {
input = var.input
}
}
5 changes: 5 additions & 0 deletions internal/stacks/stackruntime/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ var (
"input": cty.StringVal("input"),
},
},
filepath.Join("with-single-input", "provider-for-each"): {
planInputVars: map[string]cty.Value{
"input": cty.StringVal("input"),
},
},
}

// invalidConfigurations are shared between the validate and plan tests.
Expand Down