Skip to content

Commit 456d784

Browse files
committed
chore(objecstorage): switch to new SDK structure
relates to STACKITTPR-565
1 parent 24eac4d commit 456d784

File tree

17 files changed

+268
-292
lines changed

17 files changed

+268
-292
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
@@ -9,21 +9,23 @@ import (
99
"github.com/google/go-cmp/cmp"
1010
"github.com/hashicorp/terraform-plugin-framework/types"
1111
"github.com/stackitcloud/stackit-sdk-go/core/utils"
12-
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
12+
objectstorage "github.com/stackitcloud/stackit-sdk-go/services/objectstorage/v2api"
1313
)
1414

15-
type objectStorageClientMocked struct {
15+
type mockSettings struct {
1616
returnError bool
1717
}
1818

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

24-
return &objectstorage.ProjectStatus{
25-
Project: utils.Ptr(projectId),
26-
}, nil
26+
return &objectstorage.ProjectStatus{}, nil
27+
}),
28+
}
2729
}
2830

2931
func TestMapFields(t *testing.T) {
@@ -38,25 +40,26 @@ func TestMapFields(t *testing.T) {
3840
{
3941
"default_values",
4042
&objectstorage.GetBucketResponse{
41-
Bucket: &objectstorage.Bucket{},
43+
Bucket: objectstorage.Bucket{},
4244
},
4345
Model{
4446
Id: types.StringValue(id),
4547
Name: types.StringValue("bname"),
4648
ProjectId: types.StringValue("pid"),
47-
URLPathStyle: types.StringNull(),
48-
URLVirtualHostedStyle: types.StringNull(),
49+
URLPathStyle: types.StringValue(""),
50+
URLVirtualHostedStyle: types.StringValue(""),
4951
Region: types.StringValue("eu01"),
52+
ObjectLock: types.BoolValue(false),
5053
},
5154
true,
5255
},
5356
{
5457
"simple_values",
5558
&objectstorage.GetBucketResponse{
56-
Bucket: &objectstorage.Bucket{
57-
UrlPathStyle: utils.Ptr("url/path/style"),
58-
UrlVirtualHostedStyle: utils.Ptr("url/virtual/hosted/style"),
59-
ObjectLockEnabled: utils.Ptr(true),
59+
Bucket: objectstorage.Bucket{
60+
UrlPathStyle: "url/path/style",
61+
UrlVirtualHostedStyle: "url/virtual/hosted/style",
62+
ObjectLockEnabled: true,
6063
},
6164
},
6265
Model{
@@ -73,9 +76,9 @@ func TestMapFields(t *testing.T) {
7376
{
7477
"empty_strings",
7578
&objectstorage.GetBucketResponse{
76-
Bucket: &objectstorage.Bucket{
77-
UrlPathStyle: utils.Ptr(""),
78-
UrlVirtualHostedStyle: utils.Ptr(""),
79+
Bucket: objectstorage.Bucket{
80+
UrlPathStyle: "",
81+
UrlVirtualHostedStyle: "",
7982
},
8083
},
8184
Model{
@@ -85,6 +88,7 @@ func TestMapFields(t *testing.T) {
8588
URLPathStyle: types.StringValue(""),
8689
URLVirtualHostedStyle: types.StringValue(""),
8790
Region: types.StringValue("eu01"),
91+
ObjectLock: types.BoolValue(false),
8892
},
8993
true,
9094
},
@@ -94,12 +98,6 @@ func TestMapFields(t *testing.T) {
9498
Model{},
9599
false,
96100
},
97-
{
98-
"no_bucket",
99-
&objectstorage.GetBucketResponse{},
100-
Model{},
101-
false,
102-
},
103101
}
104102
for _, tt := range tests {
105103
t.Run(tt.description, func(t *testing.T) {
@@ -143,9 +141,10 @@ func TestEnableProject(t *testing.T) {
143141
}
144142
for _, tt := range tests {
145143
t.Run(tt.description, func(t *testing.T) {
146-
client := &objectStorageClientMocked{
144+
client := newAPIMock(&mockSettings{
147145
returnError: tt.enableFails,
148-
}
146+
})
147+
149148
err := enableProject(context.Background(), &Model{}, "eu01", client)
150149
if !tt.isValid && err == nil {
151150
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)