@@ -24,6 +24,7 @@ import (
2424 "regexp"
2525 "strings"
2626 "testing"
27+ "time"
2728
2829 "github.com/hashicorp/terraform-plugin-testing/helper/resource"
2930 "github.com/hashicorp/terraform-plugin-testing/terraform"
@@ -43,11 +44,14 @@ type ImportMetadata struct {
4344 IgnoredFields []string `json:"ignored_fields,omitempty"`
4445}
4546
47+ // The metadata for each step in one test
4648type TgcMetadataPayload struct {
4749 TestName string `json:"test_name"`
50+ StepNumber int `json:"step_number"`
4851 RawConfig string `json:"raw_config"`
4952 ResourceMetadata map [string ]ResourceMetadata `json:"resource_metadata"`
5053 PrimaryResource string `json:"primary_resource"`
54+ CaiReadTime time.Time `json:"cai_read_time"`
5155}
5256
5357// PROJECT_NUMBER instead of PROJECT_ID is in the CAI asset names for the resources in those services
@@ -88,6 +92,8 @@ func encodeToBase64JSON(data interface{}) (string, error) {
8892// CollectAllTgcMetadata collects metadata for all resources in a test step
8993func CollectAllTgcMetadata (tgcPayload TgcMetadataPayload ) resource.TestCheckFunc {
9094 return func (s * terraform.State ) error {
95+ tgcPayload .CaiReadTime = time .Now ()
96+
9197 projectId := envvar .GetTestProjectFromEnv ()
9298 projectNumber := envvar .GetTestProjectNumberFromEnv ()
9399
@@ -134,12 +140,14 @@ func CollectAllTgcMetadata(tgcPayload TgcMetadataPayload) resource.TestCheckFunc
134140 tgcPayload .ResourceMetadata [address ] = metadata
135141 }
136142
143+ log .Printf ("[DEBUG] tgcPayload caireadtime %s" , tgcPayload .CaiReadTime )
144+
137145 // Encode the entire payload to base64 JSON
138146 encodedData , err := encodeToBase64JSON (tgcPayload )
139147 if err != nil {
140- log .Printf ("[DEBUG]TGC Terraform error: %v" , err )
148+ log .Printf ("[DEBUG]test_step_number=%d TGC Terraform error: %v" , tgcPayload . StepNumber , err )
141149 } else {
142- log .Printf ("[DEBUG]TGC Terraform metadata: %s" , encodedData )
150+ log .Printf ("[DEBUG]test_step_number=%d TGC Terraform metadata: %s" , tgcPayload . StepNumber , encodedData )
143151 }
144152
145153 return nil
@@ -209,20 +217,10 @@ func determineImportMetadata(steps []resource.TestStep, currentStepIndex int, re
209217func extendWithTGCData (t * testing.T , c resource.TestCase ) resource.TestCase {
210218 var updatedSteps []resource.TestStep
211219
212- // Find the last non-plan config step
213- lastNonPlanConfigStep := - 1
214- for i := len (c .Steps ) - 1 ; i >= 0 ; i -- {
215- step := c .Steps [i ]
216- if step .Config != "" && ! step .PlanOnly {
217- lastNonPlanConfigStep = i
218- break
219- }
220- }
221-
222220 // Process all steps
223221 for i , step := range c .Steps {
224- // If this is the last non-plan config step, add our TGC check
225- if i == lastNonPlanConfigStep {
222+ // If this is a non-plan config step, add our TGC check
223+ if step . Config != "" && ! step . PlanOnly {
226224 // Parse resources from the config
227225 resources := parseResources (step .Config )
228226
@@ -246,6 +244,7 @@ func extendWithTGCData(t *testing.T, c resource.TestCase) resource.TestCase {
246244 // Create the consolidated TGC payload
247245 tgcPayload := TgcMetadataPayload {
248246 TestName : t .Name (),
247+ StepNumber : i + 1 , // Step number starts from 1
249248 RawConfig : step .Config ,
250249 ResourceMetadata : resourceMetadata ,
251250 }
0 commit comments