Skip to content

Commit 1af1194

Browse files
chore(postgresflex): use new multi API version support SDK (#1370)
* chore(postgresflex): use new multi API version support SDK - part1 * chore(postgresflex): use new multi API version support SDK - part2 * fix linter issues --------- Co-authored-by: cgoetz-inovex <carlo.goetz@inovex.de>
1 parent e457a12 commit 1af1194

File tree

15 files changed

+139
-128
lines changed

15 files changed

+139
-128
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.14.0
1818
github.com/stackitcloud/stackit-sdk-go/services/certificates v1.4.1
1919
github.com/stackitcloud/stackit-sdk-go/services/dns v0.19.1
20-
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.0
20+
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.1
2121
github.com/stackitcloud/stackit-sdk-go/services/git v0.11.0
2222
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.3.5
2323
github.com/stackitcloud/stackit-sdk-go/services/kms v1.6.0
@@ -30,7 +30,7 @@ require (
3030
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.7.0
3131
github.com/stackitcloud/stackit-sdk-go/services/observability v0.17.0
3232
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.26.1
33-
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.3.5
33+
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.6.2
3434
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.29.0
3535
github.com/stackitcloud/stackit-sdk-go/services/redis v0.28.1
3636
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.20.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,8 @@ github.com/stackitcloud/stackit-sdk-go/services/dns v0.19.1 h1:VfszhFq/Snsd0Lnfl
682682
github.com/stackitcloud/stackit-sdk-go/services/dns v0.19.1/go.mod h1:gBv6YkB3Xf3c0ZXg2GwtWY8zExwGPF/Ag114XiiERxg=
683683
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.0 h1:lsOTEvItzrUerCzUodyyOy6pZ4QiSzwID7U20ZvZvHw=
684684
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.0/go.mod h1:CfqSEGCW0b5JlijCwtUT1kfjThmQ5jXX47TWrdD5rTU=
685+
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.1 h1:G21hx6vztR1Np+K+MXsK1td/CKbKHdLK89zvtH6OoKk=
686+
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.1/go.mod h1:qHuMB/qt7w1u5Xk3qzP3pJFXjTqPrtpM2bMhKpIvM3I=
685687
github.com/stackitcloud/stackit-sdk-go/services/git v0.11.0 h1:siKBCYl7BwR5S25Y4SwUAX6eFTXSCkQ+8JIQEqyKIXo=
686688
github.com/stackitcloud/stackit-sdk-go/services/git v0.11.0/go.mod h1:QKy74hhLVZKXItw3y+elF8s9QezFqpEsAUmaqvNDzYs=
687689
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.3.5 h1:W57+XRa8wTLsi5CV9Tqa7mGgt/PvlRM//RurXSmvII8=
@@ -708,6 +710,8 @@ github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.26.1 h1:AO5Np67/w0
708710
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.26.1/go.mod h1:Reo2Nk3yiA686quUMrk0eKwzpzaAZ7KwmzHzScNdG8Q=
709711
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.3.5 h1:H67e3KnHQx954yI8fuQmxXwRf/myqAdLg2KvxImp00g=
710712
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.3.5/go.mod h1:xmAWk9eom8wznvLuLfm0F4xyeiBX8LaggXsKFmos+dw=
713+
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.6.2 h1:5Op07pEs6dcJxfi2UjT1645k1uMf1xMbY8/D3A797aA=
714+
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.6.2/go.mod h1:AqFWa9kddt2Wqe8k1Kw5FB1mlHG30K1FGsJx8dw/r1c=
711715
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.29.0 h1:I8NF8Q5nRj7/7qDI8Qm4oI0KCNQcOjE6kqyBdWcUoxk=
712716
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.29.0/go.mod h1:wk2Oz+7pxT9YF1h6elWzdlhU6V7GSQXcGixxlXxNe+U=
713717
github.com/stackitcloud/stackit-sdk-go/services/redis v0.28.1 h1:6loJq37rLpU84HY/rzmpZrBGPz2vKqhly8mUC8NcAu0=

stackit/internal/services/postgresflex/database/datasource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
1818

1919
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
20-
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex"
20+
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v2api"
2121
)
2222

2323
// Ensure the implementation satisfies the expected interfaces.

stackit/internal/services/postgresflex/database/resource.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
2424
"github.com/hashicorp/terraform-plugin-framework/types"
2525
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
26-
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex"
26+
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v2api"
2727
)
2828

2929
// Ensure the implementation satisfies the expected interfaces.
@@ -215,7 +215,7 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques
215215
return
216216
}
217217
// Create new database
218-
databaseResp, err := r.client.CreateDatabase(ctx, projectId, region, instanceId).CreateDatabasePayload(*payload).Execute()
218+
databaseResp, err := r.client.DefaultAPI.CreateDatabase(ctx, projectId, region, instanceId).CreateDatabasePayload(*payload).Execute()
219219
if err != nil {
220220
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating database", fmt.Sprintf("Calling API: %v", err))
221221
return
@@ -328,7 +328,7 @@ func (r *databaseResource) Delete(ctx context.Context, req resource.DeleteReques
328328
ctx = tflog.SetField(ctx, "region", region)
329329

330330
// Delete existing record set
331-
err := r.client.DeleteDatabase(ctx, projectId, region, instanceId, databaseId).Execute()
331+
err := r.client.DefaultAPI.DeleteDatabase(ctx, projectId, region, instanceId, databaseId).Execute()
332332
if err != nil {
333333
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting database", fmt.Sprintf("Calling API: %v", err))
334334
}
@@ -390,7 +390,7 @@ func mapFields(databaseResp *postgresflex.InstanceDatabase, model *Model, region
390390
model.Region = types.StringValue(region)
391391

392392
if databaseResp.Options != nil {
393-
owner, ok := (*databaseResp.Options)["owner"]
393+
owner, ok := (databaseResp.Options)["owner"]
394394
if ok {
395395
ownerStr, ok := owner.(string)
396396
if !ok {
@@ -423,14 +423,14 @@ var errDatabaseNotFound = errors.New("database not found")
423423

424424
// The API does not have a GetDatabase endpoint, only ListDatabases
425425
func getDatabase(ctx context.Context, client *postgresflex.APIClient, projectId, region, instanceId, databaseId string) (*postgresflex.InstanceDatabase, error) {
426-
resp, err := client.ListDatabases(ctx, projectId, region, instanceId).Execute()
426+
resp, err := client.DefaultAPI.ListDatabases(ctx, projectId, region, instanceId).Execute()
427427
if err != nil {
428428
return nil, err
429429
}
430430
if resp == nil || resp.Databases == nil {
431431
return nil, fmt.Errorf("response is nil")
432432
}
433-
for _, database := range *resp.Databases {
433+
for _, database := range resp.Databases {
434434
if database.Id != nil && *database.Id == databaseId {
435435
return &database, nil
436436
}

stackit/internal/services/postgresflex/database/resource_test.go

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

66
"github.com/google/go-cmp/cmp"
77
"github.com/hashicorp/terraform-plugin-framework/types"
8-
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex"
8+
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v2api"
99
)
1010

1111
func TestMapFields(t *testing.T) {
@@ -39,7 +39,7 @@ func TestMapFields(t *testing.T) {
3939
&postgresflex.InstanceDatabase{
4040
Id: new("uid"),
4141
Name: new("dbname"),
42-
Options: &map[string]any{
42+
Options: map[string]any{
4343
"owner": "username",
4444
},
4545
},
@@ -60,7 +60,7 @@ func TestMapFields(t *testing.T) {
6060
&postgresflex.InstanceDatabase{
6161
Id: new("uid"),
6262
Name: new(""),
63-
Options: &map[string]any{
63+
Options: map[string]any{
6464
"owner": "",
6565
},
6666
},
@@ -95,7 +95,7 @@ func TestMapFields(t *testing.T) {
9595
&postgresflex.InstanceDatabase{
9696
Id: new(""),
9797
Name: new("dbname"),
98-
Options: &map[string]any{
98+
Options: map[string]any{
9999
"owner": "username",
100100
},
101101
},

stackit/internal/services/postgresflex/instance/datasource.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919

2020
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
2121
"github.com/hashicorp/terraform-plugin-framework/types"
22-
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex"
23-
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/wait"
22+
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v2api"
23+
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v2api/wait"
2424
)
2525

2626
// Ensure the implementation satisfies the expected interfaces.
@@ -124,7 +124,7 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
124124
},
125125
},
126126
},
127-
"replicas": schema.Int64Attribute{
127+
"replicas": schema.Int32Attribute{
128128
Computed: true,
129129
},
130130
"storage": schema.SingleNestedAttribute{
@@ -167,7 +167,7 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
167167
ctx = tflog.SetField(ctx, "project_id", projectId)
168168
ctx = tflog.SetField(ctx, "instance_id", instanceId)
169169
ctx = tflog.SetField(ctx, "region", region)
170-
instanceResp, err := r.client.GetInstance(ctx, projectId, region, instanceId).Execute()
170+
instanceResp, err := r.client.DefaultAPI.GetInstance(ctx, projectId, region, instanceId).Execute()
171171
if err != nil {
172172
utils.LogError(
173173
ctx,

stackit/internal/services/postgresflex/instance/resource.go

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import (
2929
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
3030
"github.com/hashicorp/terraform-plugin-framework/types"
3131
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
32-
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex"
33-
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/wait"
32+
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v2api"
33+
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v2api/wait"
3434
)
3535

3636
// Ensure the implementation satisfies the expected interfaces.
@@ -49,7 +49,7 @@ type Model struct {
4949
ACL types.List `tfsdk:"acl"`
5050
BackupSchedule types.String `tfsdk:"backup_schedule"`
5151
Flavor types.Object `tfsdk:"flavor"`
52-
Replicas types.Int64 `tfsdk:"replicas"`
52+
Replicas types.Int32 `tfsdk:"replicas"`
5353
Storage types.Object `tfsdk:"storage"`
5454
Version types.String `tfsdk:"version"`
5555
Region types.String `tfsdk:"region"`
@@ -238,7 +238,7 @@ func (r *instanceResource) Schema(_ context.Context, req resource.SchemaRequest,
238238
},
239239
},
240240
},
241-
"replicas": schema.Int64Attribute{
241+
"replicas": schema.Int32Attribute{
242242
Description: descriptions["replicas"],
243243
Required: true,
244244
},
@@ -304,7 +304,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
304304
if resp.Diagnostics.HasError() {
305305
return
306306
}
307-
err := loadFlavorId(ctx, r.client, &model, flavor)
307+
err := loadFlavorId(ctx, r.client.DefaultAPI, &model, flavor)
308308
if err != nil {
309309
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Loading flavor ID: %v", err))
310310
return
@@ -326,7 +326,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
326326
return
327327
}
328328
// Create new instance
329-
createResp, err := r.client.CreateInstance(ctx, projectId, region).CreateInstancePayload(*payload).Execute()
329+
createResp, err := r.client.DefaultAPI.CreateInstance(ctx, projectId, region).CreateInstancePayload(*payload).Execute()
330330
if err != nil {
331331
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Calling API: %v", err))
332332
return
@@ -349,7 +349,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
349349
return
350350
}
351351

352-
waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, region, instanceId).WaitWithContext(ctx)
352+
waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client.DefaultAPI, projectId, region, instanceId).WaitWithContext(ctx)
353353
if err != nil {
354354
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Instance creation waiting: %v", err))
355355
return
@@ -405,7 +405,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
405405
}
406406
}
407407

408-
instanceResp, err := r.client.GetInstance(ctx, projectId, region, instanceId).Execute()
408+
instanceResp, err := r.client.DefaultAPI.GetInstance(ctx, projectId, region, instanceId).Execute()
409409
if err != nil {
410410
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
411411
if ok && oapiErr.StatusCode == http.StatusNotFound {
@@ -472,7 +472,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
472472
if resp.Diagnostics.HasError() {
473473
return
474474
}
475-
err := loadFlavorId(ctx, r.client, &model, flavor)
475+
err := loadFlavorId(ctx, r.client.DefaultAPI, &model, flavor)
476476
if err != nil {
477477
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Loading flavor ID: %v", err))
478478
return
@@ -494,15 +494,15 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
494494
return
495495
}
496496
// Update existing instance
497-
_, err = r.client.PartialUpdateInstance(ctx, projectId, region, instanceId).PartialUpdateInstancePayload(*payload).Execute()
497+
_, err = r.client.DefaultAPI.PartialUpdateInstance(ctx, projectId, region, instanceId).PartialUpdateInstancePayload(*payload).Execute()
498498
if err != nil {
499499
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", err.Error())
500500
return
501501
}
502502

503503
ctx = core.LogResponse(ctx)
504504

505-
waitResp, err := wait.PartialUpdateInstanceWaitHandler(ctx, r.client, projectId, region, instanceId).WaitWithContext(ctx)
505+
waitResp, err := wait.PartialUpdateInstanceWaitHandler(ctx, r.client.DefaultAPI, projectId, region, instanceId).WaitWithContext(ctx)
506506
if err != nil {
507507
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Instance update waiting: %v", err))
508508
return
@@ -542,15 +542,15 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques
542542
ctx = tflog.SetField(ctx, "region", region)
543543

544544
// Delete existing instance
545-
err := r.client.DeleteInstance(ctx, projectId, region, instanceId).Execute()
545+
err := r.client.DefaultAPI.DeleteInstance(ctx, projectId, region, instanceId).Execute()
546546
if err != nil {
547547
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting instance", fmt.Sprintf("Calling API: %v", err))
548548
return
549549
}
550550

551551
ctx = core.LogResponse(ctx)
552552

553-
_, err = wait.DeleteInstanceWaitHandler(ctx, r.client, projectId, region, instanceId).SetTimeout(45 * time.Minute).WaitWithContext(ctx)
553+
_, err = wait.DeleteInstanceWaitHandler(ctx, r.client.DefaultAPI, projectId, region, instanceId).SetTimeout(45 * time.Minute).WaitWithContext(ctx)
554554
if err != nil {
555555
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting instance", fmt.Sprintf("Instance deletion waiting: %v", err))
556556
return
@@ -605,7 +605,7 @@ func mapFields(ctx context.Context, resp *postgresflex.InstanceResponse, model *
605605
if instance.Acl == nil || instance.Acl.Items == nil {
606606
aclList = types.ListNull(types.StringType)
607607
} else {
608-
respACL := *instance.Acl.Items
608+
respACL := instance.Acl.Items
609609
modelACL, err := utils.ListValuetoStringSlice(model.ACL)
610610
if err != nil {
611611
return err
@@ -670,7 +670,7 @@ func mapFields(ctx context.Context, resp *postgresflex.InstanceResponse, model *
670670
model.Name = types.StringPointerValue(instance.Name)
671671
model.ACL = aclList
672672
model.Flavor = flavorObject
673-
model.Replicas = types.Int64PointerValue(instance.Replicas)
673+
model.Replicas = types.Int32PointerValue(instance.Replicas)
674674
model.Storage = storageObject
675675
model.Version = types.StringPointerValue(instance.Version)
676676
model.Region = types.StringValue(region)
@@ -692,18 +692,18 @@ func toCreatePayload(model *Model, acl []string, flavor *flavorModel, storage *s
692692
}
693693

694694
return &postgresflex.CreateInstancePayload{
695-
Acl: &postgresflex.ACL{
696-
Items: &acl,
695+
Acl: postgresflex.ACL{
696+
Items: acl,
697697
},
698-
BackupSchedule: conversion.StringValueToPointer(model.BackupSchedule),
699-
FlavorId: conversion.StringValueToPointer(flavor.Id),
700-
Name: conversion.StringValueToPointer(model.Name),
701-
Replicas: conversion.Int64ValueToPointer(model.Replicas),
702-
Storage: &postgresflex.Storage{
698+
BackupSchedule: model.BackupSchedule.ValueString(),
699+
FlavorId: flavor.Id.ValueString(),
700+
Name: model.Name.ValueString(),
701+
Replicas: model.Replicas.ValueInt32(),
702+
Storage: postgresflex.Storage{
703703
Class: conversion.StringValueToPointer(storage.Class),
704704
Size: conversion.Int64ValueToPointer(storage.Size),
705705
},
706-
Version: conversion.StringValueToPointer(model.Version),
706+
Version: model.Version.ValueString(),
707707
}, nil
708708
}
709709

@@ -723,18 +723,19 @@ func toUpdatePayload(model *Model, acl []string, flavor *flavorModel, storage *s
723723

724724
return &postgresflex.PartialUpdateInstancePayload{
725725
Acl: &postgresflex.ACL{
726-
Items: &acl,
726+
Items: acl,
727727
},
728728
BackupSchedule: conversion.StringValueToPointer(model.BackupSchedule),
729729
FlavorId: conversion.StringValueToPointer(flavor.Id),
730730
Name: conversion.StringValueToPointer(model.Name),
731-
Replicas: conversion.Int64ValueToPointer(model.Replicas),
731+
Replicas: conversion.Int32ValueToPointer(model.Replicas),
732732
Version: conversion.StringValueToPointer(model.Version),
733733
}, nil
734734
}
735735

736736
type postgresFlexClient interface {
737-
ListFlavorsExecute(ctx context.Context, projectId string, region string) (*postgresflex.ListFlavorsResponse, error)
737+
ListFlavors(ctx context.Context, projectId, region string) postgresflex.ApiListFlavorsRequest
738+
ListFlavorsExecute(r postgresflex.ApiListFlavorsRequest) (*postgresflex.ListFlavorsResponse, error)
738739
}
739740

740741
func loadFlavorId(ctx context.Context, client postgresFlexClient, model *Model, flavor *flavorModel) error {
@@ -755,7 +756,8 @@ func loadFlavorId(ctx context.Context, client postgresFlexClient, model *Model,
755756

756757
projectId := model.ProjectId.ValueString()
757758
region := model.Region.ValueString()
758-
res, err := client.ListFlavorsExecute(ctx, projectId, region)
759+
req := client.ListFlavors(ctx, projectId, region)
760+
res, err := client.ListFlavorsExecute(req)
759761
if err != nil {
760762
return fmt.Errorf("listing postgresflex flavors: %w", err)
761763
}
@@ -764,7 +766,7 @@ func loadFlavorId(ctx context.Context, client postgresFlexClient, model *Model,
764766
if res.Flavors == nil {
765767
return fmt.Errorf("finding flavors for project %s", projectId)
766768
}
767-
for _, f := range *res.Flavors {
769+
for _, f := range res.Flavors {
768770
if f.Id == nil || f.Cpu == nil || f.Memory == nil {
769771
continue
770772
}

0 commit comments

Comments
 (0)