2018-04-04 03:11:28 +02:00
|
|
|
package lang
|
|
|
|
|
|
|
|
import (
|
2019-03-19 01:15:23 +01:00
|
|
|
"github.com/hashicorp/hcl2/ext/dynblock"
|
2018-04-04 03:11:28 +02:00
|
|
|
"github.com/hashicorp/hcl2/hcl"
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-07-05 19:33:29 +02:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2018-04-04 03:11:28 +02:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
|
|
)
|
|
|
|
|
|
|
|
// References finds all of the references in the given set of traversals,
|
|
|
|
// returning diagnostics if any of the traversals cannot be interpreted as a
|
|
|
|
// reference.
|
|
|
|
//
|
|
|
|
// This function does not do any de-duplication of references, since references
|
|
|
|
// have source location information embedded in them and so any invalid
|
|
|
|
// references that are duplicated should have errors reported for each
|
|
|
|
// occurence.
|
|
|
|
//
|
|
|
|
// If the returned diagnostics contains errors then the result may be
|
|
|
|
// incomplete or invalid. Otherwise, the returned slice has one reference per
|
|
|
|
// given traversal, though it is not guaranteed that the references will
|
|
|
|
// appear in the same order as the given traversals.
|
|
|
|
func References(traversals []hcl.Traversal) ([]*addrs.Reference, tfdiags.Diagnostics) {
|
|
|
|
if len(traversals) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
refs := make([]*addrs.Reference, 0, len(traversals))
|
|
|
|
|
|
|
|
for _, traversal := range traversals {
|
|
|
|
ref, refDiags := addrs.ParseRef(traversal)
|
|
|
|
diags = diags.Append(refDiags)
|
|
|
|
if ref == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
refs = append(refs, ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
return refs, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReferencesInBlock is a helper wrapper around References that first searches
|
|
|
|
// the given body for traversals, before converting those traversals to
|
|
|
|
// references.
|
|
|
|
//
|
|
|
|
// A block schema must be provided so that this function can determine where in
|
|
|
|
// the body variables are expected.
|
|
|
|
func ReferencesInBlock(body hcl.Body, schema *configschema.Block) ([]*addrs.Reference, 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 19:33:53 +02:00
|
|
|
if body == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-04-04 03:11:28 +02:00
|
|
|
spec := schema.DecoderSpec()
|
2019-03-19 01:15:23 +01:00
|
|
|
|
|
|
|
// We use dynblock.VariablesHCLDec instead of hcldec.Variables here because
|
|
|
|
// when we evaluate a block we'll apply the HCL dynamic block extension
|
|
|
|
// expansion to it first, and so we need this specialized version in order
|
|
|
|
// to properly understand what the dependencies will be once expanded.
|
|
|
|
// Otherwise, we'd miss references that only occur inside dynamic blocks.
|
|
|
|
traversals := dynblock.VariablesHCLDec(body, spec)
|
2018-04-04 03:11:28 +02:00
|
|
|
return References(traversals)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReferencesInExpr is a helper wrapper around References that first searches
|
|
|
|
// the given expression for traversals, before converting those traversals
|
|
|
|
// to references.
|
|
|
|
func ReferencesInExpr(expr hcl.Expression) ([]*addrs.Reference, 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 19:33:53 +02:00
|
|
|
if expr == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-04-04 03:11:28 +02:00
|
|
|
traversals := expr.Variables()
|
|
|
|
return References(traversals)
|
|
|
|
}
|