@@ -13,10 +13,11 @@ import (
1313 "time"
1414
1515 "github.com/hashicorp/go-multierror"
16- testing "github.com/mitchellh/go-testing-interface"
16+ "github.com/mitchellh/go-testing-interface"
1717
1818 "github.com/hashicorp/terraform-plugin-go/tfprotov5"
1919 "github.com/hashicorp/terraform-plugin-go/tfprotov6"
20+
2021 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2122 "github.com/hashicorp/terraform-plugin-sdk/v2/internal/addrs"
2223 "github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging"
@@ -49,7 +50,7 @@ var flagSweepAllowFailures = flag.Bool("sweep-allow-failures", false, "Enable to
4950var flagSweepRun = flag .String ("sweep-run" , "" , "Comma seperated list of Sweeper Tests to run" )
5051var sweeperFuncs map [string ]* Sweeper
5152
52- // type SweeperFunc is a signature for a function that acts as a sweeper. It
53+ // SweeperFunc is a signature for a function that acts as a sweeper. It
5354// accepts a string for the region that the sweeper is to be ran in. This
5455// function must be able to construct a valid client for that region.
5556type SweeperFunc func (r string ) error
@@ -959,6 +960,68 @@ func testCheckResourceAttr(is *terraform.InstanceState, name string, key string,
959960 return nil
960961}
961962
963+ // CheckResourceAttrWithFunc is the callback type used to apply a custom checking logic
964+ // when using TestCheckResourceAttrWith and a value is found for the given name and key.
965+ //
966+ // When this function returns an error, TestCheckResourceAttrWith will fail the check.
967+ type CheckResourceAttrWithFunc func (value string ) error
968+
969+ // TestCheckResourceAttrWith ensures a value stored in state for the
970+ // given name and key combination, is checked against a custom logic.
971+ // State value checking is only recommended for testing Computed attributes
972+ // and attribute defaults.
973+ //
974+ // For managed resources, the name parameter is combination of the resource
975+ // type, a period (.), and the name label. The name for the below example
976+ // configuration would be "myprovider_thing.example".
977+ //
978+ // resource "myprovider_thing" "example" { ... }
979+ //
980+ // For data sources, the name parameter is a combination of the keyword "data",
981+ // a period (.), the data source type, a period (.), and the name label. The
982+ // name for the below example configuration would be
983+ // "data.myprovider_thing.example".
984+ //
985+ // data "myprovider_thing" "example" { ... }
986+ //
987+ // The key parameter is an attribute path in Terraform CLI 0.11 and earlier
988+ // "flatmap" syntax. Keys start with the attribute name of a top-level
989+ // attribute. Use the following special key syntax to inspect list, map, and
990+ // set attributes:
991+ //
992+ // - .{NUMBER}: List value at index, e.g. .0 to inspect the first element.
993+ // Use the TestCheckTypeSet* and TestMatchTypeSet* functions instead
994+ // for sets.
995+ // - .{KEY}: Map value at key, e.g. .example to inspect the example key
996+ // value.
997+ // - .#: Number of elements in list or set.
998+ // - .%: Number of elements in map.
999+ //
1000+ // The checkValueFunc parameter is a CheckResourceAttrWithFunc,
1001+ // and it's provided with the attribute value to apply a custom checking logic,
1002+ // if it was found in the state. The function must return an error for the
1003+ // check to fail, or `nil` to succeed.
1004+ func TestCheckResourceAttrWith (name , key string , checkValueFunc CheckResourceAttrWithFunc ) TestCheckFunc {
1005+ return checkIfIndexesIntoTypeSet (key , func (s * terraform.State ) error {
1006+ is , err := primaryInstanceState (s , name )
1007+ if err != nil {
1008+ return err
1009+ }
1010+
1011+ err = testCheckResourceAttrSet (is , name , key )
1012+ if err != nil {
1013+ return err
1014+ }
1015+
1016+ err = checkValueFunc (is .Attributes [key ])
1017+ if err != nil {
1018+ return fmt .Errorf ("%s: Attribute %q value: %w" , name , key , err )
1019+ }
1020+
1021+ return nil
1022+ })
1023+ }
1024+
9621025// TestCheckNoResourceAttr ensures no value exists in the state for the
9631026// given name and key combination. The opposite of this TestCheckFunc is
9641027// TestCheckResourceAttrSet. State value checking is only recommended for
0 commit comments