2024-02-08 03:48:59 -06:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 10:33:06 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-09-20 07:16:53 -05:00
|
|
|
package tofu
|
2017-01-25 23:00:45 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-09-09 17:58:44 -05:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2018-05-11 18:17:06 -05:00
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/addrs"
|
|
|
|
"github.com/opentofu/opentofu/internal/configs/configschema"
|
|
|
|
"github.com/opentofu/opentofu/internal/lang"
|
|
|
|
"github.com/opentofu/opentofu/internal/providers"
|
|
|
|
"github.com/opentofu/opentofu/internal/tfdiags"
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
)
|
2017-01-25 23:00:45 -06:00
|
|
|
|
2020-12-08 09:52:39 -06:00
|
|
|
// validateSelfRef checks to ensure that expressions within a particular
|
2024-08-29 12:20:33 -05:00
|
|
|
// referenceable block do not reference that same block.
|
2023-07-06 09:35:33 -05:00
|
|
|
func validateSelfRef(addr addrs.Referenceable, config hcl.Body, providerSchema providers.ProviderSchema) tfdiags.Diagnostics {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
2018-05-10 19:56:19 -05:00
|
|
|
addrStrs := make([]string, 0, 1)
|
|
|
|
addrStrs = append(addrStrs, addr.String())
|
|
|
|
switch tAddr := addr.(type) {
|
|
|
|
case addrs.ResourceInstance:
|
|
|
|
// A resource instance may not refer to its containing resource either.
|
|
|
|
addrStrs = append(addrStrs, tAddr.ContainingResource().String())
|
|
|
|
}
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
|
2018-05-11 18:17:06 -05:00
|
|
|
var schema *configschema.Block
|
|
|
|
switch tAddr := addr.(type) {
|
|
|
|
case addrs.Resource:
|
2018-11-27 17:30:18 -06:00
|
|
|
schema, _ = providerSchema.SchemaForResourceAddr(tAddr)
|
2018-05-11 18:17:06 -05:00
|
|
|
case addrs.ResourceInstance:
|
2018-11-27 17:30:18 -06:00
|
|
|
schema, _ = providerSchema.SchemaForResourceAddr(tAddr.ContainingResource())
|
2018-05-11 18:17:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if schema == nil {
|
2023-09-21 07:38:46 -05:00
|
|
|
diags = diags.Append(fmt.Errorf("no schema available for %s to validate for self-references; this is a bug in OpenTofu and should be reported", addr))
|
2020-10-28 11:32:49 -05:00
|
|
|
return diags
|
2018-05-11 18:17:06 -05:00
|
|
|
}
|
|
|
|
|
2023-06-28 02:47:24 -05:00
|
|
|
refs, _ := lang.ReferencesInBlock(addrs.ParseRef, config, schema)
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
for _, ref := range refs {
|
2018-05-10 19:56:19 -05:00
|
|
|
for _, addrStr := range addrStrs {
|
|
|
|
if ref.Subject.String() == addrStr {
|
|
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Self-referential block",
|
|
|
|
Detail: fmt.Sprintf("Configuration for %s may not refer to itself.", addrStr),
|
|
|
|
Subject: ref.SourceRange.ToHCL().Ptr(),
|
|
|
|
})
|
|
|
|
}
|
2017-01-25 23:00:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 11:32:49 -05:00
|
|
|
return diags
|
2017-01-25 23:00:45 -06:00
|
|
|
}
|
2023-06-20 10:25:31 -05:00
|
|
|
|
|
|
|
// Legacy provisioner configurations may refer to single instances using the
|
|
|
|
// resource address. We need to filter these out from the reported references
|
|
|
|
// to prevent cycles.
|
|
|
|
func filterSelfRefs(self addrs.Resource, refs []*addrs.Reference) []*addrs.Reference {
|
|
|
|
for i := 0; i < len(refs); i++ {
|
|
|
|
ref := refs[i]
|
|
|
|
|
|
|
|
var subject addrs.Resource
|
|
|
|
switch subj := ref.Subject.(type) {
|
|
|
|
case addrs.Resource:
|
|
|
|
subject = subj
|
|
|
|
case addrs.ResourceInstance:
|
|
|
|
subject = subj.ContainingResource()
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.Equal(subject) {
|
|
|
|
tail := len(refs) - 1
|
|
|
|
|
|
|
|
refs[i], refs[tail] = refs[tail], refs[i]
|
|
|
|
refs = refs[:tail]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return refs
|
|
|
|
}
|