88 "encoding/json"
99 "errors"
1010 "fmt"
11- "strings"
1211
13- tfjson "github.com/hashicorp/terraform-json"
1412 "github.com/hashicorp/terraform-plugin-testing/knownvalue"
1513)
1614
@@ -23,42 +21,15 @@ type expectIdentity struct {
2321
2422// CheckQuery implements the query check logic.
2523func (e expectIdentity ) CheckQuery (ctx context.Context , req CheckQueryRequest , resp * CheckQueryResponse ) {
26- var foundIdentities []map [string ]json.RawMessage
27-
28- if req .Query == nil {
29- resp .Error = fmt .Errorf ("Query is nil" )
30- return
31- }
32-
33- for _ , v := range * req .Query {
34- switch i := v .(type ) {
35- case tfjson.ListResourceFoundMessage :
36- prefix := "list."
37- if strings .TrimPrefix (i .ListResourceFound .Address , prefix ) == e .resourceAddress {
38- foundIdentities = append (foundIdentities , i .ListResourceFound .Identity )
39- }
40- default :
41- continue
42- }
43- }
44-
45- if len (foundIdentities ) == 0 {
46- resp .Error = fmt .Errorf ("%s - Identity not found in query." , e .resourceAddress )
47-
48- return
49- }
50-
51- var err error
52-
53- for _ , resultIdentity := range foundIdentities {
24+ for _ , res := range * req .Query {
5425 var errCollection []error
5526
5627 for attribute := range e .check {
5728 var val any
58- var ok bool
5929 var unmarshalledVal any
6030
61- if val , ok = resultIdentity [attribute ]; ! ok {
31+ val , ok := res .Identity [attribute ]
32+ if ! ok {
6233 resp .Error = fmt .Errorf ("%s - expected attribute %q not in actual identity object" , e .resourceAddress , attribute )
6334 return
6435 }
@@ -68,7 +39,7 @@ func (e expectIdentity) CheckQuery(ctx context.Context, req CheckQueryRequest, r
6839 resp .Error = fmt .Errorf ("%s - expected json.RawMessage but got %T" , e .resourceAddress , val )
6940 return
7041 }
71- err = json .Unmarshal (rawMessage , & unmarshalledVal )
42+ err : = json .Unmarshal (rawMessage , & unmarshalledVal )
7243
7344 if err != nil {
7445 resp .Error = fmt .Errorf ("%s - Error decoding message type: %s" , e .resourceAddress , err )
@@ -79,7 +50,6 @@ func (e expectIdentity) CheckQuery(ctx context.Context, req CheckQueryRequest, r
7950 errCollection = append (errCollection , fmt .Errorf ("%s - %q identity attribute: %s\n " , e .resourceAddress , e .check , err ))
8051 }
8152 }
82-
8353 if errCollection == nil {
8454 return
8555 }
0 commit comments