2018-02-02 05:33:06 +01:00
package configs
import (
2018-04-24 19:06:51 +02:00
"fmt"
2019-09-10 00:58:44 +02:00
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsyntax"
Refactoring of module source addresses and module installation
It's been a long while since we gave close attention to the codepaths for
module source address parsing and external module package installation.
Due to their age, these codepaths often diverged from our modern practices
such as representing address types in the addrs package, and encapsulating
package installation details only in a particular location.
In particular, this refactor makes source address parsing a separate step
from module installation, which therefore makes the result of that parsing
available to other Terraform subsystems which work with the configuration
representation objects.
This also presented the opportunity to better encapsulate our use of
go-getter into a new package "getmodules" (echoing "getproviders"), which
is intended to be the only part of Terraform that directly interacts with
go-getter.
This is largely just a refactor of the existing functionality into a new
code organization, but there is one notable change in behavior here: the
source address parsing now happens during configuration loading rather
than module installation, which may cause errors about invalid addresses
to be returned in different situations than before. That counts as
backward compatible because we only promise to remain compatible with
configurations that are _valid_, which means that they can be initialized,
planned, and applied without any errors. This doesn't introduce any new
error cases, and instead just makes a pre-existing error case be detected
earlier.
Our module registry client is still using its own special module address
type from registry/regsrc for now, with a small shim from the new
addrs.ModuleSourceRegistry type. Hopefully in a later commit we'll also
rework the registry client to work with the new address type, but this
commit is already big enough as it is.
2021-05-28 04:24:59 +02:00
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/getmodules"
2018-02-02 05:33:06 +01:00
)
// ModuleCall represents a "module" block in a module or file.
type ModuleCall struct {
2018-02-03 02:22:25 +01:00
Name string
Refactoring of module source addresses and module installation
It's been a long while since we gave close attention to the codepaths for
module source address parsing and external module package installation.
Due to their age, these codepaths often diverged from our modern practices
such as representing address types in the addrs package, and encapsulating
package installation details only in a particular location.
In particular, this refactor makes source address parsing a separate step
from module installation, which therefore makes the result of that parsing
available to other Terraform subsystems which work with the configuration
representation objects.
This also presented the opportunity to better encapsulate our use of
go-getter into a new package "getmodules" (echoing "getproviders"), which
is intended to be the only part of Terraform that directly interacts with
go-getter.
This is largely just a refactor of the existing functionality into a new
code organization, but there is one notable change in behavior here: the
source address parsing now happens during configuration loading rather
than module installation, which may cause errors about invalid addresses
to be returned in different situations than before. That counts as
backward compatible because we only promise to remain compatible with
configurations that are _valid_, which means that they can be initialized,
planned, and applied without any errors. This doesn't introduce any new
error cases, and instead just makes a pre-existing error case be detected
earlier.
Our module registry client is still using its own special module address
type from registry/regsrc for now, with a small shim from the new
addrs.ModuleSourceRegistry type. Hopefully in a later commit we'll also
rework the registry client to work with the new address type, but this
commit is already big enough as it is.
2021-05-28 04:24:59 +02:00
SourceAddr addrs . ModuleSource
SourceAddrRaw string
2018-02-03 02:22:25 +01:00
SourceAddrRange hcl . Range
2018-02-07 03:05:14 +01:00
SourceSet bool
2018-02-03 02:22:25 +01:00
Config hcl . Body
2018-02-02 05:33:06 +01:00
Version VersionConstraint
Count hcl . Expression
ForEach hcl . Expression
2018-04-24 19:06:51 +02:00
Providers [ ] PassedProviderConfig
2018-02-03 02:22:25 +01:00
DependsOn [ ] hcl . Traversal
2018-02-02 05:33:06 +01:00
DeclRange hcl . Range
}
2018-02-03 02:22:25 +01:00
2018-02-07 03:05:14 +01:00
func decodeModuleBlock ( block * hcl . Block , override bool ) ( * ModuleCall , hcl . Diagnostics ) {
2020-07-02 16:32:57 +02:00
var diags hcl . Diagnostics
2018-02-03 02:22:25 +01:00
mc := & ModuleCall {
Name : block . Labels [ 0 ] ,
DeclRange : block . DefRange ,
}
2018-02-07 03:05:14 +01:00
schema := moduleBlockSchema
if override {
schema = schemaForOverrides ( schema )
}
2020-07-02 16:32:57 +02:00
content , remain , moreDiags := block . Body . PartialContent ( schema )
diags = append ( diags , moreDiags ... )
2018-02-03 02:22:25 +01:00
mc . Config = remain
if ! hclsyntax . ValidIdentifier ( mc . Name ) {
diags = append ( diags , & hcl . Diagnostic {
Severity : hcl . DiagError ,
Summary : "Invalid module instance name" ,
Detail : badIdentifierDetail ,
Subject : & block . LabelRanges [ 0 ] ,
} )
}
if attr , exists := content . Attributes [ "source" ] ; exists {
2021-06-01 21:30:56 +02:00
mc . SourceSet = true
mc . SourceAddrRange = attr . Expr . Range ( )
Refactoring of module source addresses and module installation
It's been a long while since we gave close attention to the codepaths for
module source address parsing and external module package installation.
Due to their age, these codepaths often diverged from our modern practices
such as representing address types in the addrs package, and encapsulating
package installation details only in a particular location.
In particular, this refactor makes source address parsing a separate step
from module installation, which therefore makes the result of that parsing
available to other Terraform subsystems which work with the configuration
representation objects.
This also presented the opportunity to better encapsulate our use of
go-getter into a new package "getmodules" (echoing "getproviders"), which
is intended to be the only part of Terraform that directly interacts with
go-getter.
This is largely just a refactor of the existing functionality into a new
code organization, but there is one notable change in behavior here: the
source address parsing now happens during configuration loading rather
than module installation, which may cause errors about invalid addresses
to be returned in different situations than before. That counts as
backward compatible because we only promise to remain compatible with
configurations that are _valid_, which means that they can be initialized,
planned, and applied without any errors. This doesn't introduce any new
error cases, and instead just makes a pre-existing error case be detected
earlier.
Our module registry client is still using its own special module address
type from registry/regsrc for now, with a small shim from the new
addrs.ModuleSourceRegistry type. Hopefully in a later commit we'll also
rework the registry client to work with the new address type, but this
commit is already big enough as it is.
2021-05-28 04:24:59 +02:00
valDiags := gohcl . DecodeExpression ( attr . Expr , nil , & mc . SourceAddrRaw )
2018-02-03 02:22:25 +01:00
diags = append ( diags , valDiags ... )
2021-06-01 21:30:56 +02:00
if ! valDiags . HasErrors ( ) {
addr , err := addrs . ParseModuleSource ( mc . SourceAddrRaw )
mc . SourceAddr = addr
if err != nil {
// NOTE: In practice it's actually very unlikely to end up here,
// because our source address parser can turn just about any string
// into some sort of remote package address, and so for most errors
// we'll detect them only during module installation. There are
// still a _few_ purely-syntax errors we can catch at parsing time,
// though, mostly related to remote package sub-paths and local
// paths.
switch err := err . ( type ) {
case * getmodules . MaybeRelativePathErr :
diags = append ( diags , & hcl . Diagnostic {
Severity : hcl . DiagError ,
Summary : "Invalid module source address" ,
Detail : fmt . Sprintf (
"Terraform failed to determine your intended installation method for remote module package %q.\n\nIf you intended this as a path relative to the current module, use \"./%s\" instead. The \"./\" prefix indicates that the address is a relative filesystem path." ,
err . Addr , err . Addr ,
) ,
Subject : mc . SourceAddrRange . Ptr ( ) ,
} )
default :
diags = append ( diags , & hcl . Diagnostic {
Severity : hcl . DiagError ,
Summary : "Invalid module source address" ,
Detail : fmt . Sprintf ( "Failed to parse module source address: %s." , err ) ,
Subject : mc . SourceAddrRange . Ptr ( ) ,
} )
}
Refactoring of module source addresses and module installation
It's been a long while since we gave close attention to the codepaths for
module source address parsing and external module package installation.
Due to their age, these codepaths often diverged from our modern practices
such as representing address types in the addrs package, and encapsulating
package installation details only in a particular location.
In particular, this refactor makes source address parsing a separate step
from module installation, which therefore makes the result of that parsing
available to other Terraform subsystems which work with the configuration
representation objects.
This also presented the opportunity to better encapsulate our use of
go-getter into a new package "getmodules" (echoing "getproviders"), which
is intended to be the only part of Terraform that directly interacts with
go-getter.
This is largely just a refactor of the existing functionality into a new
code organization, but there is one notable change in behavior here: the
source address parsing now happens during configuration loading rather
than module installation, which may cause errors about invalid addresses
to be returned in different situations than before. That counts as
backward compatible because we only promise to remain compatible with
configurations that are _valid_, which means that they can be initialized,
planned, and applied without any errors. This doesn't introduce any new
error cases, and instead just makes a pre-existing error case be detected
earlier.
Our module registry client is still using its own special module address
type from registry/regsrc for now, with a small shim from the new
addrs.ModuleSourceRegistry type. Hopefully in a later commit we'll also
rework the registry client to work with the new address type, but this
commit is already big enough as it is.
2021-05-28 04:24:59 +02:00
}
}
2021-06-01 21:30:56 +02:00
// NOTE: We leave mc.SourceAddr as nil for any situation where the
// source attribute is invalid, so any code which tries to carefully
// use the partial result of a failed config decode must be
// resilient to that.
2018-02-03 02:22:25 +01:00
}
if attr , exists := content . Attributes [ "version" ] ; exists {
var versionDiags hcl . Diagnostics
mc . Version , versionDiags = decodeVersionConstraint ( attr )
diags = append ( diags , versionDiags ... )
}
if attr , exists := content . Attributes [ "count" ] ; exists {
mc . Count = attr . Expr
}
if attr , exists := content . Attributes [ "for_each" ] ; exists {
2020-04-07 20:18:08 +02:00
if mc . Count != nil {
diags = append ( diags , & hcl . Diagnostic {
Severity : hcl . DiagError ,
Summary : ` Invalid combination of "count" and "for_each" ` ,
Detail : ` The "count" and "for_each" meta-arguments are mutually-exclusive, only one should be used to be explicit about the number of resources to be created. ` ,
Subject : & attr . NameRange ,
} )
}
2018-02-03 02:22:25 +01:00
mc . ForEach = attr . Expr
}
if attr , exists := content . Attributes [ "depends_on" ] ; exists {
deps , depsDiags := decodeDependsOn ( attr )
diags = append ( diags , depsDiags ... )
mc . DependsOn = append ( mc . DependsOn , deps ... )
}
2018-04-24 19:06:51 +02:00
if attr , exists := content . Attributes [ "providers" ] ; exists {
seen := make ( map [ string ] hcl . Range )
pairs , pDiags := hcl . ExprMap ( attr . Expr )
diags = append ( diags , pDiags ... )
for _ , pair := range pairs {
key , keyDiags := decodeProviderConfigRef ( pair . Key , "providers" )
diags = append ( diags , keyDiags ... )
value , valueDiags := decodeProviderConfigRef ( pair . Value , "providers" )
diags = append ( diags , valueDiags ... )
if keyDiags . HasErrors ( ) || valueDiags . HasErrors ( ) {
continue
}
matchKey := key . String ( )
if prev , exists := seen [ matchKey ] ; exists {
diags = append ( diags , & hcl . Diagnostic {
Severity : hcl . DiagError ,
Summary : "Duplicate provider address" ,
Detail : fmt . Sprintf ( "A provider configuration was already passed to %s at %s. Each child provider configuration can be assigned only once." , matchKey , prev ) ,
Subject : pair . Value . Range ( ) . Ptr ( ) ,
} )
continue
}
rng := hcl . RangeBetween ( pair . Key . Range ( ) , pair . Value . Range ( ) )
seen [ matchKey ] = rng
mc . Providers = append ( mc . Providers , PassedProviderConfig {
InChild : key ,
InParent : value ,
} )
}
}
2021-05-15 01:09:51 +02:00
var seenEscapeBlock * hcl . Block
2018-11-20 20:53:45 +01:00
for _ , block := range content . Blocks {
2021-05-15 01:09:51 +02:00
switch block . Type {
case "_" :
if seenEscapeBlock != nil {
diags = append ( diags , & hcl . Diagnostic {
Severity : hcl . DiagError ,
Summary : "Duplicate escaping block" ,
Detail : fmt . Sprintf (
"The special block type \"_\" can be used to force particular arguments to be interpreted as module input variables rather than as meta-arguments, but each module block can have only one such block. The first escaping block was at %s." ,
seenEscapeBlock . DefRange ,
) ,
Subject : & block . DefRange ,
} )
continue
}
seenEscapeBlock = block
// When there's an escaping block its content merges with the
// existing config we extracted earlier, so later decoding
// will see a blend of both.
mc . Config = hcl . MergeBodies ( [ ] hcl . Body { mc . Config , block . Body } )
default :
// All of the other block types in our schema are reserved.
diags = append ( diags , & hcl . Diagnostic {
Severity : hcl . DiagError ,
Summary : "Reserved block type name in module block" ,
Detail : fmt . Sprintf ( "The block type name %q is reserved for use by Terraform in a future version." , block . Type ) ,
Subject : & block . TypeRange ,
} )
}
2018-11-20 20:53:45 +01:00
}
2018-02-03 02:22:25 +01:00
return mc , diags
}
2021-06-01 21:30:56 +02:00
// EntersNewPackage returns true if this call is to an external module, either
// directly via a remote source address or indirectly via a registry source
// address.
//
// Other behaviors in Terraform may treat package crossings as a special
// situation, because that indicates that the caller and callee can change
// independently of one another and thus we should disallow using any features
// where the caller assumes anything about the callee other than its input
// variables, required provider configurations, and output values.
func ( mc * ModuleCall ) EntersNewPackage ( ) bool {
return moduleSourceAddrEntersNewPackage ( mc . SourceAddr )
}
2018-04-24 19:06:51 +02:00
// PassedProviderConfig represents a provider config explicitly passed down to
// a child module, possibly giving it a new local address in the process.
type PassedProviderConfig struct {
InChild * ProviderConfigRef
InParent * ProviderConfigRef
}
2018-02-03 02:22:25 +01:00
var moduleBlockSchema = & hcl . BodySchema {
Attributes : [ ] hcl . AttributeSchema {
{
Name : "source" ,
Required : true ,
} ,
{
Name : "version" ,
} ,
{
Name : "count" ,
} ,
{
Name : "for_each" ,
} ,
{
Name : "depends_on" ,
} ,
2018-04-24 19:06:51 +02:00
{
Name : "providers" ,
} ,
2018-02-03 02:22:25 +01:00
} ,
2018-11-20 20:53:45 +01:00
Blocks : [ ] hcl . BlockHeaderSchema {
2021-05-15 01:09:51 +02:00
{ Type : "_" } , // meta-argument escaping block
2018-11-20 20:53:45 +01:00
// These are all reserved for future use.
{ Type : "lifecycle" } ,
{ Type : "locals" } ,
{ Type : "provider" , LabelNames : [ ] string { "type" } } ,
} ,
2018-02-03 02:22:25 +01:00
}
2021-06-01 21:30:56 +02:00
func moduleSourceAddrEntersNewPackage ( addr addrs . ModuleSource ) bool {
switch addr . ( type ) {
case nil :
// There are only two situations where we should get here:
// - We've been asked about the source address of the root module,
// which is always nil.
// - We've been asked about a ModuleCall that is part of the partial
// result of a failed decode.
// The root module exists outside of all module packages, so we'll
// just return false for that case. For the error case it doesn't
// really matter what we return as long as we don't panic, because
// we only make a best-effort to allow careful inspection of objects
// representing invalid configuration.
return false
case addrs . ModuleSourceLocal :
// Local source addresses are the only address type that remains within
// the same package.
return false
default :
// All other address types enter a new package.
return true
}
}