Skip to content

Commit da67dcc

Browse files
committed
implement review feedback
1 parent fe9686d commit da67dcc

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

.github/docs/contribution-guide/resource.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ func (r *barResource) Create(ctx context.Context, req resource.CreateRequest, re
184184
if resp.Diagnostics.HasError() {
185185
return
186186
}
187+
187188
ctx = core.InitProviderContext(ctx)
189+
188190
projectId := model.ProjectId.ValueString()
189191
region := model.Region.ValueString() // not needed for global APIs
190192
ctx = tflog.SetField(ctx, "project_id", projectId)
@@ -203,6 +205,7 @@ func (r *barResource) Create(ctx context.Context, req resource.CreateRequest, re
203205
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating bar", fmt.Sprintf("Calling API: %v", err))
204206
return
205207
}
208+
206209
ctx = core.LogResponse(ctx)
207210

208211
// only in case the create bar API call is asynchronous (Make sure to include *ALL* fields which are part of the
@@ -246,7 +249,9 @@ func (r *barResource) Read(ctx context.Context, req resource.ReadRequest, resp *
246249
if resp.Diagnostics.HasError() {
247250
return
248251
}
252+
249253
ctx = core.InitProviderContext(ctx)
254+
250255
projectId := model.ProjectId.ValueString()
251256
region := r.providerData.GetRegionWithOverride(model.Region)
252257
barId := model.BarId.ValueString()
@@ -259,6 +264,7 @@ func (r *barResource) Read(ctx context.Context, req resource.ReadRequest, resp *
259264
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading bar", fmt.Sprintf("Calling API: %v", err))
260265
return
261266
}
267+
262268
ctx = core.LogResponse(ctx)
263269

264270
// Map response body to schema
@@ -289,7 +295,9 @@ func (r *barResource) Delete(ctx context.Context, req resource.DeleteRequest, re
289295
if resp.Diagnostics.HasError() {
290296
return
291297
}
298+
292299
ctx = core.InitProviderContext(ctx)
300+
293301
projectId := model.ProjectId.ValueString()
294302
region := model.Region.ValueString()
295303
barId := model.BarId.ValueString()
@@ -302,6 +310,7 @@ func (r *barResource) Delete(ctx context.Context, req resource.DeleteRequest, re
302310
if err != nil {
303311
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting bar", fmt.Sprintf("Calling API: %v", err))
304312
}
313+
305314
ctx = core.LogResponse(ctx)
306315

307316
// only in case the bar delete API endpoint is asynchronous: use a wait handler to wait for the delete operation to complete

stackit/internal/core/core.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func LogAndAddError(ctx context.Context, diags *diag.Diagnostics, summary, detai
106106
if traceId := runtime.GetTraceId(ctx); traceId != "" {
107107
detail = fmt.Sprintf("%s\nTrace ID: %q", detail, traceId)
108108
}
109+
109110
tflog.Error(ctx, fmt.Sprintf("%s | %s", summary, detail))
110111
diags.AddError(summary, detail)
111112
}
@@ -115,6 +116,7 @@ func LogAndAddWarning(ctx context.Context, diags *diag.Diagnostics, summary, det
115116
if traceId := runtime.GetTraceId(ctx); traceId != "" {
116117
detail = fmt.Sprintf("%s\nTrace ID: %q", detail, traceId)
117118
}
119+
118120
tflog.Warn(ctx, fmt.Sprintf("%s | %s", summary, detail))
119121
diags.AddWarning(summary, detail)
120122
}
@@ -133,18 +135,19 @@ func LogAndAddErrorBeta(ctx context.Context, diags *diag.Diagnostics, name strin
133135
diags.AddError(errTitle, errContent)
134136
}
135137

138+
// InitProviderContext extends the context to capture the http response
136139
func InitProviderContext(ctx context.Context) context.Context {
137140
// Capture http response to get trace-id
138141
var httpResp *http.Response
139-
ctx = runtime.WithCaptureHTTPResponse(ctx, &httpResp)
140-
141-
return ctx
142+
return runtime.WithCaptureHTTPResponse(ctx, &httpResp)
142143
}
143144

145+
// LogResponse logs the trace-id of the last request
144146
func LogResponse(ctx context.Context) context.Context {
145147
// Logs the trace-id of the request
146148
traceId := runtime.GetTraceId(ctx)
147149
ctx = tflog.SetField(ctx, "x-trace-id", traceId)
150+
148151
tflog.Info(ctx, "response data", map[string]interface{}{
149152
"x-trace-id": traceId,
150153
})

0 commit comments

Comments
 (0)