@@ -2,36 +2,55 @@ package github
22
33import (
44 "fmt"
5- "strings"
65
7- "github.com/hashicorp/terraform-plugin-testing/helper/resource"
8- "github.com/hashicorp/terraform-plugin-testing/terraform"
6+ "github.com/hashicorp/terraform-plugin-testing/knownvalue"
97)
108
11- // checkCollectionItemAbsent checks that a collection attribute does not contain an item with a specific field value.
12- func checkCollectionItemAbsent (resourceName , collectionAttr , field , forbidden string ) resource.TestCheckFunc {
13- return func (s * terraform.State ) error {
14- rs , ok := s .RootModule ().Resources [resourceName ]
15- if ! ok {
16- return fmt .Errorf ("resource not found: %s" , resourceName )
17- }
9+ var _ knownvalue.Check = setAbsent {}
1810
19- prefix := collectionAttr + "."
20- suffix := "." + field
11+ // setAbsent is a knownvalue.Check implementation that asserts that a specific value is not present in a set.
12+ type setAbsent struct {
13+ value []knownvalue.Check
14+ }
2115
22- for k , v := range rs .Primary .Attributes {
23- if ! strings .HasPrefix (k , prefix ) {
24- continue
25- }
26- // skip collection metadata
27- if strings .HasSuffix (k , ".#" ) || strings .HasSuffix (k , ".%" ) {
28- continue
29- }
30- if strings .HasSuffix (k , suffix ) && v == forbidden {
31- return fmt .Errorf ("%s contains forbidden %s=%q (key %s)" , collectionAttr , field , forbidden , k )
16+ // CheckValue determines whether the passed value of type []any, and does not contain the expected value.
17+ func (v setAbsent ) CheckValue (other any ) error {
18+ otherVals , ok := other .([]any )
19+ if ! ok {
20+ return fmt .Errorf ("expected []any value for SetAbsent check, got: %T" , other )
21+ }
22+
23+ for _ , otherVal := range otherVals {
24+ match := true
25+ for _ , check := range v .value {
26+ if err := check .CheckValue (otherVal ); err != nil {
27+ match = false
28+ break
3229 }
3330 }
3431
35- return nil
32+ if match {
33+ return fmt .Errorf ("found unexpected value %s for SetAbsent check" , v .String ())
34+ }
35+ }
36+
37+ return nil
38+ }
39+
40+ // String returns the string representation of the value.
41+ func (v setAbsent ) String () string {
42+ var setVals []string
43+
44+ for _ , val := range v .value {
45+ setVals = append (setVals , val .String ())
46+ }
47+
48+ return fmt .Sprintf ("%s" , setVals )
49+ }
50+
51+ // SetAbsent returns a knownvalue.Check for asserting the value defined by the []knownvalue.Check slice is not present in the set.
52+ func SetAbsent (value []knownvalue.Check ) setAbsent {
53+ return setAbsent {
54+ value : value ,
3655 }
3756}
0 commit comments