Skip to content

Commit c63222a

Browse files
Peter Paul Schaffrathrubenhoenle
authored andcommitted
chore(provider): refactor sdk client creation to allow mock injection
1 parent e4d4e51 commit c63222a

48 files changed

Lines changed: 887 additions & 460 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"regexp"
88

99
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
10-
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
10+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/clientutils"
1111

1212
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
1313
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
@@ -28,12 +28,16 @@ var (
2828
_ datasource.DataSourceWithConfigure = &affinityGroupDatasource{}
2929
)
3030

31-
func NewAffinityGroupDatasource() datasource.DataSource {
32-
return &affinityGroupDatasource{}
31+
func NewAffinityGroupDatasource(clientFactory clientutils.ClientFactory) datasource.DataSource {
32+
return &affinityGroupDatasource{
33+
clientFactory: clientFactory,
34+
}
3335
}
3436

3537
type affinityGroupDatasource struct {
36-
client *iaas.APIClient
38+
clientFactory clientutils.ClientFactory
39+
40+
client iaas.DefaultAPI
3741
providerData core.ProviderData
3842
}
3943

@@ -44,11 +48,11 @@ func (d *affinityGroupDatasource) Configure(ctx context.Context, req datasource.
4448
return
4549
}
4650

47-
apiClient := iaasUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
51+
d.client = d.clientFactory.NewIaaSV2Client(ctx, &d.providerData, &resp.Diagnostics)
4852
if resp.Diagnostics.HasError() {
4953
return
5054
}
51-
d.client = apiClient
55+
5256
tflog.Info(ctx, "iaas client configured")
5357
}
5458

@@ -133,7 +137,7 @@ func (d *affinityGroupDatasource) Read(ctx context.Context, req datasource.ReadR
133137
ctx = tflog.SetField(ctx, "region", region)
134138
ctx = tflog.SetField(ctx, "affinity_group_id", affinityGroupId)
135139

136-
affinityGroupResp, err := d.client.DefaultAPI.GetAffinityGroup(ctx, projectId, region, affinityGroupId).Execute()
140+
affinityGroupResp, err := d.client.GetAffinityGroup(ctx, projectId, region, affinityGroupId).Execute()
137141
if err != nil {
138142
utils.LogError(
139143
ctx,

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import (
99
"strings"
1010

1111
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
12-
13-
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
12+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/clientutils"
1413

1514
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
1615
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
@@ -47,13 +46,17 @@ type Model struct {
4746
Members types.List `tfsdk:"members"`
4847
}
4948

50-
func NewAffinityGroupResource() resource.Resource {
51-
return &affinityGroupResource{}
49+
func NewAffinityGroupResource(clientFactory clientutils.ClientFactory) resource.Resource {
50+
return &affinityGroupResource{
51+
clientFactory: clientFactory,
52+
}
5253
}
5354

5455
// affinityGroupResource is the resource implementation.
5556
type affinityGroupResource struct {
56-
client *iaas.APIClient
57+
clientFactory clientutils.ClientFactory
58+
59+
client iaas.DefaultAPI
5760
providerData core.ProviderData
5861
}
5962

@@ -100,11 +103,11 @@ func (r *affinityGroupResource) Configure(ctx context.Context, req resource.Conf
100103
return
101104
}
102105

103-
apiClient := iaasUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
106+
r.client = r.clientFactory.NewIaaSV2Client(ctx, &r.providerData, &resp.Diagnostics)
104107
if resp.Diagnostics.HasError() {
105108
return
106109
}
107-
r.client = apiClient
110+
108111
tflog.Info(ctx, "iaas client configured")
109112
}
110113

@@ -210,7 +213,7 @@ func (r *affinityGroupResource) Create(ctx context.Context, req resource.CreateR
210213
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating affinity group", fmt.Sprintf("Creating API payload: %v", err))
211214
return
212215
}
213-
affinityGroupResp, err := r.client.DefaultAPI.CreateAffinityGroup(ctx, projectId, region).CreateAffinityGroupPayload(*payload).Execute()
216+
affinityGroupResp, err := r.client.CreateAffinityGroup(ctx, projectId, region).CreateAffinityGroupPayload(*payload).Execute()
214217
if err != nil {
215218
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating affinity group", fmt.Sprintf("Calling API: %v", err))
216219
return
@@ -258,7 +261,7 @@ func (r *affinityGroupResource) Read(ctx context.Context, req resource.ReadReque
258261
ctx = tflog.SetField(ctx, "region", region)
259262
ctx = tflog.SetField(ctx, "affinity_group_id", affinityGroupId)
260263

261-
affinityGroupResp, err := r.client.DefaultAPI.GetAffinityGroup(ctx, projectId, region, affinityGroupId).Execute()
264+
affinityGroupResp, err := r.client.GetAffinityGroup(ctx, projectId, region, affinityGroupId).Execute()
262265
if err != nil {
263266
var oapiErr *oapierror.GenericOpenAPIError
264267
if errors.As(err, &oapiErr) && oapiErr.StatusCode == http.StatusNotFound {
@@ -309,7 +312,7 @@ func (r *affinityGroupResource) Delete(ctx context.Context, req resource.DeleteR
309312
ctx = tflog.SetField(ctx, "affinity_group_id", affinityGroupId)
310313

311314
// Delete existing affinity group
312-
err := r.client.DefaultAPI.DeleteAffinityGroup(ctx, projectId, region, affinityGroupId).Execute()
315+
err := r.client.DeleteAffinityGroup(ctx, projectId, region, affinityGroupId).Execute()
313316
if err != nil {
314317
var oapiErr *oapierror.GenericOpenAPIError
315318
if errors.As(err, &oapiErr) && oapiErr.StatusCode == http.StatusNotFound {

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
99
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
10+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/clientutils"
1011

1112
"github.com/hashicorp/terraform-plugin-framework/attr"
1213
"github.com/hashicorp/terraform-plugin-framework/datasource"
@@ -45,13 +46,17 @@ type DataSourceModel struct {
4546
}
4647

4748
// NewImageDataSource is a helper function to simplify the provider implementation.
48-
func NewImageDataSource() datasource.DataSource {
49-
return &imageDataSource{}
49+
func NewImageDataSource(clientFactory clientutils.ClientFactory) datasource.DataSource {
50+
return &imageDataSource{
51+
clientFactory: clientFactory,
52+
}
5053
}
5154

5255
// imageDataSource is the data source implementation.
5356
type imageDataSource struct {
54-
client *iaas.APIClient
57+
clientFactory clientutils.ClientFactory
58+
59+
client iaas.DefaultAPI
5560
providerData core.ProviderData
5661
}
5762

@@ -67,11 +72,11 @@ func (d *imageDataSource) Configure(ctx context.Context, req datasource.Configur
6772
return
6873
}
6974

70-
apiClient := iaasUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
75+
d.client = d.clientFactory.NewIaaSV2Client(ctx, &d.providerData, &resp.Diagnostics)
7176
if resp.Diagnostics.HasError() {
7277
return
7378
}
74-
d.client = apiClient
79+
7580
tflog.Info(ctx, "iaas client configured")
7681
}
7782

@@ -231,7 +236,7 @@ func (d *imageDataSource) Read(ctx context.Context, req datasource.ReadRequest,
231236
ctx = tflog.SetField(ctx, "region", region)
232237
ctx = tflog.SetField(ctx, "image_id", imageId)
233238

234-
imageResp, err := d.client.DefaultAPI.GetImage(ctx, projectId, region, imageId).Execute()
239+
imageResp, err := d.client.GetImage(ctx, projectId, region, imageId).Execute()
235240
if err != nil {
236241
utils.LogError(
237242
ctx,

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
14+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/clientutils"
1415

1516
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
1617

@@ -108,13 +109,17 @@ var checksumTypes = map[string]attr.Type{
108109
}
109110

110111
// NewImageResource is a helper function to simplify the provider implementation.
111-
func NewImageResource() resource.Resource {
112-
return &imageResource{}
112+
func NewImageResource(clientFactory clientutils.ClientFactory) resource.Resource {
113+
return &imageResource{
114+
clientFactory: clientFactory,
115+
}
113116
}
114117

115118
// imageResource is the resource implementation.
116119
type imageResource struct {
117-
client *iaas.APIClient
120+
clientFactory clientutils.ClientFactory
121+
122+
client iaas.DefaultAPI
118123
providerData core.ProviderData
119124
}
120125

@@ -161,11 +166,11 @@ func (r *imageResource) Configure(ctx context.Context, req resource.ConfigureReq
161166
return
162167
}
163168

164-
apiClient := iaasUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
169+
r.client = r.clientFactory.NewIaaSV2Client(ctx, &r.providerData, &resp.Diagnostics)
165170
if resp.Diagnostics.HasError() {
166171
return
167172
}
168-
r.client = apiClient
173+
169174
tflog.Info(ctx, "iaas client configured")
170175
}
171176

@@ -436,7 +441,7 @@ func (r *imageResource) Create(ctx context.Context, req resource.CreateRequest,
436441
}
437442

438443
// Create new image
439-
imageCreateResp, err := r.client.DefaultAPI.CreateImage(ctx, projectId, region).CreateImagePayload(*payload).Execute()
444+
imageCreateResp, err := r.client.CreateImage(ctx, projectId, region).CreateImagePayload(*payload).Execute()
440445
if err != nil {
441446
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating image", fmt.Sprintf("Calling API: %v", err))
442447
return
@@ -447,7 +452,7 @@ func (r *imageResource) Create(ctx context.Context, req resource.CreateRequest,
447452
ctx = tflog.SetField(ctx, "image_id", imageCreateResp.Id)
448453

449454
// Get the image object, as the creation response does not contain all fields
450-
image, err := r.client.DefaultAPI.GetImage(ctx, projectId, region, imageCreateResp.Id).Execute()
455+
image, err := r.client.GetImage(ctx, projectId, region, imageCreateResp.Id).Execute()
451456
if err != nil {
452457
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating image", fmt.Sprintf("Calling API: %v", err))
453458
return
@@ -475,8 +480,8 @@ func (r *imageResource) Create(ctx context.Context, req resource.CreateRequest,
475480
}
476481

477482
// Wait for image to become available
478-
waiter := wait.UploadImageWaitHandler(ctx, r.client.DefaultAPI, projectId, region, imageCreateResp.Id) //nolint:tfwriteid // false positive - id fields are actually stored already using the mapFields() call above
479-
waiter = waiter.SetTimeout(7 * 24 * time.Hour) // Set timeout to one week, to make the timeout useless
483+
waiter := wait.UploadImageWaitHandler(ctx, r.client, projectId, region, imageCreateResp.Id) //nolint:tfwriteid // false positive - id fields are actually stored already using the mapFields() call above
484+
waiter = waiter.SetTimeout(7 * 24 * time.Hour) // Set timeout to one week, to make the timeout useless
480485
waitResp, err := waiter.WaitWithContext(ctx)
481486
if err != nil {
482487
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating image", fmt.Sprintf("Waiting for image to become available: %v", err))
@@ -523,7 +528,7 @@ func (r *imageResource) Read(ctx context.Context, req resource.ReadRequest, resp
523528
ctx = tflog.SetField(ctx, "region", region)
524529
ctx = tflog.SetField(ctx, "image_id", imageId)
525530

526-
imageResp, err := r.client.DefaultAPI.GetImage(ctx, projectId, region, imageId).Execute()
531+
imageResp, err := r.client.GetImage(ctx, projectId, region, imageId).Execute()
527532
if err != nil {
528533
var oapiErr *oapierror.GenericOpenAPIError
529534
if errors.As(err, &oapiErr) && oapiErr.StatusCode == http.StatusNotFound {
@@ -586,7 +591,7 @@ func (r *imageResource) Update(ctx context.Context, req resource.UpdateRequest,
586591
return
587592
}
588593
// Update existing image
589-
updatedImage, err := r.client.DefaultAPI.UpdateImage(ctx, projectId, region, imageId).UpdateImagePayload(*payload).Execute()
594+
updatedImage, err := r.client.UpdateImage(ctx, projectId, region, imageId).UpdateImagePayload(*payload).Execute()
590595
if err != nil {
591596
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating image", fmt.Sprintf("Calling API: %v", err))
592597
return
@@ -627,7 +632,7 @@ func (r *imageResource) Delete(ctx context.Context, req resource.DeleteRequest,
627632
ctx = core.InitProviderContext(ctx)
628633

629634
// Delete existing image
630-
err := r.client.DefaultAPI.DeleteImage(ctx, projectId, region, imageId).Execute()
635+
err := r.client.DeleteImage(ctx, projectId, region, imageId).Execute()
631636
if err != nil {
632637
var oapiErr *oapierror.GenericOpenAPIError
633638
if errors.As(err, &oapiErr) && oapiErr.StatusCode == http.StatusNotFound {
@@ -640,7 +645,7 @@ func (r *imageResource) Delete(ctx context.Context, req resource.DeleteRequest,
640645

641646
ctx = core.LogResponse(ctx)
642647

643-
_, err = wait.DeleteImageWaitHandler(ctx, r.client.DefaultAPI, projectId, region, imageId).WaitWithContext(ctx)
648+
_, err = wait.DeleteImageWaitHandler(ctx, r.client, projectId, region, imageId).WaitWithContext(ctx)
644649
if err != nil {
645650
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting image", fmt.Sprintf("image deletion waiting: %v", err))
646651
return

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework/path"
1212
iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api"
1313

14+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/clientutils"
15+
1416
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
1517
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
1618
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features"
@@ -109,13 +111,17 @@ var checksumTypes = map[string]attr.Type{
109111
}
110112

111113
// NewImageV2DataSource is a helper function to simplify the provider implementation.
112-
func NewImageV2DataSource() datasource.DataSource {
113-
return &imageDataV2Source{}
114+
func NewImageV2DataSource(clientFactory clientutils.ClientFactory) datasource.DataSource {
115+
return &imageDataV2Source{
116+
clientFactory: clientFactory,
117+
}
114118
}
115119

116120
// imageDataV2Source is the data source implementation.
117121
type imageDataV2Source struct {
118-
client *iaas.APIClient
122+
clientFactory clientutils.ClientFactory
123+
124+
client iaas.DefaultAPI
119125
providerData core.ProviderData
120126
}
121127

@@ -136,12 +142,11 @@ func (d *imageDataV2Source) Configure(ctx context.Context, req datasource.Config
136142
return
137143
}
138144

139-
apiClient := iaasUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
145+
d.client = d.clientFactory.NewIaaSV2Client(ctx, &d.providerData, &resp.Diagnostics)
140146
if resp.Diagnostics.HasError() {
141147
return
142148
}
143149

144-
d.client = apiClient
145150
tflog.Info(ctx, "iaas client configured")
146151
}
147152

@@ -394,7 +399,7 @@ func (d *imageDataV2Source) Read(ctx context.Context, req datasource.ReadRequest
394399

395400
// Case 1: Direct lookup by image ID
396401
if imageID != "" {
397-
imageResp, err = d.client.DefaultAPI.GetImage(ctx, projectID, region, imageID).Execute()
402+
imageResp, err = d.client.GetImage(ctx, projectID, region, imageID).Execute()
398403
if err != nil {
399404
utils.LogError(ctx, &resp.Diagnostics, err, "Reading image",
400405
fmt.Sprintf("Image with ID %q does not exist in project %q.", imageID, projectID),
@@ -420,7 +425,7 @@ func (d *imageDataV2Source) Read(ctx context.Context, req datasource.ReadRequest
420425
}
421426

422427
// Fetch all available images
423-
imageList, err := d.client.DefaultAPI.ListImages(ctx, projectID, region).Execute()
428+
imageList, err := d.client.ListImages(ctx, projectID, region).Execute()
424429
if err != nil {
425430
utils.LogError(ctx, &resp.Diagnostics, err, "List images", "Unable to fetch images", nil)
426431
return

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66

77
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
8-
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
8+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/clientutils"
99

1010
"github.com/hashicorp/terraform-plugin-framework/datasource"
1111
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
@@ -23,13 +23,17 @@ var (
2323
)
2424

2525
// NewKeyPairDataSource is a helper function to simplify the provider implementation.
26-
func NewKeyPairDataSource() datasource.DataSource {
27-
return &keyPairDataSource{}
26+
func NewKeyPairDataSource(clientFactory clientutils.ClientFactory) datasource.DataSource {
27+
return &keyPairDataSource{
28+
clientFactory: clientFactory,
29+
}
2830
}
2931

3032
// keyPairDataSource is the data source implementation.
3133
type keyPairDataSource struct {
32-
client *iaas.APIClient
34+
clientFactory clientutils.ClientFactory
35+
36+
client iaas.DefaultAPI
3337
}
3438

3539
// Metadata returns the data source type name.
@@ -43,11 +47,11 @@ func (d *keyPairDataSource) Configure(ctx context.Context, req datasource.Config
4347
return
4448
}
4549

46-
apiClient := iaasUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics)
50+
d.client = d.clientFactory.NewIaaSV2Client(ctx, &providerData, &resp.Diagnostics)
4751
if resp.Diagnostics.HasError() {
4852
return
4953
}
50-
d.client = apiClient
54+
5155
tflog.Info(ctx, "iaas client configured")
5256
}
5357

@@ -98,7 +102,7 @@ func (d *keyPairDataSource) Read(ctx context.Context, req datasource.ReadRequest
98102

99103
ctx = tflog.SetField(ctx, "name", name)
100104

101-
keypairResp, err := d.client.DefaultAPI.GetKeyPair(ctx, name).Execute()
105+
keypairResp, err := d.client.GetKeyPair(ctx, name).Execute()
102106
if err != nil {
103107
utils.LogError(
104108
ctx,

0 commit comments

Comments
 (0)