Skip to content

Commit 34290e2

Browse files
committed
chore(postgresflex): use new multi API version support SDK - part1
1 parent 20dcaac commit 34290e2

File tree

8 files changed

+97
-88
lines changed

8 files changed

+97
-88
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/stackitcloud/stackit-sdk-go/services/alb v0.12.1
1717
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.13.0
1818
github.com/stackitcloud/stackit-sdk-go/services/dns v0.19.1
19-
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.0
19+
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.1
2020
github.com/stackitcloud/stackit-sdk-go/services/git v0.11.0
2121
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.3.5
2222
github.com/stackitcloud/stackit-sdk-go/services/kms v1.6.0
@@ -29,7 +29,7 @@ require (
2929
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.7.0
3030
github.com/stackitcloud/stackit-sdk-go/services/observability v0.17.0
3131
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.26.1
32-
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.3.5
32+
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.6.2
3333
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.26.0
3434
github.com/stackitcloud/stackit-sdk-go/services/redis v0.25.6
3535
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
@@ -165,6 +165,8 @@ github.com/stackitcloud/stackit-sdk-go/services/dns v0.19.1 h1:VfszhFq/Snsd0Lnfl
165165
github.com/stackitcloud/stackit-sdk-go/services/dns v0.19.1/go.mod h1:gBv6YkB3Xf3c0ZXg2GwtWY8zExwGPF/Ag114XiiERxg=
166166
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.0 h1:lsOTEvItzrUerCzUodyyOy6pZ4QiSzwID7U20ZvZvHw=
167167
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.0/go.mod h1:CfqSEGCW0b5JlijCwtUT1kfjThmQ5jXX47TWrdD5rTU=
168+
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.1 h1:G21hx6vztR1Np+K+MXsK1td/CKbKHdLK89zvtH6OoKk=
169+
github.com/stackitcloud/stackit-sdk-go/services/edge v0.8.1/go.mod h1:qHuMB/qt7w1u5Xk3qzP3pJFXjTqPrtpM2bMhKpIvM3I=
168170
github.com/stackitcloud/stackit-sdk-go/services/git v0.11.0 h1:siKBCYl7BwR5S25Y4SwUAX6eFTXSCkQ+8JIQEqyKIXo=
169171
github.com/stackitcloud/stackit-sdk-go/services/git v0.11.0/go.mod h1:QKy74hhLVZKXItw3y+elF8s9QezFqpEsAUmaqvNDzYs=
170172
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.3.5 h1:W57+XRa8wTLsi5CV9Tqa7mGgt/PvlRM//RurXSmvII8=
@@ -195,6 +197,8 @@ github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.26.1 h1:AO5Np67/w0
195197
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.26.1/go.mod h1:Reo2Nk3yiA686quUMrk0eKwzpzaAZ7KwmzHzScNdG8Q=
196198
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.3.5 h1:H67e3KnHQx954yI8fuQmxXwRf/myqAdLg2KvxImp00g=
197199
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.3.5/go.mod h1:xmAWk9eom8wznvLuLfm0F4xyeiBX8LaggXsKFmos+dw=
200+
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.6.2 h1:5Op07pEs6dcJxfi2UjT1645k1uMf1xMbY8/D3A797aA=
201+
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.6.2/go.mod h1:AqFWa9kddt2Wqe8k1Kw5FB1mlHG30K1FGsJx8dw/r1c=
198202
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.26.0 h1:/8lmviszgrB+0Cz7HdhFELyTiTeqIs7LfnI6sNX4rW8=
199203
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.26.0/go.mod h1:hnhvlLX1Y71R8KIQqLBeoSZqkU5ZJOG0J4wz0LeUdaw=
200204
github.com/stackitcloud/stackit-sdk-go/services/redis v0.25.6 h1:CXM9cZ9WeTyJd+Aw/hnJnDsKRVAQi4qgtd0RJ3zoPwo=

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

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

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

2525
// Ensure the implementation satisfies the expected interfaces.
@@ -123,7 +123,7 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
123123
},
124124
},
125125
},
126-
"replicas": schema.Int64Attribute{
126+
"replicas": schema.Int32Attribute{
127127
Computed: true,
128128
},
129129
"storage": schema.SingleNestedAttribute{
@@ -166,7 +166,7 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
166166
ctx = tflog.SetField(ctx, "project_id", projectId)
167167
ctx = tflog.SetField(ctx, "instance_id", instanceId)
168168
ctx = tflog.SetField(ctx, "region", region)
169-
instanceResp, err := r.client.GetInstance(ctx, projectId, region, instanceId).Execute()
169+
instanceResp, err := r.client.DefaultAPI.GetInstance(ctx, projectId, region, instanceId).Execute()
170170
if err != nil {
171171
utils.LogError(
172172
ctx,

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

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

3535
// Ensure the implementation satisfies the expected interfaces.
@@ -48,7 +48,7 @@ type Model struct {
4848
ACL types.List `tfsdk:"acl"`
4949
BackupSchedule types.String `tfsdk:"backup_schedule"`
5050
Flavor types.Object `tfsdk:"flavor"`
51-
Replicas types.Int64 `tfsdk:"replicas"`
51+
Replicas types.Int32 `tfsdk:"replicas"`
5252
Storage types.Object `tfsdk:"storage"`
5353
Version types.String `tfsdk:"version"`
5454
Region types.String `tfsdk:"region"`
@@ -237,7 +237,7 @@ func (r *instanceResource) Schema(_ context.Context, req resource.SchemaRequest,
237237
},
238238
},
239239
},
240-
"replicas": schema.Int64Attribute{
240+
"replicas": schema.Int32Attribute{
241241
Description: descriptions["replicas"],
242242
Required: true,
243243
},
@@ -303,7 +303,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
303303
if resp.Diagnostics.HasError() {
304304
return
305305
}
306-
err := loadFlavorId(ctx, r.client, &model, flavor)
306+
err := loadFlavorId(ctx, r.client.DefaultAPI, &model, flavor)
307307
if err != nil {
308308
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Loading flavor ID: %v", err))
309309
return
@@ -325,7 +325,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
325325
return
326326
}
327327
// Create new instance
328-
createResp, err := r.client.CreateInstance(ctx, projectId, region).CreateInstancePayload(*payload).Execute()
328+
createResp, err := r.client.DefaultAPI.CreateInstance(ctx, projectId, region).CreateInstancePayload(*payload).Execute()
329329
if err != nil {
330330
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Calling API: %v", err))
331331
return
@@ -348,7 +348,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
348348
return
349349
}
350350

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

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

502502
ctx = core.LogResponse(ctx)
503503

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

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

550550
ctx = core.LogResponse(ctx)
551551

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

693693
return &postgresflex.CreateInstancePayload{
694-
Acl: &postgresflex.ACL{
695-
Items: &acl,
694+
Acl: postgresflex.ACL{
695+
Items: acl,
696696
},
697-
BackupSchedule: conversion.StringValueToPointer(model.BackupSchedule),
698-
FlavorId: conversion.StringValueToPointer(flavor.Id),
699-
Name: conversion.StringValueToPointer(model.Name),
700-
Replicas: conversion.Int64ValueToPointer(model.Replicas),
701-
Storage: &postgresflex.Storage{
697+
BackupSchedule: model.BackupSchedule.ValueString(),
698+
FlavorId: flavor.Id.ValueString(),
699+
Name: model.Name.ValueString(),
700+
Replicas: model.Replicas.ValueInt32(),
701+
Storage: postgresflex.Storage{
702702
Class: conversion.StringValueToPointer(storage.Class),
703703
Size: conversion.Int64ValueToPointer(storage.Size),
704704
},
705-
Version: conversion.StringValueToPointer(model.Version),
705+
Version: model.Version.ValueString(),
706706
}, nil
707707
}
708708

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

723723
return &postgresflex.PartialUpdateInstancePayload{
724724
Acl: &postgresflex.ACL{
725-
Items: &acl,
725+
Items: acl,
726726
},
727727
BackupSchedule: conversion.StringValueToPointer(model.BackupSchedule),
728728
FlavorId: conversion.StringValueToPointer(flavor.Id),
729729
Name: conversion.StringValueToPointer(model.Name),
730-
Replicas: conversion.Int64ValueToPointer(model.Replicas),
730+
Replicas: conversion.Int32ValueToPointer(model.Replicas),
731731
Version: conversion.StringValueToPointer(model.Version),
732732
}, nil
733733
}
734734

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

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

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

0 commit comments

Comments
 (0)