mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
config: validate that only proper variables can be in the count
This commit is contained in:
parent
53d05cb81f
commit
5090678168
@ -255,6 +255,26 @@ func (c *Config) Validate() error {
|
|||||||
|
|
||||||
// Validate resources
|
// Validate resources
|
||||||
for n, r := range resources {
|
for n, r := range resources {
|
||||||
|
// Verify count variables
|
||||||
|
for _, v := range r.RawCount.Variables {
|
||||||
|
switch v.(type) {
|
||||||
|
case *ModuleVariable:
|
||||||
|
errs = append(errs, fmt.Errorf(
|
||||||
|
"%s: resource count can't reference module variable: %s",
|
||||||
|
n,
|
||||||
|
v.FullKey()))
|
||||||
|
case *ResourceVariable:
|
||||||
|
errs = append(errs, fmt.Errorf(
|
||||||
|
"%s: resource count can't reference resource variable: %s",
|
||||||
|
n,
|
||||||
|
v.FullKey()))
|
||||||
|
case *UserVariable:
|
||||||
|
// Good
|
||||||
|
default:
|
||||||
|
panic("Unknown type in count var: " + n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, d := range r.DependsOn {
|
for _, d := range r.DependsOn {
|
||||||
if _, ok := resources[d]; !ok {
|
if _, ok := resources[d]; !ok {
|
||||||
errs = append(errs, fmt.Errorf(
|
errs = append(errs, fmt.Errorf(
|
||||||
|
@ -53,6 +53,27 @@ func TestConfigValidate_badDependsOn(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfigValidate_countModuleVar(t *testing.T) {
|
||||||
|
c := testConfig(t, "validate-count-module-var")
|
||||||
|
if err := c.Validate(); err == nil {
|
||||||
|
t.Fatal("should not be valid")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConfigValidate_countResourceVar(t *testing.T) {
|
||||||
|
c := testConfig(t, "validate-count-resource-var")
|
||||||
|
if err := c.Validate(); err == nil {
|
||||||
|
t.Fatal("should not be valid")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConfigValidate_countUserVar(t *testing.T) {
|
||||||
|
c := testConfig(t, "validate-count-user-var")
|
||||||
|
if err := c.Validate(); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestConfigValidate_dupModule(t *testing.T) {
|
func TestConfigValidate_dupModule(t *testing.T) {
|
||||||
c := testConfig(t, "validate-dup-module")
|
c := testConfig(t, "validate-dup-module")
|
||||||
if err := c.Validate(); err == nil {
|
if err := c.Validate(); err == nil {
|
||||||
|
7
config/test-fixtures/validate-count-module-var/main.tf
Normal file
7
config/test-fixtures/validate-count-module-var/main.tf
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
module "foo" {
|
||||||
|
source = "./bar"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_instance" "web" {
|
||||||
|
count = "${module.foo.bar}"
|
||||||
|
}
|
5
config/test-fixtures/validate-count-resource-var/main.tf
Normal file
5
config/test-fixtures/validate-count-resource-var/main.tf
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
resource "aws_instance" "foo" {}
|
||||||
|
|
||||||
|
resource "aws_instance" "web" {
|
||||||
|
count = "${aws_instance.foo.bar}"
|
||||||
|
}
|
5
config/test-fixtures/validate-count-user-var/main.tf
Normal file
5
config/test-fixtures/validate-count-user-var/main.tf
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
variable "foo" {}
|
||||||
|
|
||||||
|
resource "aws_instance" "web" {
|
||||||
|
count = "${var.foo}"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user