2019-12-06 18:14:54 +01:00
|
|
|
package configs
|
|
|
|
|
|
|
|
import (
|
2020-01-10 17:54:53 +01:00
|
|
|
version "github.com/hashicorp/go-version"
|
2019-12-06 18:14:54 +01:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2020-01-13 17:31:47 +01:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2020-06-24 16:09:12 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2019-12-06 18:14:54 +01:00
|
|
|
)
|
|
|
|
|
2020-01-13 17:31:47 +01:00
|
|
|
// RequiredProvider represents a declaration of a dependency on a particular
|
2020-03-12 17:00:00 +01:00
|
|
|
// provider version or source without actually configuring that provider. This
|
|
|
|
// is used in child modules that expect a provider to be passed in from their
|
|
|
|
// parent.
|
2020-01-13 17:31:47 +01:00
|
|
|
type RequiredProvider struct {
|
2019-12-06 18:14:54 +01:00
|
|
|
Name string
|
2020-05-06 19:35:35 +02:00
|
|
|
Source string
|
2020-04-24 16:54:24 +02:00
|
|
|
Type addrs.Provider
|
2019-12-06 18:14:54 +01:00
|
|
|
Requirement VersionConstraint
|
2020-04-24 16:54:24 +02:00
|
|
|
DeclRange hcl.Range
|
2019-12-06 18:14:54 +01:00
|
|
|
}
|
|
|
|
|
2020-04-24 16:54:24 +02:00
|
|
|
type RequiredProviders struct {
|
|
|
|
RequiredProviders map[string]*RequiredProvider
|
|
|
|
DeclRange hcl.Range
|
2020-03-12 17:00:00 +01:00
|
|
|
}
|
|
|
|
|
2020-04-24 16:54:24 +02:00
|
|
|
func decodeRequiredProvidersBlock(block *hcl.Block) (*RequiredProviders, hcl.Diagnostics) {
|
2019-12-06 18:14:54 +01:00
|
|
|
attrs, diags := block.Body.JustAttributes()
|
2020-04-24 16:54:24 +02:00
|
|
|
ret := &RequiredProviders{
|
|
|
|
RequiredProviders: make(map[string]*RequiredProvider),
|
|
|
|
DeclRange: block.DefRange,
|
|
|
|
}
|
2019-12-06 18:14:54 +01:00
|
|
|
for name, attr := range attrs {
|
2020-01-10 17:54:53 +01:00
|
|
|
expr, err := attr.Expr.Value(nil)
|
|
|
|
if err != nil {
|
|
|
|
diags = append(diags, err...)
|
|
|
|
}
|
|
|
|
|
2020-05-14 15:00:58 +02:00
|
|
|
// verify that the local name is already localized or produce an error.
|
|
|
|
nameDiags := checkProviderNameNormalized(name, attr.Expr.Range())
|
|
|
|
diags = append(diags, nameDiags...)
|
|
|
|
|
2020-04-24 16:54:24 +02:00
|
|
|
rp := &RequiredProvider{
|
|
|
|
Name: name,
|
|
|
|
DeclRange: attr.Expr.Range(),
|
|
|
|
}
|
|
|
|
|
2020-01-10 17:54:53 +01:00
|
|
|
switch {
|
|
|
|
case expr.Type().IsPrimitiveType():
|
|
|
|
vc, reqDiags := decodeVersionConstraint(attr)
|
|
|
|
diags = append(diags, reqDiags...)
|
2020-04-24 16:54:24 +02:00
|
|
|
rp.Requirement = vc
|
|
|
|
|
2020-01-10 17:54:53 +01:00
|
|
|
case expr.Type().IsObjectType():
|
|
|
|
if expr.Type().HasAttribute("version") {
|
|
|
|
vc := VersionConstraint{
|
|
|
|
DeclRange: attr.Range,
|
|
|
|
}
|
2020-06-24 16:09:12 +02:00
|
|
|
constraint := expr.GetAttr("version")
|
|
|
|
if !constraint.Type().Equals(cty.String) || constraint.IsNull() {
|
2020-01-10 17:54:53 +01:00
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Invalid version constraint",
|
2020-06-24 16:09:12 +02:00
|
|
|
Detail: "Version must be specified as a string.",
|
2020-01-10 17:54:53 +01:00
|
|
|
Subject: attr.Expr.Range().Ptr(),
|
|
|
|
})
|
2020-02-11 19:17:37 +01:00
|
|
|
} else {
|
2020-06-24 16:09:12 +02:00
|
|
|
constraintStr := constraint.AsString()
|
|
|
|
constraints, err := version.NewConstraint(constraintStr)
|
|
|
|
if err != nil {
|
|
|
|
// NewConstraint doesn't return user-friendly errors, so we'll just
|
|
|
|
// ignore the provided error and produce our own generic one.
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Invalid version constraint",
|
|
|
|
Detail: "This string does not use correct version constraint syntax.",
|
|
|
|
Subject: attr.Expr.Range().Ptr(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
vc.Required = constraints
|
|
|
|
rp.Requirement = vc
|
|
|
|
}
|
2020-01-10 17:54:53 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-11 19:17:37 +01:00
|
|
|
if expr.Type().HasAttribute("source") {
|
2020-06-24 16:09:12 +02:00
|
|
|
source := expr.GetAttr("source")
|
|
|
|
if !source.Type().Equals(cty.String) || source.IsNull() {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Invalid source",
|
|
|
|
Detail: "Source must be specified as a string.",
|
|
|
|
Subject: attr.Expr.Range().Ptr(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
rp.Source = source.AsString()
|
2020-05-14 15:00:58 +02:00
|
|
|
|
2020-06-24 16:09:12 +02:00
|
|
|
fqn, sourceDiags := addrs.ParseProviderSourceString(rp.Source)
|
2020-04-24 16:54:24 +02:00
|
|
|
|
2020-06-24 16:09:12 +02:00
|
|
|
if sourceDiags.HasErrors() {
|
|
|
|
hclDiags := sourceDiags.ToHCL()
|
|
|
|
// The diagnostics from ParseProviderSourceString don't contain
|
|
|
|
// source location information because it has no context to compute
|
|
|
|
// them from, and so we'll add those in quickly here before we
|
|
|
|
// return.
|
|
|
|
for _, diag := range hclDiags {
|
|
|
|
if diag.Subject == nil {
|
|
|
|
diag.Subject = attr.Expr.Range().Ptr()
|
|
|
|
}
|
2020-04-24 16:54:24 +02:00
|
|
|
}
|
2020-06-24 16:09:12 +02:00
|
|
|
diags = append(diags, hclDiags...)
|
|
|
|
} else {
|
|
|
|
rp.Type = fqn
|
2020-04-24 16:54:24 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-11 19:17:37 +01:00
|
|
|
}
|
2020-04-24 16:54:24 +02:00
|
|
|
|
2020-01-10 17:54:53 +01:00
|
|
|
default:
|
|
|
|
// should not happen
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2020-04-24 16:54:24 +02:00
|
|
|
Summary: "Invalid required_providers syntax",
|
|
|
|
Detail: "required_providers entries must be strings or objects.",
|
2020-01-10 17:54:53 +01:00
|
|
|
Subject: attr.Expr.Range().Ptr(),
|
2019-12-06 18:14:54 +01:00
|
|
|
})
|
|
|
|
}
|
2020-04-24 16:54:24 +02:00
|
|
|
|
2020-05-14 15:00:58 +02:00
|
|
|
if rp.Type.IsZero() && !diags.HasErrors() { // Don't try to generate an FQN if we've encountered errors
|
2020-04-24 16:54:24 +02:00
|
|
|
pType, err := addrs.ParseProviderPart(rp.Name)
|
|
|
|
if err != nil {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Invalid provider name",
|
|
|
|
Detail: err.Error(),
|
|
|
|
Subject: attr.Expr.Range().Ptr(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
rp.Type = addrs.ImpliedProviderForUnqualifiedType(pType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.RequiredProviders[rp.Name] = rp
|
2019-12-06 18:14:54 +01:00
|
|
|
}
|
2020-04-24 16:54:24 +02:00
|
|
|
|
|
|
|
return ret, diags
|
2019-12-06 18:14:54 +01:00
|
|
|
}
|