mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
config: outputs must be unique
This commit is contained in:
parent
8494cad8c4
commit
099293b690
@ -586,43 +586,55 @@ func (c *Config) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check that all outputs are valid
|
// Check that all outputs are valid
|
||||||
for _, o := range c.Outputs {
|
{
|
||||||
var invalidKeys []string
|
found := make(map[string]struct{})
|
||||||
valueKeyFound := false
|
for _, o := range c.Outputs {
|
||||||
for k := range o.RawConfig.Raw {
|
// Verify the output is new
|
||||||
if k == "value" {
|
if _, ok := found[o.Name]; ok {
|
||||||
valueKeyFound = true
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if k == "sensitive" {
|
|
||||||
if sensitive, ok := o.RawConfig.config[k].(bool); ok {
|
|
||||||
if sensitive {
|
|
||||||
o.Sensitive = true
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
errs = append(errs, fmt.Errorf(
|
errs = append(errs, fmt.Errorf(
|
||||||
"%s: value for 'sensitive' must be boolean",
|
"%s: duplicate output. output names must be unique.",
|
||||||
o.Name))
|
o.Name))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
invalidKeys = append(invalidKeys, k)
|
found[o.Name] = struct{}{}
|
||||||
}
|
|
||||||
if len(invalidKeys) > 0 {
|
|
||||||
errs = append(errs, fmt.Errorf(
|
|
||||||
"%s: output has invalid keys: %s",
|
|
||||||
o.Name, strings.Join(invalidKeys, ", ")))
|
|
||||||
}
|
|
||||||
if !valueKeyFound {
|
|
||||||
errs = append(errs, fmt.Errorf(
|
|
||||||
"%s: output is missing required 'value' key", o.Name))
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, v := range o.RawConfig.Variables {
|
var invalidKeys []string
|
||||||
if _, ok := v.(*CountVariable); ok {
|
valueKeyFound := false
|
||||||
|
for k := range o.RawConfig.Raw {
|
||||||
|
if k == "value" {
|
||||||
|
valueKeyFound = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if k == "sensitive" {
|
||||||
|
if sensitive, ok := o.RawConfig.config[k].(bool); ok {
|
||||||
|
if sensitive {
|
||||||
|
o.Sensitive = true
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
errs = append(errs, fmt.Errorf(
|
||||||
|
"%s: value for 'sensitive' must be boolean",
|
||||||
|
o.Name))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
invalidKeys = append(invalidKeys, k)
|
||||||
|
}
|
||||||
|
if len(invalidKeys) > 0 {
|
||||||
errs = append(errs, fmt.Errorf(
|
errs = append(errs, fmt.Errorf(
|
||||||
"%s: count variables are only valid within resources", o.Name))
|
"%s: output has invalid keys: %s",
|
||||||
|
o.Name, strings.Join(invalidKeys, ", ")))
|
||||||
|
}
|
||||||
|
if !valueKeyFound {
|
||||||
|
errs = append(errs, fmt.Errorf(
|
||||||
|
"%s: output is missing required 'value' key", o.Name))
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range o.RawConfig.Variables {
|
||||||
|
if _, ok := v.(*CountVariable); ok {
|
||||||
|
errs = append(errs, fmt.Errorf(
|
||||||
|
"%s: count variables are only valid within resources", o.Name))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -300,6 +300,13 @@ func TestConfigValidate_outputBadField(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfigValidate_outputDuplicate(t *testing.T) {
|
||||||
|
c := testConfig(t, "validate-output-dup")
|
||||||
|
if err := c.Validate(); err == nil {
|
||||||
|
t.Fatal("should not be valid")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestConfigValidate_pathVar(t *testing.T) {
|
func TestConfigValidate_pathVar(t *testing.T) {
|
||||||
c := testConfig(t, "validate-path-var")
|
c := testConfig(t, "validate-path-var")
|
||||||
if err := c.Validate(); err != nil {
|
if err := c.Validate(); err != nil {
|
||||||
|
10
config/test-fixtures/validate-output-dup/main.tf
Normal file
10
config/test-fixtures/validate-output-dup/main.tf
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
resource "aws_instance" "web" {
|
||||||
|
}
|
||||||
|
|
||||||
|
output "ip" {
|
||||||
|
value = "foo"
|
||||||
|
}
|
||||||
|
|
||||||
|
output "ip" {
|
||||||
|
value = "bar"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user