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
|
|
|
|
|
2018-02-01 22:33:06 -06:00
|
|
|
package configs
|
|
|
|
|
|
|
|
import (
|
2018-02-02 19:22:25 -06:00
|
|
|
"fmt"
|
|
|
|
|
2018-02-01 22:33:06 -06:00
|
|
|
version "github.com/hashicorp/go-version"
|
2019-09-09 17:58:44 -05:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2024-10-03 09:46:58 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/lang/marks"
|
2018-02-02 19:22:25 -06:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"github.com/zclconf/go-cty/cty/convert"
|
2018-02-01 22:33:06 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// VersionConstraint represents a version constraint on some resource
|
2023-09-26 12:09:27 -05:00
|
|
|
// (e.g. OpenTofu Core, a provider, a module, ...) that carries with it
|
2018-02-01 22:33:06 -06:00
|
|
|
// a source range so that a helpful diagnostic can be printed in the event
|
|
|
|
// that a particular constraint does not match.
|
|
|
|
type VersionConstraint struct {
|
2018-02-02 19:22:25 -06:00
|
|
|
Required version.Constraints
|
2018-02-01 22:33:06 -06:00
|
|
|
DeclRange hcl.Range
|
|
|
|
}
|
2018-02-02 19:22:25 -06:00
|
|
|
|
|
|
|
func decodeVersionConstraint(attr *hcl.Attribute) (VersionConstraint, hcl.Diagnostics) {
|
2024-06-24 08:13:07 -05:00
|
|
|
val, diags := attr.Expr.Value(nil)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
return VersionConstraint{}, diags
|
|
|
|
}
|
|
|
|
return decodeVersionConstraintValue(attr, val)
|
|
|
|
}
|
|
|
|
func decodeVersionConstraintValue(attr *hcl.Attribute, val cty.Value) (VersionConstraint, hcl.Diagnostics) {
|
|
|
|
var diags hcl.Diagnostics
|
|
|
|
|
2018-02-02 19:22:25 -06:00
|
|
|
ret := VersionConstraint{
|
|
|
|
DeclRange: attr.Range,
|
|
|
|
}
|
|
|
|
|
2024-10-03 09:46:58 -05:00
|
|
|
if val.HasMark(marks.Sensitive) {
|
|
|
|
return ret, diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Invalid version constraint",
|
|
|
|
Detail: fmt.Sprintf("Sensitive values, or values derived from sensitive values, cannot be used as %s arguments.", attr.Name),
|
|
|
|
Subject: attr.Expr.Range().Ptr(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:22:25 -06:00
|
|
|
var err error
|
|
|
|
val, err = convert.Convert(val, cty.String)
|
|
|
|
if err != nil {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Invalid version constraint",
|
|
|
|
Detail: fmt.Sprintf("A string value is required for %s.", attr.Name),
|
|
|
|
Subject: attr.Expr.Range().Ptr(),
|
|
|
|
})
|
|
|
|
return ret, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
if val.IsNull() {
|
|
|
|
// A null version constraint is strange, but we'll just treat it
|
|
|
|
// like an empty constraint set.
|
|
|
|
return ret, diags
|
|
|
|
}
|
|
|
|
|
2019-06-21 13:30:17 -05:00
|
|
|
if !val.IsWhollyKnown() {
|
|
|
|
// If there is a syntax error, HCL sets the value of the given attribute
|
|
|
|
// to cty.DynamicVal. A diagnostic for the syntax error will already
|
|
|
|
// bubble up, so we will move forward gracefully here.
|
|
|
|
return ret, diags
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:22:25 -06:00
|
|
|
constraintStr := val.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.", // Not very actionable :(
|
|
|
|
Subject: attr.Expr.Range().Ptr(),
|
|
|
|
})
|
|
|
|
return ret, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.Required = constraints
|
|
|
|
return ret, diags
|
|
|
|
}
|