Skip to content

Commit b8e4c89

Browse files
committed
- add configure client
- add trace id logging - fix acc tests
1 parent 9851b3b commit b8e4c89

File tree

10 files changed

+234
-154
lines changed

10 files changed

+234
-154
lines changed

stackit/internal/services/sfs/export-policy/datasource.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1212
"github.com/hashicorp/terraform-plugin-framework/types"
1313
"github.com/hashicorp/terraform-plugin-log/tflog"
14-
"github.com/stackitcloud/stackit-sdk-go/core/config"
1514
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1615
"github.com/stackitcloud/stackit-sdk-go/services/sfs"
1716
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
1817
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
1918
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features"
19+
sfsUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/sfs/utils"
2020
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
2121
)
2222

@@ -56,26 +56,10 @@ func (d *exportPolicyDataSource) Configure(ctx context.Context, req datasource.C
5656
datasourceBetaCheckDone = true
5757
}
5858

59-
var apiClient *sfs.APIClient
60-
var err error
61-
if d.providerData.SfsCustomEndpoint != "" {
62-
ctx = tflog.SetField(ctx, "sfs_custom_endpoint", d.providerData.SfsCustomEndpoint)
63-
apiClient, err = sfs.NewAPIClient(
64-
config.WithCustomAuth(d.providerData.RoundTripper),
65-
config.WithEndpoint(d.providerData.SfsCustomEndpoint),
66-
)
67-
} else {
68-
apiClient, err = sfs.NewAPIClient(
69-
config.WithCustomAuth(d.providerData.RoundTripper),
70-
config.WithRegion(d.providerData.GetRegion()),
71-
)
72-
}
73-
74-
if err != nil {
75-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Configuring client: %v. This is an error related to the provider configuration, not to the datasource configuration", err))
59+
apiClient := sfsUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
60+
if resp.Diagnostics.HasError() {
7661
return
7762
}
78-
7963
d.client = apiClient
8064
tflog.Info(ctx, "SFS client configured")
8165
}
@@ -99,6 +83,8 @@ func (d *exportPolicyDataSource) Read(ctx context.Context, req datasource.ReadRe
9983
ctx = tflog.SetField(ctx, "policy_id", exportPolicyId)
10084
ctx = tflog.SetField(ctx, "region", region)
10185

86+
ctx = core.InitProviderContext(ctx)
87+
10288
// get export policy
10389
exportPolicyResp, err := d.client.GetShareExportPolicy(ctx, projectId, region, exportPolicyId).Execute()
10490
if err != nil {
@@ -112,7 +98,8 @@ func (d *exportPolicyDataSource) Read(ctx context.Context, req datasource.ReadRe
11298
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading export policy", fmt.Sprintf("Calling API to get export policy: %v", err))
11399
return
114100
}
115-
// TODO: log traceId
101+
102+
ctx = core.LogResponse(ctx)
116103

117104
// map export policy
118105
err = mapFields(ctx, exportPolicyResp, &model, region)

stackit/internal/services/sfs/export-policy/resource.go

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import (
2121
"github.com/hashicorp/terraform-plugin-framework/types"
2222
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
2323
"github.com/hashicorp/terraform-plugin-log/tflog"
24-
"github.com/stackitcloud/stackit-sdk-go/core/config"
2524
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
2625
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
2726
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
2827
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features"
28+
sfsUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/sfs/utils"
2929
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
3030
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
3131

@@ -141,26 +141,10 @@ func (r *exportPolicyResource) Configure(ctx context.Context, req resource.Confi
141141
resourceBetaCheckDone = true
142142
}
143143

144-
var apiClient *sfs.APIClient
145-
var err error
146-
if r.providerData.SfsCustomEndpoint != "" {
147-
ctx = tflog.SetField(ctx, "sfs_custom_endpoint", r.providerData.SfsCustomEndpoint)
148-
apiClient, err = sfs.NewAPIClient(
149-
config.WithCustomAuth(r.providerData.RoundTripper),
150-
config.WithEndpoint(r.providerData.SfsCustomEndpoint),
151-
)
152-
} else {
153-
apiClient, err = sfs.NewAPIClient(
154-
config.WithCustomAuth(r.providerData.RoundTripper),
155-
config.WithRegion(r.providerData.GetRegion()),
156-
)
157-
}
158-
159-
if err != nil {
160-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration", err))
144+
apiClient := sfsUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
145+
if resp.Diagnostics.HasError() {
161146
return
162147
}
163-
164148
r.client = apiClient
165149
tflog.Info(ctx, "SFS client configured")
166150
}
@@ -281,6 +265,8 @@ func (r *exportPolicyResource) Create(ctx context.Context, req resource.CreateRe
281265
ctx = tflog.SetField(ctx, "project_id", projectId)
282266
ctx = tflog.SetField(ctx, "region", region)
283267

268+
ctx = core.InitProviderContext(ctx)
269+
284270
var rules = []rulesModel{}
285271
if !(model.Rules.IsNull() || model.Rules.IsUnknown()) {
286272
diags = model.Rules.ElementsAs(ctx, &rules, false)
@@ -301,7 +287,9 @@ func (r *exportPolicyResource) Create(ctx context.Context, req resource.CreateRe
301287
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating export policy", fmt.Sprintf("Calling API: %v", err))
302288
return
303289
}
304-
// TODO: log traceId
290+
291+
ctx = core.LogResponse(ctx)
292+
305293
if createResp == nil || createResp.ShareExportPolicy == nil || createResp.ShareExportPolicy.Id == nil {
306294
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating export policy", "response did not contain an ID")
307295
return
@@ -353,6 +341,8 @@ func (r *exportPolicyResource) Read(ctx context.Context, req resource.ReadReques
353341
ctx = tflog.SetField(ctx, "policy_id", exportPolicyId)
354342
ctx = tflog.SetField(ctx, "region", region)
355343

344+
ctx = core.InitProviderContext(ctx)
345+
356346
// get export policy
357347
exportPolicyResp, err := r.client.GetShareExportPolicy(ctx, projectId, region, exportPolicyId).Execute()
358348
if err != nil {
@@ -366,7 +356,8 @@ func (r *exportPolicyResource) Read(ctx context.Context, req resource.ReadReques
366356
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading export policy", fmt.Sprintf("Calling API to get export policy: %v", err))
367357
return
368358
}
369-
// TODO: log traceId
359+
360+
ctx = core.LogResponse(ctx)
370361

371362
// map export policy
372363
err = mapFields(ctx, exportPolicyResp, &model, region)
@@ -400,6 +391,8 @@ func (r *exportPolicyResource) Update(ctx context.Context, req resource.UpdateRe
400391
ctx = tflog.SetField(ctx, "policy_id", exportPolicyId)
401392
ctx = tflog.SetField(ctx, "region", region)
402393

394+
ctx = core.InitProviderContext(ctx)
395+
403396
var rules = []rulesModel{}
404397
if !(model.Rules.IsNull() || model.Rules.IsUnknown()) {
405398
diags = model.Rules.ElementsAs(ctx, &rules, false)
@@ -420,7 +413,8 @@ func (r *exportPolicyResource) Update(ctx context.Context, req resource.UpdateRe
420413
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating export policy", fmt.Sprintf("Calling API to update export policy: %v", err))
421414
return
422415
}
423-
// TODO: log traceId
416+
417+
ctx = core.LogResponse(ctx)
424418

425419
// get export policy
426420
exportPolicyResp, err := r.client.GetShareExportPolicy(ctx, projectId, region, exportPolicyId).Execute()
@@ -461,11 +455,14 @@ func (r *exportPolicyResource) Delete(ctx context.Context, req resource.DeleteRe
461455
ctx = tflog.SetField(ctx, "policy_id", exportPolicyId)
462456
ctx = tflog.SetField(ctx, "region", region)
463457

458+
ctx = core.InitProviderContext(ctx)
459+
464460
_, err := r.client.DeleteShareExportPolicy(ctx, projectId, region, exportPolicyId).Execute()
465461
if err != nil {
466462
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting export policy", fmt.Sprintf("Calling API: %v", err))
467463
}
468-
// TODO: log traceId
464+
465+
ctx = core.LogResponse(ctx)
469466

470467
tflog.Info(ctx, "SFS export policy delete")
471468
}

stackit/internal/services/sfs/resourcepool/datasource.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import (
1414
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1515
"github.com/hashicorp/terraform-plugin-framework/types"
1616
"github.com/hashicorp/terraform-plugin-log/tflog"
17-
"github.com/stackitcloud/stackit-sdk-go/core/config"
1817
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1918
"github.com/stackitcloud/stackit-sdk-go/services/sfs"
2019
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
2120
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
2221
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features"
22+
sfsUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/sfs/utils"
2323
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
2424
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
2525
)
@@ -69,26 +69,10 @@ func (r *resourcePoolDataSource) Configure(ctx context.Context, req datasource.C
6969
datasourceBetaCheckDone = true
7070
}
7171

72-
var apiClient *sfs.APIClient
73-
var err error
74-
if r.providerData.SfsCustomEndpoint != "" {
75-
ctx = tflog.SetField(ctx, "sfs_custom_endpoint", r.providerData.SfsCustomEndpoint)
76-
apiClient, err = sfs.NewAPIClient(
77-
config.WithCustomAuth(r.providerData.RoundTripper),
78-
config.WithEndpoint(r.providerData.SfsCustomEndpoint),
79-
)
80-
} else {
81-
apiClient, err = sfs.NewAPIClient(
82-
config.WithCustomAuth(r.providerData.RoundTripper),
83-
config.WithRegion(r.providerData.GetRegion()),
84-
)
85-
}
86-
87-
if err != nil {
88-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Configuring client: %v. This is an error related to the provider configuration, not to the datasource configuration", err))
72+
apiClient := sfsUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
73+
if resp.Diagnostics.HasError() {
8974
return
9075
}
91-
9276
r.client = apiClient
9377
tflog.Info(ctx, "SFS client configured")
9478
}
@@ -113,6 +97,8 @@ func (r *resourcePoolDataSource) Read(ctx context.Context, req datasource.ReadRe
11397
ctx = tflog.SetField(ctx, "resource_pool_id", resourcePoolId)
11498
ctx = tflog.SetField(ctx, "region", region)
11599

100+
ctx = core.InitProviderContext(ctx)
101+
116102
response, err := r.client.GetResourcePoolExecute(ctx, projectId, region, resourcePoolId)
117103
if err != nil {
118104
var openapiError *oapierror.GenericOpenAPIError
@@ -125,7 +111,8 @@ func (r *resourcePoolDataSource) Read(ctx context.Context, req datasource.ReadRe
125111
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading resource pool", fmt.Sprintf("Calling API: %v", err))
126112
return
127113
}
128-
// TODO: log traceId
114+
115+
ctx = core.LogResponse(ctx)
129116

130117
// Map response body to schema
131118
err = mapDataSourceFields(ctx, region, response.ResourcePool, &model)

stackit/internal/services/sfs/resourcepool/resource.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ import (
1919
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
2020
"github.com/hashicorp/terraform-plugin-framework/types"
2121
"github.com/hashicorp/terraform-plugin-log/tflog"
22-
"github.com/stackitcloud/stackit-sdk-go/core/config"
2322
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
2423
"github.com/stackitcloud/stackit-sdk-go/services/sfs"
2524
"github.com/stackitcloud/stackit-sdk-go/services/sfs/wait"
2625
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
2726
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
2827
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features"
28+
sfsUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/sfs/utils"
2929
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
3030
coreutils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
3131
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
@@ -121,26 +121,10 @@ func (r *resourcePoolResource) Configure(ctx context.Context, req resource.Confi
121121
datasourceBetaCheckDone = true
122122
}
123123

124-
var apiClient *sfs.APIClient
125-
var err error
126-
if r.providerData.SfsCustomEndpoint != "" {
127-
ctx = tflog.SetField(ctx, "sfs_custom_endpoint", r.providerData.SfsCustomEndpoint)
128-
apiClient, err = sfs.NewAPIClient(
129-
config.WithCustomAuth(r.providerData.RoundTripper),
130-
config.WithEndpoint(r.providerData.SfsCustomEndpoint),
131-
)
132-
} else {
133-
apiClient, err = sfs.NewAPIClient(
134-
config.WithCustomAuth(r.providerData.RoundTripper),
135-
config.WithRegion(r.providerData.GetRegion()),
136-
)
137-
}
138-
139-
if err != nil {
140-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration", err))
124+
apiClient := sfsUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
125+
if resp.Diagnostics.HasError() {
141126
return
142127
}
143-
144128
r.client = apiClient
145129
tflog.Info(ctx, "SFS client configured")
146130
}
@@ -248,6 +232,8 @@ func (r *resourcePoolResource) Create(ctx context.Context, req resource.CreateRe
248232
ctx = tflog.SetField(ctx, "project_id", projectId)
249233
ctx = tflog.SetField(ctx, "region", region)
250234

235+
ctx = core.InitProviderContext(ctx)
236+
251237
payload, err := toCreatePayload(&model)
252238
if err != nil {
253239
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating resource pool", fmt.Sprintf("Cannot create payload: %v", err))
@@ -262,7 +248,9 @@ func (r *resourcePoolResource) Create(ctx context.Context, req resource.CreateRe
262248
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating resource pool", fmt.Sprintf("Calling API: %v", err))
263249
return
264250
}
265-
// TODO: log traceId
251+
252+
ctx = core.LogResponse(ctx)
253+
266254
if resourcePool == nil || resourcePool.ResourcePool == nil || resourcePool.ResourcePool.Id == nil {
267255
core.LogAndAddError(ctx, &resp.Diagnostics, "error creating resource pool", "Calling API: Incomplete response (id missing)")
268256
return
@@ -330,6 +318,8 @@ func (r *resourcePoolResource) Read(ctx context.Context, req resource.ReadReques
330318
ctx = tflog.SetField(ctx, "resource_pool_id", resourcePoolId)
331319
ctx = tflog.SetField(ctx, "region", region)
332320

321+
ctx = core.InitProviderContext(ctx)
322+
333323
response, err := r.client.GetResourcePoolExecute(ctx, projectId, region, resourcePoolId)
334324
if err != nil {
335325
var openapiError *oapierror.GenericOpenAPIError
@@ -342,7 +332,8 @@ func (r *resourcePoolResource) Read(ctx context.Context, req resource.ReadReques
342332
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading resource pool", fmt.Sprintf("Calling API: %v", err))
343333
return
344334
}
345-
// TODO: log traceId
335+
336+
ctx = core.LogResponse(ctx)
346337

347338
// Map response body to schema
348339
err = mapFields(ctx, region, response.ResourcePool, &model)
@@ -375,6 +366,8 @@ func (r *resourcePoolResource) Update(ctx context.Context, req resource.UpdateRe
375366
ctx = tflog.SetField(ctx, "resource_pool_id", resourcePoolId)
376367
ctx = tflog.SetField(ctx, "region", region)
377368

369+
ctx = core.InitProviderContext(ctx)
370+
378371
// Retrieve values from state
379372
var stateModel Model
380373
diags = req.State.Get(ctx, &stateModel)
@@ -403,7 +396,9 @@ func (r *resourcePoolResource) Update(ctx context.Context, req resource.UpdateRe
403396
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating resource pool", fmt.Sprintf("Calling API: %v", err))
404397
return
405398
}
406-
// TODO: log traceId
399+
400+
ctx = core.LogResponse(ctx)
401+
407402
// the responses of create and update are not compatible, so we can't use a unified
408403
// mapFields function. Therefore, we issue a GET request after the create
409404
// to get a compatible structure
@@ -447,13 +442,17 @@ func (r *resourcePoolResource) Delete(ctx context.Context, req resource.DeleteRe
447442
ctx = tflog.SetField(ctx, "resource_pool_id", resourcePoolId)
448443
ctx = tflog.SetField(ctx, "region", region)
449444

445+
ctx = core.InitProviderContext(ctx)
446+
450447
// Delete existing resource pool
451448
_, err := r.client.DeleteResourcePoolExecute(ctx, projectId, region, resourcePoolId)
452449
if err != nil {
453450
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting resource pool", fmt.Sprintf("Calling API: %v", err))
454451
return
455452
}
456-
// TODO: log traceId
453+
454+
ctx = core.LogResponse(ctx)
455+
457456
// only delete, if no error occurred
458457
_, err = wait.DeleteResourcePoolWaitHandler(ctx, r.client, projectId, region, resourcePoolId).WaitWithContext(ctx)
459458
if err != nil {

0 commit comments

Comments
 (0)