Skip to content

Commit d39f23d

Browse files
committed
switch to multi API version support SDK structure
1 parent 0d8fea1 commit d39f23d

File tree

11 files changed

+187
-179
lines changed

11 files changed

+187
-179
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ 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/sqlserverflex"
21+
sqlserverflex "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/v2api"
2222
)
2323

2424
// Ensure the implementation satisfies the expected interfaces.
@@ -117,15 +117,15 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
117117
"description": schema.StringAttribute{
118118
Computed: true,
119119
},
120-
"cpu": schema.Int64Attribute{
120+
"cpu": schema.Int32Attribute{
121121
Computed: true,
122122
},
123-
"ram": schema.Int64Attribute{
123+
"ram": schema.Int32Attribute{
124124
Computed: true,
125125
},
126126
},
127127
},
128-
"replicas": schema.Int64Attribute{
128+
"replicas": schema.Int32Attribute{
129129
Computed: true,
130130
},
131131
"storage": schema.SingleNestedAttribute{
@@ -180,7 +180,7 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
180180
ctx = tflog.SetField(ctx, "project_id", projectId)
181181
ctx = tflog.SetField(ctx, "instance_id", instanceId)
182182
ctx = tflog.SetField(ctx, "region", region)
183-
instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId, region).Execute()
183+
instanceResp, err := r.client.DefaultAPI.GetInstance(ctx, projectId, instanceId, region).Execute()
184184
if err != nil {
185185
utils.LogError(
186186
ctx,

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

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"time"
1111

12+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int32planmodifier"
1213
sqlserverflexUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/sqlserverflex/utils"
1314

1415
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
@@ -32,8 +33,8 @@ import (
3233
"github.com/hashicorp/terraform-plugin-framework/types"
3334
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
3435
coreUtils "github.com/stackitcloud/stackit-sdk-go/core/utils"
35-
"github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex"
36-
"github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/wait"
36+
sqlserverflex "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/v2api"
37+
"github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/v2api/wait"
3738
)
3839

3940
// Ensure the implementation satisfies the expected interfaces.
@@ -54,7 +55,7 @@ type Model struct {
5455
Flavor types.Object `tfsdk:"flavor"`
5556
Storage types.Object `tfsdk:"storage"`
5657
Version types.String `tfsdk:"version"`
57-
Replicas types.Int64 `tfsdk:"replicas"`
58+
Replicas types.Int32 `tfsdk:"replicas"`
5859
Options types.Object `tfsdk:"options"`
5960
Region types.String `tfsdk:"region"`
6061
}
@@ -63,16 +64,16 @@ type Model struct {
6364
type flavorModel struct {
6465
Id types.String `tfsdk:"id"`
6566
Description types.String `tfsdk:"description"`
66-
CPU types.Int64 `tfsdk:"cpu"`
67-
RAM types.Int64 `tfsdk:"ram"`
67+
CPU types.Int32 `tfsdk:"cpu"`
68+
RAM types.Int32 `tfsdk:"ram"`
6869
}
6970

7071
// Types corresponding to flavorModel
7172
var flavorTypes = map[string]attr.Type{
7273
"id": basetypes.StringType{},
7374
"description": basetypes.StringType{},
74-
"cpu": basetypes.Int64Type{},
75-
"ram": basetypes.Int64Type{},
75+
"cpu": basetypes.Int32Type{},
76+
"ram": basetypes.Int32Type{},
7677
}
7778

7879
// Struct corresponding to Model.Storage
@@ -250,18 +251,18 @@ func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, r
250251
stringplanmodifier.UseStateForUnknown(),
251252
},
252253
},
253-
"cpu": schema.Int64Attribute{
254+
"cpu": schema.Int32Attribute{
254255
Required: true,
255256
},
256-
"ram": schema.Int64Attribute{
257+
"ram": schema.Int32Attribute{
257258
Required: true,
258259
},
259260
},
260261
},
261-
"replicas": schema.Int64Attribute{
262+
"replicas": schema.Int32Attribute{
262263
Computed: true,
263-
PlanModifiers: []planmodifier.Int64{
264-
int64planmodifier.UseStateForUnknown(),
264+
PlanModifiers: []planmodifier.Int32{
265+
int32planmodifier.UseStateForUnknown(),
265266
},
266267
},
267268
"storage": schema.SingleNestedAttribute{
@@ -367,7 +368,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
367368
if resp.Diagnostics.HasError() {
368369
return
369370
}
370-
err := loadFlavorId(ctx, r.client, &model, flavor)
371+
err := loadFlavorId(ctx, r.client.DefaultAPI, &model, flavor)
371372
if err != nil {
372373
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Loading flavor ID: %v", err))
373374
return
@@ -398,7 +399,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
398399
return
399400
}
400401
// Create new instance
401-
createResp, err := r.client.CreateInstance(ctx, projectId, region).CreateInstancePayload(*payload).Execute()
402+
createResp, err := r.client.DefaultAPI.CreateInstance(ctx, projectId, region).CreateInstancePayload(*payload).Execute()
402403
if err != nil {
403404
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Calling API: %v", err))
404405
return
@@ -423,7 +424,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
423424
}
424425
// The creation waiter sometimes returns an error from the API: "instance with id xxx has unexpected status Failure"
425426
// which can be avoided by sleeping before wait
426-
waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, instanceId, region).SetSleepBeforeWait(30 * time.Second).WaitWithContext(ctx)
427+
waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, region).SetSleepBeforeWait(30 * time.Second).WaitWithContext(ctx)
427428
if err != nil {
428429
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Instance creation waiting: %v", err))
429430
return
@@ -494,7 +495,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
494495
}
495496
}
496497

497-
instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId, region).Execute()
498+
instanceResp, err := r.client.DefaultAPI.GetInstance(ctx, projectId, instanceId, region).Execute()
498499
if err != nil {
499500
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
500501
if ok && oapiErr.StatusCode == http.StatusNotFound {
@@ -557,7 +558,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
557558
if resp.Diagnostics.HasError() {
558559
return
559560
}
560-
err := loadFlavorId(ctx, r.client, &model, flavor)
561+
err := loadFlavorId(ctx, r.client.DefaultAPI, &model, flavor)
561562
if err != nil {
562563
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Loading flavor ID: %v", err))
563564
return
@@ -588,15 +589,15 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
588589
return
589590
}
590591
// Update existing instance
591-
_, err = r.client.PartialUpdateInstance(ctx, projectId, instanceId, region).PartialUpdateInstancePayload(*payload).Execute()
592+
_, err = r.client.DefaultAPI.PartialUpdateInstance(ctx, projectId, instanceId, region).PartialUpdateInstancePayload(*payload).Execute()
592593
if err != nil {
593594
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", err.Error())
594595
return
595596
}
596597

597598
ctx = core.LogResponse(ctx)
598599

599-
waitResp, err := wait.UpdateInstanceWaitHandler(ctx, r.client, projectId, instanceId, region).WaitWithContext(ctx)
600+
waitResp, err := wait.UpdateInstanceWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, region).WaitWithContext(ctx)
600601
if err != nil {
601602
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Instance update waiting: %v", err))
602603
return
@@ -636,15 +637,15 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques
636637
ctx = tflog.SetField(ctx, "region", region)
637638

638639
// Delete existing instance
639-
err := r.client.DeleteInstance(ctx, projectId, instanceId, region).Execute()
640+
err := r.client.DefaultAPI.DeleteInstance(ctx, projectId, instanceId, region).Execute()
640641
if err != nil {
641642
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting instance", fmt.Sprintf("Calling API: %v", err))
642643
return
643644
}
644645

645646
ctx = core.LogResponse(ctx)
646647

647-
_, err = wait.DeleteInstanceWaitHandler(ctx, r.client, projectId, instanceId, region).WaitWithContext(ctx)
648+
_, err = wait.DeleteInstanceWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, region).WaitWithContext(ctx)
648649
if err != nil {
649650
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting instance", fmt.Sprintf("Instance deletion waiting: %v", err))
650651
return
@@ -699,7 +700,7 @@ func mapFields(ctx context.Context, resp *sqlserverflex.GetInstanceResponse, mod
699700
if instance.Acl == nil || instance.Acl.Items == nil {
700701
aclList = types.ListNull(types.StringType)
701702
} else {
702-
respACL := *instance.Acl.Items
703+
respACL := instance.Acl.Items
703704
modelACL, err := utils.ListValuetoStringSlice(model.ACL)
704705
if err != nil {
705706
return err
@@ -725,8 +726,8 @@ func mapFields(ctx context.Context, resp *sqlserverflex.GetInstanceResponse, mod
725726
flavorValues = map[string]attr.Value{
726727
"id": types.StringValue(*instance.Flavor.Id),
727728
"description": types.StringValue(*instance.Flavor.Description),
728-
"cpu": types.Int64PointerValue(instance.Flavor.Cpu),
729-
"ram": types.Int64PointerValue(instance.Flavor.Memory),
729+
"cpu": types.Int32PointerValue(instance.Flavor.Cpu),
730+
"ram": types.Int32PointerValue(instance.Flavor.Memory),
730731
}
731732
}
732733
flavorObject, diags := types.ObjectValue(flavorTypes, flavorValues)
@@ -796,7 +797,7 @@ func mapFields(ctx context.Context, resp *sqlserverflex.GetInstanceResponse, mod
796797
model.Name = types.StringPointerValue(instance.Name)
797798
model.ACL = aclList
798799
model.Flavor = flavorObject
799-
model.Replicas = types.Int64PointerValue(instance.Replicas)
800+
model.Replicas = types.Int32PointerValue(instance.Replicas)
800801
model.Storage = storageObject
801802
model.Version = types.StringPointerValue(instance.Version)
802803
model.Options = optionsObject
@@ -808,19 +809,19 @@ func toCreatePayload(model *Model, acl []string, flavor *flavorModel, storage *s
808809
if model == nil {
809810
return nil, fmt.Errorf("nil model")
810811
}
811-
aclPayload := &sqlserverflex.CreateInstancePayloadAcl{}
812+
aclPayload := &sqlserverflex.InstanceDocumentationACL{}
812813
if acl != nil {
813-
aclPayload.Items = &acl
814+
aclPayload.Items = acl
814815
}
815816
if flavor == nil {
816817
return nil, fmt.Errorf("nil flavor")
817818
}
818-
storagePayload := &sqlserverflex.CreateInstancePayloadStorage{}
819+
storagePayload := &sqlserverflex.InstanceDocumentationStorage{}
819820
if storage != nil {
820821
storagePayload.Class = conversion.StringValueToPointer(storage.Class)
821822
storagePayload.Size = conversion.Int64ValueToPointer(storage.Size)
822823
}
823-
optionsPayload := &sqlserverflex.CreateInstancePayloadOptions{}
824+
optionsPayload := &sqlserverflex.InstanceDocumentationOptions{}
824825
if options != nil {
825826
optionsPayload.Edition = conversion.StringValueToPointer(options.Edition)
826827
retentionDaysInt := conversion.Int64ValueToPointer(options.RetentionDays)
@@ -834,8 +835,8 @@ func toCreatePayload(model *Model, acl []string, flavor *flavorModel, storage *s
834835
return &sqlserverflex.CreateInstancePayload{
835836
Acl: aclPayload,
836837
BackupSchedule: conversion.StringValueToPointer(model.BackupSchedule),
837-
FlavorId: conversion.StringValueToPointer(flavor.Id),
838-
Name: conversion.StringValueToPointer(model.Name),
838+
FlavorId: flavor.Id.ValueString(),
839+
Name: model.Name.ValueString(),
839840
Storage: storagePayload,
840841
Version: conversion.StringValueToPointer(model.Version),
841842
Options: optionsPayload,
@@ -846,9 +847,9 @@ func toUpdatePayload(model *Model, acl []string, flavor *flavorModel) (*sqlserve
846847
if model == nil {
847848
return nil, fmt.Errorf("nil model")
848849
}
849-
aclPayload := &sqlserverflex.CreateInstancePayloadAcl{}
850+
aclPayload := &sqlserverflex.InstanceDocumentationACL{}
850851
if acl != nil {
851-
aclPayload.Items = &acl
852+
aclPayload.Items = acl
852853
}
853854
if flavor == nil {
854855
return nil, fmt.Errorf("nil flavor")
@@ -864,7 +865,8 @@ func toUpdatePayload(model *Model, acl []string, flavor *flavorModel) (*sqlserve
864865
}
865866

866867
type sqlserverflexClient interface {
867-
ListFlavorsExecute(ctx context.Context, projectId, region string) (*sqlserverflex.ListFlavorsResponse, error)
868+
ListFlavors(ctx context.Context, projectId, region string) sqlserverflex.ApiListFlavorsRequest
869+
ListFlavorsExecute(r sqlserverflex.ApiListFlavorsRequest) (*sqlserverflex.ListFlavorsResponse, error)
868870
}
869871

870872
func loadFlavorId(ctx context.Context, client sqlserverflexClient, model *Model, flavor *flavorModel) error {
@@ -874,18 +876,19 @@ func loadFlavorId(ctx context.Context, client sqlserverflexClient, model *Model,
874876
if flavor == nil {
875877
return fmt.Errorf("nil flavor")
876878
}
877-
cpu := conversion.Int64ValueToPointer(flavor.CPU)
879+
cpu := conversion.Int32ValueToPointer(flavor.CPU)
878880
if cpu == nil {
879881
return fmt.Errorf("nil CPU")
880882
}
881-
ram := conversion.Int64ValueToPointer(flavor.RAM)
883+
ram := conversion.Int32ValueToPointer(flavor.RAM)
882884
if ram == nil {
883885
return fmt.Errorf("nil RAM")
884886
}
885887

886888
projectId := model.ProjectId.ValueString()
887889
region := model.Region.ValueString()
888-
res, err := client.ListFlavorsExecute(ctx, projectId, region)
890+
req := client.ListFlavors(ctx, projectId, region)
891+
res, err := client.ListFlavorsExecute(req)
889892
if err != nil {
890893
return fmt.Errorf("listing sqlserverflex flavors: %w", err)
891894
}
@@ -894,7 +897,7 @@ func loadFlavorId(ctx context.Context, client sqlserverflexClient, model *Model,
894897
if res.Flavors == nil {
895898
return fmt.Errorf("finding flavors for project %s", projectId)
896899
}
897-
for _, f := range *res.Flavors {
900+
for _, f := range res.Flavors {
898901
if f.Id == nil || f.Cpu == nil || f.Memory == nil {
899902
continue
900903
}

0 commit comments

Comments
 (0)