Skip to content

Commit fc82b3e

Browse files
committed
feat(iaas): log trace id
1 parent b5f82e7 commit fc82b3e

File tree

35 files changed

+177
-0
lines changed

35 files changed

+177
-0
lines changed

stackit/internal/core/core.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"github.com/stackitcloud/stackit-sdk-go/core/runtime"
1011

1112
"github.com/hashicorp/terraform-plugin-framework/diag"
1213
"github.com/hashicorp/terraform-plugin-log/tflog"
@@ -102,6 +103,9 @@ func DiagsToError(diags diag.Diagnostics) error {
102103

103104
// LogAndAddError Logs the error and adds it to the diags
104105
func LogAndAddError(ctx context.Context, diags *diag.Diagnostics, summary, detail string) {
106+
if traceId := runtime.GetTraceId(ctx); traceId != "" {
107+
detail = fmt.Sprintf("%s\nTrace ID: %q", detail, traceId)
108+
}
105109
tflog.Error(ctx, fmt.Sprintf("%s | %s", summary, detail))
106110
diags.AddError(summary, detail)
107111
}
@@ -125,3 +129,17 @@ func LogAndAddErrorBeta(ctx context.Context, diags *diag.Diagnostics, name strin
125129
tflog.Error(ctx, fmt.Sprintf("%s | %s", errTitle, errContent))
126130
diags.AddError(errTitle, errContent)
127131
}
132+
133+
func InitProviderContext(ctx context.Context) context.Context {
134+
// Capture http response to get trace-id
135+
var httpResp *http.Response
136+
ctx = runtime.WithCaptureHTTPResponse(ctx, &httpResp)
137+
138+
return ctx
139+
}
140+
141+
func LogResponse(ctx context.Context) context.Context {
142+
// Logs the trace-id of the request
143+
ctx = tflog.SetField(ctx, "x-trace-id", runtime.GetTraceId(ctx))
144+
return ctx
145+
}

stackit/internal/services/iaas/affinitygroup/datasource.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ func (d *affinityGroupDatasource) Read(ctx context.Context, req datasource.ReadR
118118
}
119119
projectId := model.ProjectId.ValueString()
120120
affinityGroupId := model.AffinityGroupId.ValueString()
121+
ctx = core.InitProviderContext(ctx)
121122
ctx = tflog.SetField(ctx, "project_id", projectId)
122123
ctx = tflog.SetField(ctx, "affinity_group_id", affinityGroupId)
123124

@@ -136,6 +137,7 @@ func (d *affinityGroupDatasource) Read(ctx context.Context, req datasource.ReadR
136137
resp.State.RemoveResource(ctx)
137138
return
138139
}
140+
ctx = core.LogResponse(ctx)
139141

140142
err = mapFields(ctx, affinityGroupResp, &model)
141143
if err != nil {

stackit/internal/services/iaas/affinitygroup/resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ func (r *affinityGroupResource) Create(ctx context.Context, req resource.CreateR
154154
return
155155
}
156156
projectId := model.ProjectId.ValueString()
157+
ctx = core.InitProviderContext(ctx)
157158
ctx = tflog.SetField(ctx, "project_id", projectId)
158159

159160
// Create new affinityGroup
@@ -167,6 +168,7 @@ func (r *affinityGroupResource) Create(ctx context.Context, req resource.CreateR
167168
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating affinity group", fmt.Sprintf("Calling API: %v", err))
168169
return
169170
}
171+
ctx = core.LogResponse(ctx)
170172
ctx = tflog.SetField(ctx, "affinity_group_id", affinityGroupResp.Id)
171173

172174
// Map response body to schema
@@ -194,6 +196,7 @@ func (r *affinityGroupResource) Read(ctx context.Context, req resource.ReadReque
194196
}
195197
projectId := model.ProjectId.ValueString()
196198
affinityGroupId := model.AffinityGroupId.ValueString()
199+
ctx = core.InitProviderContext(ctx)
197200
ctx = tflog.SetField(ctx, "project_id", projectId)
198201
ctx = tflog.SetField(ctx, "affinity_group_id", affinityGroupId)
199202

@@ -207,6 +210,7 @@ func (r *affinityGroupResource) Read(ctx context.Context, req resource.ReadReque
207210
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading affinity group", fmt.Sprintf("Call API: %v", err))
208211
return
209212
}
213+
ctx = core.LogResponse(ctx)
210214

211215
err = mapFields(ctx, affinityGroupResp, &model)
212216
if err != nil {
@@ -237,6 +241,7 @@ func (r *affinityGroupResource) Delete(ctx context.Context, req resource.DeleteR
237241

238242
projectId := model.ProjectId.ValueString()
239243
affinityGroupId := model.AffinityGroupId.ValueString()
244+
ctx = core.InitProviderContext(ctx)
240245
ctx = tflog.SetField(ctx, "project_id", projectId)
241246
ctx = tflog.SetField(ctx, "affinity_group_id", affinityGroupId)
242247

@@ -246,6 +251,7 @@ func (r *affinityGroupResource) Delete(ctx context.Context, req resource.DeleteR
246251
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting affinity group", fmt.Sprintf("Calling API: %v", err))
247252
return
248253
}
254+
ctx = core.LogResponse(ctx)
249255

250256
tflog.Info(ctx, "Affinity group deleted")
251257
}

stackit/internal/services/iaas/image/datasource.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ func (r *imageDataSource) Read(ctx context.Context, req datasource.ReadRequest,
213213
}
214214
projectId := model.ProjectId.ValueString()
215215
imageId := model.ImageId.ValueString()
216+
ctx = core.InitProviderContext(ctx)
216217
ctx = tflog.SetField(ctx, "project_id", projectId)
217218
ctx = tflog.SetField(ctx, "image_id", imageId)
218219

@@ -231,6 +232,7 @@ func (r *imageDataSource) Read(ctx context.Context, req datasource.ReadRequest,
231232
resp.State.RemoveResource(ctx)
232233
return
233234
}
235+
ctx = core.LogResponse(ctx)
234236

235237
// Map response body to schema
236238
err = mapDataSourceFields(ctx, imageResp, &model)

stackit/internal/services/iaas/image/resource.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ func (r *imageResource) Create(ctx context.Context, req resource.CreateRequest,
378378
}
379379

380380
projectId := model.ProjectId.ValueString()
381+
ctx = core.InitProviderContext(ctx)
381382
ctx = tflog.SetField(ctx, "project_id", projectId)
382383

383384
// Generate API request body from model
@@ -393,6 +394,7 @@ func (r *imageResource) Create(ctx context.Context, req resource.CreateRequest,
393394
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating image", fmt.Sprintf("Calling API: %v", err))
394395
return
395396
}
397+
ctx = core.LogResponse(ctx)
396398
ctx = tflog.SetField(ctx, "image_id", *imageCreateResp.Id)
397399

398400
// Get the image object, as the create response does not contain all fields
@@ -458,6 +460,7 @@ func (r *imageResource) Read(ctx context.Context, req resource.ReadRequest, resp
458460
}
459461
projectId := model.ProjectId.ValueString()
460462
imageId := model.ImageId.ValueString()
463+
ctx = core.InitProviderContext(ctx)
461464
ctx = tflog.SetField(ctx, "project_id", projectId)
462465
ctx = tflog.SetField(ctx, "image_id", imageId)
463466

@@ -471,6 +474,7 @@ func (r *imageResource) Read(ctx context.Context, req resource.ReadRequest, resp
471474
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading image", fmt.Sprintf("Calling API: %v", err))
472475
return
473476
}
477+
ctx = core.LogResponse(ctx)
474478

475479
// Map response body to schema
476480
err = mapFields(ctx, imageResp, &model)
@@ -498,6 +502,7 @@ func (r *imageResource) Update(ctx context.Context, req resource.UpdateRequest,
498502
}
499503
projectId := model.ProjectId.ValueString()
500504
imageId := model.ImageId.ValueString()
505+
ctx = core.InitProviderContext(ctx)
501506
ctx = tflog.SetField(ctx, "project_id", projectId)
502507
ctx = tflog.SetField(ctx, "image_id", imageId)
503508

@@ -521,6 +526,7 @@ func (r *imageResource) Update(ctx context.Context, req resource.UpdateRequest,
521526
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating image", fmt.Sprintf("Calling API: %v", err))
522527
return
523528
}
529+
ctx = core.LogResponse(ctx)
524530

525531
err = mapFields(ctx, updatedImage, &model)
526532
if err != nil {
@@ -547,6 +553,7 @@ func (r *imageResource) Delete(ctx context.Context, req resource.DeleteRequest,
547553

548554
projectId := model.ProjectId.ValueString()
549555
imageId := model.ImageId.ValueString()
556+
ctx = core.InitProviderContext(ctx)
550557
ctx = tflog.SetField(ctx, "project_id", projectId)
551558
ctx = tflog.SetField(ctx, "image_id", imageId)
552559

@@ -556,6 +563,7 @@ func (r *imageResource) Delete(ctx context.Context, req resource.DeleteRequest,
556563
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting image", fmt.Sprintf("Calling API: %v", err))
557564
return
558565
}
566+
ctx = core.LogResponse(ctx)
559567
_, err = wait.DeleteImageWaitHandler(ctx, r.client, projectId, imageId).WaitWithContext(ctx)
560568
if err != nil {
561569
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting image", fmt.Sprintf("image deletion waiting: %v", err))

stackit/internal/services/iaas/imagev2/datasource.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ func (d *imageDataV2Source) Read(ctx context.Context, req datasource.ReadRequest
370370
}
371371
}
372372

373+
ctx = core.InitProviderContext(ctx)
373374
ctx = tflog.SetField(ctx, "project_id", projectID)
374375
ctx = tflog.SetField(ctx, "image_id", imageID)
375376
ctx = tflog.SetField(ctx, "name", name)
@@ -391,6 +392,7 @@ func (d *imageDataV2Source) Read(ctx context.Context, req datasource.ReadRequest
391392
resp.State.RemoveResource(ctx)
392393
return
393394
}
395+
ctx = core.LogResponse(ctx)
394396
} else {
395397
// Case 2: Lookup by name or name_regex
396398

@@ -410,6 +412,7 @@ func (d *imageDataV2Source) Read(ctx context.Context, req datasource.ReadRequest
410412
utils.LogError(ctx, &resp.Diagnostics, err, "List images", "Unable to fetch images", nil)
411413
return
412414
}
415+
ctx = core.LogResponse(ctx)
413416

414417
// Step 1: Match images by name or regular expression (name or name_regex, if provided)
415418
var matchedImages []*iaas.Image

stackit/internal/services/iaas/keypair/datasource.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func (r *keyPairDataSource) Read(ctx context.Context, req datasource.ReadRequest
9292
return
9393
}
9494
name := model.Name.ValueString()
95+
ctx = core.InitProviderContext(ctx)
9596
ctx = tflog.SetField(ctx, "name", name)
9697

9798
keypairResp, err := r.client.GetKeyPair(ctx, name).Execute()
@@ -107,6 +108,7 @@ func (r *keyPairDataSource) Read(ctx context.Context, req datasource.ReadRequest
107108
resp.State.RemoveResource(ctx)
108109
return
109110
}
111+
ctx = core.LogResponse(ctx)
110112

111113
// Map response body to schema
112114
err = mapFields(ctx, keypairResp, &model)

stackit/internal/services/iaas/keypair/resource.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ func (r *keyPairResource) Create(ctx context.Context, req resource.CreateRequest
146146
}
147147

148148
name := model.Name.ValueString()
149+
ctx = core.InitProviderContext(ctx)
149150
ctx = tflog.SetField(ctx, "name", name)
150151

151152
// Generate API request body from model
@@ -162,6 +163,7 @@ func (r *keyPairResource) Create(ctx context.Context, req resource.CreateRequest
162163
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating key pair", fmt.Sprintf("Calling API: %v", err))
163164
return
164165
}
166+
ctx = core.LogResponse(ctx)
165167

166168
// Map response body to schema
167169
err = mapFields(ctx, keyPair, &model)
@@ -187,6 +189,7 @@ func (r *keyPairResource) Read(ctx context.Context, req resource.ReadRequest, re
187189
return
188190
}
189191
name := model.Name.ValueString()
192+
ctx = core.InitProviderContext(ctx)
190193
ctx = tflog.SetField(ctx, "name", name)
191194

192195
keyPairResp, err := r.client.GetKeyPair(ctx, name).Execute()
@@ -199,6 +202,7 @@ func (r *keyPairResource) Read(ctx context.Context, req resource.ReadRequest, re
199202
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading key pair", fmt.Sprintf("Calling API: %v", err))
200203
return
201204
}
205+
ctx = core.LogResponse(ctx)
202206

203207
// Map response body to schema
204208
err = mapFields(ctx, keyPairResp, &model)
@@ -225,6 +229,7 @@ func (r *keyPairResource) Update(ctx context.Context, req resource.UpdateRequest
225229
return
226230
}
227231
name := model.Name.ValueString()
232+
ctx = core.InitProviderContext(ctx)
228233
ctx = tflog.SetField(ctx, "name", name)
229234

230235
// Retrieve values from state
@@ -247,6 +252,7 @@ func (r *keyPairResource) Update(ctx context.Context, req resource.UpdateRequest
247252
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating key pair", fmt.Sprintf("Calling API: %v", err))
248253
return
249254
}
255+
ctx = core.LogResponse(ctx)
250256

251257
err = mapFields(ctx, updatedKeyPair, &model)
252258
if err != nil {
@@ -272,6 +278,7 @@ func (r *keyPairResource) Delete(ctx context.Context, req resource.DeleteRequest
272278
}
273279

274280
name := model.Name.ValueString()
281+
ctx = core.InitProviderContext(ctx)
275282
ctx = tflog.SetField(ctx, "name", name)
276283

277284
// Delete existing key pair
@@ -280,6 +287,7 @@ func (r *keyPairResource) Delete(ctx context.Context, req resource.DeleteRequest
280287
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting key pair", fmt.Sprintf("Calling API: %v", err))
281288
return
282289
}
290+
ctx = core.LogResponse(ctx)
283291

284292
tflog.Info(ctx, "Key pair deleted")
285293
}

stackit/internal/services/iaas/machinetype/datasource.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ func (d *machineTypeDataSource) Read(ctx context.Context, req datasource.ReadReq
144144
projectId := model.ProjectId.ValueString()
145145
sortAscending := model.SortAscending.ValueBool()
146146

147+
ctx = core.InitProviderContext(ctx)
147148
ctx = tflog.SetField(ctx, "project_id", projectId)
148149
ctx = tflog.SetField(ctx, "filter_is_null", model.Filter.IsNull())
149150
ctx = tflog.SetField(ctx, "filter_is_unknown", model.Filter.IsUnknown())
@@ -165,6 +166,7 @@ func (d *machineTypeDataSource) Read(ctx context.Context, req datasource.ReadReq
165166
resp.State.RemoveResource(ctx)
166167
return
167168
}
169+
ctx = core.LogResponse(ctx)
168170

169171
if apiResp.Items == nil || len(*apiResp.Items) == 0 {
170172
core.LogAndAddWarning(ctx, &resp.Diagnostics, "No machine types found", "No matching machine types.")

stackit/internal/services/iaas/network/utils/v1network/datasource.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func DatasourceRead(ctx context.Context, req datasource.ReadRequest, resp *datas
2323
if resp.Diagnostics.HasError() {
2424
return
2525
}
26+
ctx = core.InitProviderContext(ctx)
2627
projectId := model.ProjectId.ValueString()
2728
networkId := model.NetworkId.ValueString()
2829
ctx = tflog.SetField(ctx, "project_id", projectId)
@@ -43,6 +44,7 @@ func DatasourceRead(ctx context.Context, req datasource.ReadRequest, resp *datas
4344
resp.State.RemoveResource(ctx)
4445
return
4546
}
47+
ctx = core.LogResponse(ctx)
4648

4749
err = mapDataSourceFields(ctx, networkResp, &model)
4850
if err != nil {

0 commit comments

Comments
 (0)