Skip to content

Commit 61e693e

Browse files
authored
fix: allow array output type in tfstate outputs (#1373)
* fix: allow tfstate output type to be array * fix test name * fix test name * fix(tfstate): allow output type to be JSON array * fix(tf): allow tfstate output type to be an array * fix test import format * test(tfstate): verify GetOutputs with array output type
1 parent a4626e1 commit 61e693e

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

brokerpaktestframework/terraform_mock.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,41 @@ type TFStateValue struct {
134134
// SetTFState set the Terraform State in a JSON file.
135135
func (p TerraformMock) SetTFState(values []TFStateValue) error {
136136
var outputs = make(map[string]struct {
137-
Type string `json:"type"`
138-
Value any `json:"value"`
137+
Type json.RawMessage `json:"type"`
138+
Value any `json:"value"`
139139
})
140+
140141
for _, value := range values {
141142
outputs[value.Name] = struct {
142-
Type string `json:"type"`
143-
Value any `json:"value"`
143+
Type json.RawMessage `json:"type"`
144+
Value any `json:"value"`
144145
}{
145-
Type: value.Type,
146+
Type: normalizeOutputType(value.Type),
146147
Value: value.Value,
147148
}
148149
}
149150

150151
return p.setTFStateFile(workspace.Tfstate{
151152
Version: 4,
152153
TerraformVersion: p.Version,
153-
Outputs: outputs})
154+
Outputs: outputs,
155+
})
156+
}
157+
158+
func normalizeOutputType(t string) json.RawMessage {
159+
s := strings.TrimSpace(t)
160+
if s == "" {
161+
return json.RawMessage(`"string"`)
162+
}
163+
164+
if (strings.HasPrefix(s, "[") && strings.HasSuffix(s, "]")) ||
165+
(strings.HasPrefix(s, "{") && strings.HasSuffix(s, "}")) ||
166+
(strings.HasPrefix(s, "\"") && strings.HasSuffix(s, "\"")) {
167+
return json.RawMessage(s)
168+
}
169+
170+
b, _ := json.Marshal(s)
171+
return json.RawMessage(b)
154172
}
155173

156174
// ReturnTFState set the Terraform State in a JSON file.

pkg/providers/tf/workspace/tfstate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ type Tfstate struct {
4242
Version int `json:"version"`
4343
TerraformVersion string `json:"terraform_version"`
4444
Outputs map[string]struct {
45-
Type string `json:"type"`
46-
Value any `json:"value"`
45+
Type json.RawMessage `json:"type"`
46+
Value any `json:"value"`
4747
} `json:"outputs"`
4848
}
4949

pkg/providers/tf/workspace/tfstate_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
package workspace
1616

17-
import "fmt"
17+
import (
18+
"fmt"
19+
"testing"
20+
)
1821

1922
func ExampleNewTfstate_good() {
2023
state := `{
@@ -110,3 +113,26 @@ func ExampleTfstate_GetOutputs() {
110113

111114
// Output: map[hostname:somehost]
112115
}
116+
117+
func TestNewTfstate_AllowsArrayOutputType(t *testing.T) {
118+
state := []byte(`{
119+
"version": 4,
120+
"terraform_version": "1.8.2",
121+
"outputs": {
122+
"list_output": {
123+
"value": ["a", "b", "c"],
124+
"type": ["list", "string"]
125+
}
126+
}
127+
}`)
128+
129+
tfstate, err := NewTfstate(state)
130+
if err != nil {
131+
t.Fatalf("unexpected error when output.type is an array: %v", err)
132+
}
133+
134+
outputs := tfstate.GetOutputs()
135+
if outputs["list_output"] == nil {
136+
t.Fatal("expected list_output to be present in outputs")
137+
}
138+
}

0 commit comments

Comments
 (0)