Skip to content

Commit 8097374

Browse files
authored
Fix panic when generating json config for tf query (#37596)
1 parent 90809dc commit 8097374

File tree

18 files changed

+679
-16
lines changed

18 files changed

+679
-16
lines changed

internal/command/query_test.go

Lines changed: 469 additions & 9 deletions
Large diffs are not rendered by default.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
terraform {
2+
required_providers {
3+
test = {
4+
source = "hashicorp/test"
5+
}
6+
}
7+
}
8+
9+
variable "target_ami" {
10+
description = "The AMI to search for"
11+
type = string
12+
default = "ami-12345"
13+
}
14+
15+
variable "instance_name" {
16+
description = "The instance name to search for"
17+
type = string
18+
}
19+
20+
provider "test" {}
21+
22+
resource "test_instance" "example" {
23+
ami = "ami-12345"
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// already defined in main.tf
2+
variable "instance_name" {
3+
description = "The instance name to search for"
4+
type = string
5+
}
6+
7+
8+
list "test_instance" "example" {
9+
provider = test
10+
11+
config {
12+
ami = var.target_ami
13+
foo = var.instance_name
14+
}
15+
}

internal/command/testdata/query/empty-result/query.tfquery.hcl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ list "test_instance" "example2" {
1111

1212
config {
1313
ami = "ami-nonexistent"
14+
// to force deterministic ordering in the result
15+
foo = list.test_instance.example.data[0].state.id
1416
}
1517
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
terraform {
2+
required_providers {
3+
test = {
4+
source = "hashicorp/test"
5+
}
6+
}
7+
}
8+
9+
provider "test" {}
10+
11+
resource "test_instance" "example" {
12+
ami = "ami-12345"
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
list "test_instance" "example" {
2+
provider = test
3+
4+
config {
5+
ami = uuid() // forces the config to be unknown at plan time
6+
}
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
terraform {
2+
required_providers {
3+
test = {
4+
source = "hashicorp/test"
5+
}
6+
}
7+
}
8+
9+
provider "test" {}
10+
11+
resource "test_instance" "example" {
12+
ami = "ami-12345"
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
variable "target_ami" {
2+
description = "The AMI to search for"
3+
type = string
4+
validation {
5+
condition = length(var.target_ami) > 10
6+
error_message = "AMI ID must be longer than 10 characters."
7+
}
8+
}
9+
10+
list "test_instance" "example" {
11+
provider = test
12+
13+
config {
14+
ami = var.target_ami
15+
foo = "invalid-instance"
16+
}
17+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
target_ami = "ami-67890"
2+
environment = "staging"
3+
instance_count = 3
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
terraform {
2+
required_providers {
3+
test = {
4+
source = "hashicorp/test"
5+
}
6+
}
7+
}
8+
9+
provider "test" {}
10+
11+
resource "test_instance" "example" {
12+
ami = "ami-12345"
13+
}

0 commit comments

Comments
 (0)