Skip to content

Commit 71ab7e0

Browse files
committed
Adds querycheck.ExpectNoIdentity
1 parent 2949565 commit 71ab7e0

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

querycheck/expect_no_identity.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package querycheck
5+
6+
import (
7+
"context"
8+
"errors"
9+
"fmt"
10+
"sort"
11+
"strings"
12+
13+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
14+
"github.com/hashicorp/terraform-plugin-testing/statecheck"
15+
)
16+
17+
var _ QueryResultCheck = expectNoIdentity{}
18+
19+
type expectNoIdentity struct {
20+
listResourceAddress string
21+
check map[string]knownvalue.Check
22+
}
23+
24+
// CheckQuery implements the query check logic.
25+
func (e expectNoIdentity) CheckQuery(_ context.Context, req CheckQueryRequest, resp *CheckQueryResponse) {
26+
for _, res := range req.Query {
27+
var errCollection []error
28+
29+
if e.listResourceAddress != strings.TrimPrefix(res.Address, "list.") {
30+
continue
31+
}
32+
33+
if len(res.Identity) != len(e.check) {
34+
deltaMsg := ""
35+
if len(res.Identity) > len(e.check) {
36+
deltaMsg = statecheck.CreateDeltaString(res.Identity, e.check, "actual identity has extra attribute(s): ")
37+
} else {
38+
deltaMsg = statecheck.CreateDeltaString(e.check, res.Identity, "actual identity is missing attribute(s): ")
39+
}
40+
41+
resp.Error = fmt.Errorf("%s - Expected %d attribute(s) in the actual identity object, got %d attribute(s): %s", e.listResourceAddress, len(e.check), len(res.Identity), deltaMsg)
42+
return
43+
}
44+
45+
var keys []string
46+
47+
for k := range e.check {
48+
keys = append(keys, k)
49+
}
50+
51+
sort.SliceStable(keys, func(i, j int) bool {
52+
return keys[i] < keys[j]
53+
})
54+
55+
for _, k := range keys {
56+
actualIdentityVal, ok := res.Identity[k]
57+
58+
if !ok {
59+
resp.Error = fmt.Errorf("%s - missing attribute %q in actual identity object", e.listResourceAddress, k)
60+
return
61+
}
62+
63+
if err := e.check[k].CheckValue(actualIdentityVal); err != nil {
64+
errCollection = append(errCollection, fmt.Errorf("%s - %q identity attribute: %s", e.listResourceAddress, k, err))
65+
}
66+
}
67+
68+
if errCollection == nil {
69+
errs := []error{fmt.Errorf("an unexpected identity matching the given attributes was found")}
70+
// wrap errors for each check
71+
for attr, check := range e.check {
72+
errs = append(errs, fmt.Errorf("attribute %q: %s", attr, check))
73+
}
74+
errs = append(errs, fmt.Errorf("address: %s\n", e.listResourceAddress))
75+
resp.Error = errors.Join(errs...)
76+
}
77+
}
78+
}
79+
80+
// ExpectNoIdentity returns a query check that asserts that the identity at the given resource does not match a known object, where each
81+
// map key represents an identity attribute name. The identity in query must exactly match the given object.
82+
//
83+
// This query check can only be used with managed resources that support resource identity and query. Query is only supported in Terraform v1.14+
84+
func ExpectNoIdentity(resourceAddress string, identity map[string]knownvalue.Check) QueryResultCheck {
85+
return expectNoIdentity{
86+
listResourceAddress: resourceAddress,
87+
check: identity,
88+
}
89+
}

0 commit comments

Comments
 (0)