@@ -24,6 +24,7 @@ import (
24
24
"regexp"
25
25
"strings"
26
26
"testing"
27
+ "time"
27
28
28
29
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
29
30
"github.com/hashicorp/terraform-plugin-testing/terraform"
@@ -43,11 +44,14 @@ type ImportMetadata struct {
43
44
IgnoredFields []string `json:"ignored_fields,omitempty"`
44
45
}
45
46
47
+ // The metadata for each step in one test
46
48
type TgcMetadataPayload struct {
47
49
TestName string `json:"test_name"`
50
+ StepNumber int `json:"step_number"`
48
51
RawConfig string `json:"raw_config"`
49
52
ResourceMetadata map [string ]ResourceMetadata `json:"resource_metadata"`
50
53
PrimaryResource string `json:"primary_resource"`
54
+ CaiReadTime time.Time `json:"cai_read_time"`
51
55
}
52
56
53
57
// 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) {
88
92
// CollectAllTgcMetadata collects metadata for all resources in a test step
89
93
func CollectAllTgcMetadata (tgcPayload TgcMetadataPayload ) resource.TestCheckFunc {
90
94
return func (s * terraform.State ) error {
95
+ tgcPayload .CaiReadTime = time .Now ()
96
+
91
97
projectId := envvar .GetTestProjectFromEnv ()
92
98
projectNumber := envvar .GetTestProjectNumberFromEnv ()
93
99
@@ -134,12 +140,14 @@ func CollectAllTgcMetadata(tgcPayload TgcMetadataPayload) resource.TestCheckFunc
134
140
tgcPayload .ResourceMetadata [address ] = metadata
135
141
}
136
142
143
+ log .Printf ("[DEBUG] tgcPayload caireadtime %s" , tgcPayload .CaiReadTime )
144
+
137
145
// Encode the entire payload to base64 JSON
138
146
encodedData , err := encodeToBase64JSON (tgcPayload )
139
147
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 )
141
149
} 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 )
143
151
}
144
152
145
153
return nil
@@ -209,20 +217,10 @@ func determineImportMetadata(steps []resource.TestStep, currentStepIndex int, re
209
217
func extendWithTGCData (t * testing.T , c resource.TestCase ) resource.TestCase {
210
218
var updatedSteps []resource.TestStep
211
219
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
-
222
220
// Process all steps
223
221
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 {
226
224
// Parse resources from the config
227
225
resources := parseResources (step .Config )
228
226
@@ -246,6 +244,7 @@ func extendWithTGCData(t *testing.T, c resource.TestCase) resource.TestCase {
246
244
// Create the consolidated TGC payload
247
245
tgcPayload := TgcMetadataPayload {
248
246
TestName : t .Name (),
247
+ StepNumber : i + 1 , // Step number starts from 1
249
248
RawConfig : step .Config ,
250
249
ResourceMetadata : resourceMetadata ,
251
250
}
0 commit comments