Skip to content

Commit 1419d79

Browse files
authored
chore(objectstorage): switch to new SDK structure (#1335)
relates to STACKITTPR-565
1 parent c3d98ff commit 1419d79

File tree

17 files changed

+269
-291
lines changed

17 files changed

+269
-291
lines changed

stackit/internal/services/objectstorage/bucket/datasource.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
1717

1818
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
19-
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
19+
objectstorage "github.com/stackitcloud/stackit-sdk-go/services/objectstorage/v2api"
2020
)
2121

2222
// Ensure the implementation satisfies the expected interfaces.
@@ -129,7 +129,7 @@ func (r *bucketDataSource) Read(ctx context.Context, req datasource.ReadRequest,
129129
ctx = tflog.SetField(ctx, "name", bucketName)
130130
ctx = tflog.SetField(ctx, "region", region)
131131

132-
bucketResp, err := r.client.GetBucket(ctx, projectId, region, bucketName).Execute()
132+
bucketResp, err := r.client.DefaultAPI.GetBucket(ctx, projectId, region, bucketName).Execute()
133133
if err != nil {
134134
utils.LogError(
135135
ctx,

stackit/internal/services/objectstorage/bucket/resource.go

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
2525
"github.com/hashicorp/terraform-plugin-framework/types"
2626
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
27-
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
28-
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage/wait"
27+
objectstorage "github.com/stackitcloud/stackit-sdk-go/services/objectstorage/v2api"
28+
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage/v2api/wait"
2929
)
3030

3131
// Ensure the implementation satisfies the expected interfaces.
@@ -202,14 +202,14 @@ func (r *bucketResource) Create(ctx context.Context, req resource.CreateRequest,
202202
ctx = tflog.SetField(ctx, "region", region)
203203

204204
// Handle project init
205-
err := enableProject(ctx, &model, region, r.client)
205+
err := enableProject(ctx, &model, region, r.client.DefaultAPI)
206206
if err != nil {
207207
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating bucket", fmt.Sprintf("Enabling object storage project before creation: %v", err))
208208
return
209209
}
210210

211211
// Create new bucket
212-
_, err = r.client.CreateBucket(ctx, projectId, region, bucketName).ObjectLockEnabled(model.ObjectLock.ValueBool()).Execute()
212+
_, err = r.client.DefaultAPI.CreateBucket(ctx, projectId, region, bucketName).ObjectLockEnabled(model.ObjectLock.ValueBool()).Execute()
213213
if err != nil {
214214
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating bucket", fmt.Sprintf("Calling API: %v", err))
215215
return
@@ -227,7 +227,7 @@ func (r *bucketResource) Create(ctx context.Context, req resource.CreateRequest,
227227
return
228228
}
229229

230-
waitResp, err := wait.CreateBucketWaitHandler(ctx, r.client, projectId, region, bucketName).WaitWithContext(ctx)
230+
waitResp, err := wait.CreateBucketWaitHandler(ctx, r.client.DefaultAPI, projectId, region, bucketName).WaitWithContext(ctx)
231231
if err != nil {
232232
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating bucket", fmt.Sprintf("Bucket creation waiting: %v", err))
233233
return
@@ -266,7 +266,7 @@ func (r *bucketResource) Read(ctx context.Context, req resource.ReadRequest, res
266266
ctx = tflog.SetField(ctx, "name", bucketName)
267267
ctx = tflog.SetField(ctx, "region", region)
268268

269-
bucketResp, err := r.client.GetBucket(ctx, projectId, region, bucketName).Execute()
269+
bucketResp, err := r.client.DefaultAPI.GetBucket(ctx, projectId, region, bucketName).Execute()
270270
if err != nil {
271271
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
272272
if ok && oapiErr.StatusCode == http.StatusNotFound {
@@ -321,7 +321,7 @@ func (r *bucketResource) Delete(ctx context.Context, req resource.DeleteRequest,
321321
ctx = tflog.SetField(ctx, "region", region)
322322

323323
// Delete existing bucket
324-
_, err := r.client.DeleteBucket(ctx, projectId, region, bucketName).Execute()
324+
_, err := r.client.DefaultAPI.DeleteBucket(ctx, projectId, region, bucketName).Execute()
325325
if err != nil {
326326
var oapiErr *oapierror.GenericOpenAPIError
327327
if errors.As(err, &oapiErr) {
@@ -335,7 +335,7 @@ func (r *bucketResource) Delete(ctx context.Context, req resource.DeleteRequest,
335335

336336
ctx = core.LogResponse(ctx)
337337

338-
_, err = wait.DeleteBucketWaitHandler(ctx, r.client, projectId, region, bucketName).WaitWithContext(ctx)
338+
_, err = wait.DeleteBucketWaitHandler(ctx, r.client.DefaultAPI, projectId, region, bucketName).WaitWithContext(ctx)
339339
if err != nil {
340340
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting bucket", fmt.Sprintf("Bucket deletion waiting: %v", err))
341341
return
@@ -368,32 +368,25 @@ func mapFields(bucketResp *objectstorage.GetBucketResponse, model *Model, region
368368
if bucketResp == nil {
369369
return fmt.Errorf("response input is nil")
370370
}
371-
if bucketResp.Bucket == nil {
372-
return fmt.Errorf("response bucket is nil")
373-
}
374371
if model == nil {
375372
return fmt.Errorf("model input is nil")
376373
}
377374
bucket := bucketResp.Bucket
378375

379376
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), region, model.Name.ValueString())
380-
model.URLPathStyle = types.StringPointerValue(bucket.UrlPathStyle)
381-
model.URLVirtualHostedStyle = types.StringPointerValue(bucket.UrlVirtualHostedStyle)
377+
model.URLPathStyle = types.StringValue(bucket.UrlPathStyle)
378+
model.URLVirtualHostedStyle = types.StringValue(bucket.UrlVirtualHostedStyle)
382379
model.Region = types.StringValue(region)
383-
model.ObjectLock = types.BoolPointerValue(bucket.ObjectLockEnabled)
380+
model.ObjectLock = types.BoolValue(bucket.ObjectLockEnabled)
384381
return nil
385382
}
386383

387-
type objectStorageClient interface {
388-
EnableServiceExecute(ctx context.Context, projectId, region string) (*objectstorage.ProjectStatus, error)
389-
}
390-
391384
// enableProject enables object storage for the specified project. If the project is already enabled, nothing happens
392-
func enableProject(ctx context.Context, model *Model, region string, client objectStorageClient) error {
385+
func enableProject(ctx context.Context, model *Model, region string, client objectstorage.DefaultAPI) error {
393386
projectId := model.ProjectId.ValueString()
394387

395388
// From the object storage OAS: Creation will also be successful if the project is already enabled, but will not create a duplicate
396-
_, err := client.EnableServiceExecute(ctx, projectId, region)
389+
_, err := client.EnableService(ctx, projectId, region).Execute()
397390
if err != nil {
398391
return fmt.Errorf("failed to create object storage project: %w", err)
399392
}

stackit/internal/services/objectstorage/bucket/resource_test.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@ import (
88

99
"github.com/google/go-cmp/cmp"
1010
"github.com/hashicorp/terraform-plugin-framework/types"
11-
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
11+
objectstorage "github.com/stackitcloud/stackit-sdk-go/services/objectstorage/v2api"
1212
)
1313

14-
type objectStorageClientMocked struct {
14+
type mockSettings struct {
1515
returnError bool
1616
}
1717

18-
func (c *objectStorageClientMocked) EnableServiceExecute(_ context.Context, projectId, _ string) (*objectstorage.ProjectStatus, error) {
19-
if c.returnError {
20-
return nil, fmt.Errorf("create project failed")
21-
}
18+
func newAPIMock(settings *mockSettings) objectstorage.DefaultAPI {
19+
return &objectstorage.DefaultAPIServiceMock{
20+
EnableServiceExecuteMock: new(func(_ objectstorage.ApiEnableServiceRequest) (*objectstorage.ProjectStatus, error) {
21+
if settings.returnError {
22+
return nil, fmt.Errorf("create project failed")
23+
}
2224

23-
return &objectstorage.ProjectStatus{
24-
Project: new(projectId),
25-
}, nil
25+
return &objectstorage.ProjectStatus{}, nil
26+
}),
27+
}
2628
}
2729

2830
func TestMapFields(t *testing.T) {
@@ -37,25 +39,26 @@ func TestMapFields(t *testing.T) {
3739
{
3840
"default_values",
3941
&objectstorage.GetBucketResponse{
40-
Bucket: &objectstorage.Bucket{},
42+
Bucket: objectstorage.Bucket{},
4143
},
4244
Model{
4345
Id: types.StringValue(id),
4446
Name: types.StringValue("bname"),
4547
ProjectId: types.StringValue("pid"),
46-
URLPathStyle: types.StringNull(),
47-
URLVirtualHostedStyle: types.StringNull(),
48+
URLPathStyle: types.StringValue(""),
49+
URLVirtualHostedStyle: types.StringValue(""),
4850
Region: types.StringValue("eu01"),
51+
ObjectLock: types.BoolValue(false),
4952
},
5053
true,
5154
},
5255
{
5356
"simple_values",
5457
&objectstorage.GetBucketResponse{
55-
Bucket: &objectstorage.Bucket{
56-
UrlPathStyle: new("url/path/style"),
57-
UrlVirtualHostedStyle: new("url/virtual/hosted/style"),
58-
ObjectLockEnabled: new(true),
58+
Bucket: objectstorage.Bucket{
59+
UrlPathStyle: "url/path/style",
60+
UrlVirtualHostedStyle: "url/virtual/hosted/style",
61+
ObjectLockEnabled: true,
5962
},
6063
},
6164
Model{
@@ -72,9 +75,9 @@ func TestMapFields(t *testing.T) {
7275
{
7376
"empty_strings",
7477
&objectstorage.GetBucketResponse{
75-
Bucket: &objectstorage.Bucket{
76-
UrlPathStyle: new(""),
77-
UrlVirtualHostedStyle: new(""),
78+
Bucket: objectstorage.Bucket{
79+
UrlPathStyle: "",
80+
UrlVirtualHostedStyle: "",
7881
},
7982
},
8083
Model{
@@ -84,6 +87,7 @@ func TestMapFields(t *testing.T) {
8487
URLPathStyle: types.StringValue(""),
8588
URLVirtualHostedStyle: types.StringValue(""),
8689
Region: types.StringValue("eu01"),
90+
ObjectLock: types.BoolValue(false),
8791
},
8892
true,
8993
},
@@ -93,12 +97,6 @@ func TestMapFields(t *testing.T) {
9397
Model{},
9498
false,
9599
},
96-
{
97-
"no_bucket",
98-
&objectstorage.GetBucketResponse{},
99-
Model{},
100-
false,
101-
},
102100
}
103101
for _, tt := range tests {
104102
t.Run(tt.description, func(t *testing.T) {
@@ -142,9 +140,10 @@ func TestEnableProject(t *testing.T) {
142140
}
143141
for _, tt := range tests {
144142
t.Run(tt.description, func(t *testing.T) {
145-
client := &objectStorageClientMocked{
143+
client := newAPIMock(&mockSettings{
146144
returnError: tt.enableFails,
147-
}
145+
})
146+
148147
err := enableProject(context.Background(), &Model{}, "eu01", client)
149148
if !tt.isValid && err == nil {
150149
t.Fatalf("Should have failed")

stackit/internal/services/objectstorage/compliance-lock/datasource.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1010
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1111
"github.com/hashicorp/terraform-plugin-log/tflog"
12-
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
12+
objectstorage "github.com/stackitcloud/stackit-sdk-go/services/objectstorage/v2api"
1313
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
1414
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
1515
objectstorageUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/objectstorage/utils"
@@ -79,7 +79,7 @@ func (d *compliancelockDataSource) Schema(_ context.Context, _ datasource.Schema
7979
validate.NoSeparator(),
8080
},
8181
},
82-
"max_retention_days": schema.Int64Attribute{
82+
"max_retention_days": schema.Int32Attribute{
8383
Description: descriptions["max_retention_days"],
8484
Computed: true,
8585
},
@@ -109,7 +109,7 @@ func (d *compliancelockDataSource) Read(ctx context.Context, req datasource.Read
109109
ctx = tflog.SetField(ctx, "project_id", projectId)
110110
ctx = tflog.SetField(ctx, "region", region)
111111

112-
complianceResp, err := d.client.GetComplianceLock(ctx, projectId, region).Execute()
112+
complianceResp, err := d.client.DefaultAPI.GetComplianceLock(ctx, projectId, region).Execute()
113113
if err != nil {
114114
utils.LogError(
115115
ctx,

stackit/internal/services/objectstorage/compliance-lock/resource.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/hashicorp/terraform-plugin-framework/types"
1818
"github.com/hashicorp/terraform-plugin-log/tflog"
1919
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
20-
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
20+
objectstorage "github.com/stackitcloud/stackit-sdk-go/services/objectstorage/v2api"
2121
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
2222
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
2323
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
@@ -33,7 +33,7 @@ type Model struct {
3333
Id types.String `tfsdk:"id"` // needed by TF
3434
ProjectId types.String `tfsdk:"project_id"`
3535
Region types.String `tfsdk:"region"`
36-
MaxRetentionDays types.Int64 `tfsdk:"max_retention_days"`
36+
MaxRetentionDays types.Int32 `tfsdk:"max_retention_days"`
3737
}
3838

3939
// NewComplianceLockResource is a helper function to simplify the provider implementation.
@@ -129,7 +129,7 @@ func (r *compliancelockResource) Schema(_ context.Context, _ resource.SchemaRequ
129129
validate.NoSeparator(),
130130
},
131131
},
132-
"max_retention_days": schema.Int64Attribute{
132+
"max_retention_days": schema.Int32Attribute{
133133
Description: descriptions["max_retention_days"],
134134
Computed: true,
135135
},
@@ -163,7 +163,7 @@ func (r *compliancelockResource) Create(ctx context.Context, req resource.Create
163163
ctx = tflog.SetField(ctx, "project_id", projectId)
164164
ctx = tflog.SetField(ctx, "region", region)
165165

166-
complianceResp, err := r.client.CreateComplianceLock(ctx, projectId, region).Execute()
166+
complianceResp, err := r.client.DefaultAPI.CreateComplianceLock(ctx, projectId, region).Execute()
167167
if err != nil {
168168
var oapiErr *oapierror.GenericOpenAPIError
169169
ok := errors.As(err, &oapiErr)
@@ -174,7 +174,7 @@ func (r *compliancelockResource) Create(ctx context.Context, req resource.Create
174174
}
175175

176176
tflog.Info(ctx, "Compliance lock is already enabled for this project. Please check duplicate resources.")
177-
complianceResp, err = r.client.GetComplianceLock(ctx, projectId, region).Execute()
177+
complianceResp, err = r.client.DefaultAPI.GetComplianceLock(ctx, projectId, region).Execute()
178178
if err != nil {
179179
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading compliance lock", fmt.Sprintf("Calling API: %v", err))
180180
return
@@ -212,7 +212,7 @@ func (r *compliancelockResource) Read(ctx context.Context, req resource.ReadRequ
212212
ctx = tflog.SetField(ctx, "project_id", projectId)
213213
ctx = tflog.SetField(ctx, "region", region)
214214

215-
complianceResp, err := r.client.GetComplianceLock(ctx, projectId, region).Execute()
215+
complianceResp, err := r.client.DefaultAPI.GetComplianceLock(ctx, projectId, region).Execute()
216216
if err != nil {
217217
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
218218
if ok && oapiErr.StatusCode == http.StatusNotFound {
@@ -264,7 +264,7 @@ func (r *compliancelockResource) Delete(ctx context.Context, req resource.Delete
264264
ctx = tflog.SetField(ctx, "project_id", projectId)
265265
ctx = tflog.SetField(ctx, "region", region)
266266

267-
_, err := r.client.DeleteComplianceLock(ctx, projectId, region).Execute()
267+
_, err := r.client.DefaultAPI.DeleteComplianceLock(ctx, projectId, region).Execute()
268268
if err != nil {
269269
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting compliance lock", fmt.Sprintf("Calling API: %v", err))
270270
return
@@ -285,6 +285,6 @@ func mapFields(complianceResp *objectstorage.ComplianceLockResponse, model *Mode
285285

286286
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), region)
287287
model.Region = types.StringValue(region)
288-
model.MaxRetentionDays = types.Int64PointerValue(complianceResp.MaxRetentionDays)
288+
model.MaxRetentionDays = types.Int32Value(complianceResp.MaxRetentionDays)
289289
return nil
290290
}

stackit/internal/services/objectstorage/compliance-lock/resource_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import (
66

77
"github.com/google/go-cmp/cmp"
88
"github.com/hashicorp/terraform-plugin-framework/types"
9-
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
9+
objectstorage "github.com/stackitcloud/stackit-sdk-go/services/objectstorage/v2api"
1010
)
1111

1212
func TestMapFields(t *testing.T) {
1313
const testRegion = "eu01"
1414
id := fmt.Sprintf("%s,%s", "pid", testRegion)
15-
retentionDays := int64(30)
15+
retentionDays := int32(30)
1616
tests := []struct {
1717
description string
1818
input *objectstorage.ComplianceLockResponse
@@ -23,22 +23,23 @@ func TestMapFields(t *testing.T) {
2323
"default_values",
2424
&objectstorage.ComplianceLockResponse{},
2525
Model{
26-
Id: types.StringValue(id),
27-
ProjectId: types.StringValue("pid"),
28-
Region: types.StringValue("eu01"),
26+
Id: types.StringValue(id),
27+
ProjectId: types.StringValue("pid"),
28+
Region: types.StringValue("eu01"),
29+
MaxRetentionDays: types.Int32Value(0),
2930
},
3031
true,
3132
},
3233
{
3334
"simple_values",
3435
&objectstorage.ComplianceLockResponse{
35-
MaxRetentionDays: &retentionDays,
36+
MaxRetentionDays: retentionDays,
3637
},
3738
Model{
3839
Id: types.StringValue(id),
3940
ProjectId: types.StringValue("pid"),
4041
Region: types.StringValue("eu01"),
41-
MaxRetentionDays: types.Int64Value(retentionDays),
42+
MaxRetentionDays: types.Int32Value(retentionDays),
4243
},
4344
true,
4445
},

0 commit comments

Comments
 (0)