Skip to content

Commit 10dd78c

Browse files
committed
add expect known value check
1 parent c465a33 commit 10dd78c

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

querycheck/expect_known_value.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package querycheck
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
8+
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
9+
"strings"
10+
)
11+
12+
var _ QueryResultCheck = expectKnownValue{}
13+
14+
type expectKnownValue struct {
15+
listResourceAddress string
16+
resourceName string
17+
attributePath tfjsonpath.Path
18+
knownValue knownvalue.Check
19+
}
20+
21+
func (e expectKnownValue) CheckQuery(_ context.Context, req CheckQueryRequest, resp *CheckQueryResponse) {
22+
for _, res := range *req.Query {
23+
diags := make([]error, 0)
24+
25+
if e.listResourceAddress == strings.TrimPrefix(res.Address, "list.") && e.resourceName == res.DisplayName {
26+
if res.ResourceObject == nil {
27+
resp.Error = fmt.Errorf("%s - no resource object was returned, ensure `include_resource` has been set to `true` in the list resource config`", e.listResourceAddress)
28+
return
29+
}
30+
31+
// Ideally we can do the check like below which is identical to the expect known value state check but... terraform-json hasn't
32+
// defined the resource object as a map[string]interface, we so we need to iterate over it manually
33+
//resource, err := tfjsonpath.Traverse(res.ResourceObject, e.attributePath)
34+
//if err != nil {
35+
// resp.Error = err
36+
// return
37+
//}
38+
//
39+
//if err := e.knownValue.CheckValue(resource); err != nil {
40+
// diags = append(diags, fmt.Errorf("error checking value for attribute at path: %s for resource %s, err: %s", e.attributePath.String(), e.resourceName, err))
41+
//}
42+
//
43+
//if diags == nil {
44+
// return
45+
//}
46+
47+
for k, v := range res.ResourceObject {
48+
if k == e.attributePath.String() {
49+
var val any
50+
51+
err := json.Unmarshal(v, &val)
52+
if err != nil {
53+
resp.Error = fmt.Errorf("%s - Error decoding message type: %s", e.listResourceAddress, err)
54+
return
55+
}
56+
57+
if err := e.knownValue.CheckValue(val); err != nil {
58+
diags = append(diags, fmt.Errorf("error checking value for attribute at path: %s for resource %s, err: %s", e.attributePath.String(), e.resourceName, err))
59+
}
60+
}
61+
}
62+
}
63+
}
64+
65+
resp.Error = fmt.Errorf("%s - the resource %s was not found", e.listResourceAddress, e.resourceName)
66+
67+
return
68+
}
69+
70+
func ExpectKnownValue(listResourceAddress string, resourceName string, attributePath tfjsonpath.Path, knownValue knownvalue.Check) QueryResultCheck {
71+
return expectKnownValue{
72+
listResourceAddress: listResourceAddress,
73+
resourceName: resourceName,
74+
attributePath: attributePath,
75+
knownValue: knownValue,
76+
}
77+
}

0 commit comments

Comments
 (0)