@@ -26,6 +26,7 @@ type Helper struct {
2626func NewHelper (tfstate * tfjson.State ) * Helper {
2727 return & Helper {tfstate }
2828}
29+
2930func (s * Helper ) ResourceExists (t * testing.T , resourceAddress string ) bool {
3031 t .Helper ()
3132 _ , err := getAttributesValuesFromResource (s , resourceAddress )
@@ -42,6 +43,16 @@ func getAttributesValuesFromResource(state *Helper, address string) (interface{}
4243 return nil , fmt .Errorf ("Could not find resource %q in state" , address )
4344}
4445
46+ // getIdentityValues pulls out the getIdentityValues field from the resource at the given address
47+ func getIdentityValuesFromResource (state * Helper , address string ) (map [string ]any , error ) {
48+ for _ , r := range state .Values .RootModule .Resources {
49+ if r .Address == address {
50+ return r .IdentityValues , nil
51+ }
52+ }
53+ return nil , fmt .Errorf ("Could not find resource %q in state" , address )
54+ }
55+
4556// getOutputValues gets the given output name value from the state
4657func getOutputValues (state * Helper , name string ) (interface {}, error ) {
4758 for n , v := range state .Values .Outputs {
@@ -95,10 +106,10 @@ func parseStateAddress(address string) (string, string) {
95106 switch parts [0 ] {
96107 case "data" :
97108 resourceAddress = strings .Join (parts [0 :3 ], "." )
98- attributeAddress = strings .Join (parts [3 :len ( parts ) ], "." )
109+ attributeAddress = strings .Join (parts [3 :], "." )
99110 default :
100111 resourceAddress = strings .Join (parts [0 :2 ], "." )
101- attributeAddress = strings .Join (parts [2 :len ( parts ) ], "." )
112+ attributeAddress = strings .Join (parts [2 :], "." )
102113 }
103114
104115 return resourceAddress , attributeAddress
@@ -123,6 +134,22 @@ func (s *Helper) GetAttributeValue(t *testing.T, address string) interface{} {
123134 return value
124135}
125136
137+ // GetIdentityValue will get the identity value at the given resource address from the state
138+ func (s * Helper ) GetIdentityValue (t * testing.T , address string , identitykey string ) interface {} {
139+ t .Helper ()
140+
141+ identityvals , err := getIdentityValuesFromResource (s , address )
142+ if err != nil {
143+ t .Fatal (err )
144+ }
145+
146+ val , ok := identityvals [identitykey ]
147+ if ! ok {
148+ t .Fatalf ("resource identity value %q does not exist for %q" , identitykey , address )
149+ }
150+ return val
151+ }
152+
126153// GetOutputValue gets the given output name value from the state
127154func (s * Helper ) GetOutputValue (t * testing.T , name string ) interface {} {
128155 t .Helper ()
@@ -157,6 +184,14 @@ func (s *Helper) AssertAttributeEqual(t *testing.T, address string, expectedValu
157184 fmt .Sprintf ("Address: %q" , address ))
158185}
159186
187+ // AssertIdentityValueEqual will fail the test if the identity value does not equal expectedValue
188+ func (s * Helper ) AssertIdentityValueEqual (t * testing.T , address string , identitykey string , expectedValue interface {}) {
189+ t .Helper ()
190+
191+ assert .EqualValues (t , expectedValue , s .GetIdentityValue (t , address , identitykey ),
192+ fmt .Sprintf ("Resource: %q, Identity key: %q" , address , identitykey ))
193+ }
194+
160195// AssertAttributeNotEqual will fail the test if the attribute is equal to expectedValue
161196func (s * Helper ) AssertAttributeNotEqual (t * testing.T , address string , expectedValue interface {}) {
162197 t .Helper ()
0 commit comments