99 "strings"
1010 "time"
1111
12+ "github.com/hashicorp/terraform-plugin-framework/resource/schema/int32planmodifier"
13+
1214 sqlserverflexUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/sqlserverflex/utils"
1315 stringplanmodifierCustom "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/planmodifiers/stringplanmodifier"
1416
@@ -33,8 +35,8 @@ import (
3335 "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
3436 "github.com/hashicorp/terraform-plugin-framework/types"
3537 "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
36- "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex"
37- "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/wait"
38+ sqlserverflex "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/v2api "
39+ "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/v2api/ wait"
3840)
3941
4042// Ensure the implementation satisfies the expected interfaces.
@@ -55,7 +57,7 @@ type Model struct {
5557 Flavor types.Object `tfsdk:"flavor"`
5658 Storage types.Object `tfsdk:"storage"`
5759 Version types.String `tfsdk:"version"`
58- Replicas types.Int64 `tfsdk:"replicas"`
60+ Replicas types.Int32 `tfsdk:"replicas"`
5961 Options types.Object `tfsdk:"options"`
6062 Region types.String `tfsdk:"region"`
6163}
@@ -64,16 +66,16 @@ type Model struct {
6466type flavorModel struct {
6567 Id types.String `tfsdk:"id"`
6668 Description types.String `tfsdk:"description"`
67- CPU types.Int64 `tfsdk:"cpu"`
68- RAM types.Int64 `tfsdk:"ram"`
69+ CPU types.Int32 `tfsdk:"cpu"`
70+ RAM types.Int32 `tfsdk:"ram"`
6971}
7072
7173// Types corresponding to flavorModel
7274var flavorTypes = map [string ]attr.Type {
7375 "id" : basetypes.StringType {},
7476 "description" : basetypes.StringType {},
75- "cpu" : basetypes.Int64Type {},
76- "ram" : basetypes.Int64Type {},
77+ "cpu" : basetypes.Int32Type {},
78+ "ram" : basetypes.Int32Type {},
7779}
7880
7981// Struct corresponding to Model.Storage
@@ -252,18 +254,18 @@ func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, r
252254 stringplanmodifier .UseStateForUnknown (),
253255 },
254256 },
255- "cpu" : schema.Int64Attribute {
257+ "cpu" : schema.Int32Attribute {
256258 Required : true ,
257259 },
258- "ram" : schema.Int64Attribute {
260+ "ram" : schema.Int32Attribute {
259261 Required : true ,
260262 },
261263 },
262264 },
263- "replicas" : schema.Int64Attribute {
265+ "replicas" : schema.Int32Attribute {
264266 Computed : true ,
265- PlanModifiers : []planmodifier.Int64 {
266- int64planmodifier .UseStateForUnknown (),
267+ PlanModifiers : []planmodifier.Int32 {
268+ int32planmodifier .UseStateForUnknown (),
267269 },
268270 },
269271 "storage" : schema.SingleNestedAttribute {
@@ -369,7 +371,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
369371 if resp .Diagnostics .HasError () {
370372 return
371373 }
372- err := loadFlavorId (ctx , r .client , & model , flavor )
374+ err := loadFlavorId (ctx , r .client . DefaultAPI , & model , flavor )
373375 if err != nil {
374376 core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating instance" , fmt .Sprintf ("Loading flavor ID: %v" , err ))
375377 return
@@ -400,7 +402,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
400402 return
401403 }
402404 // Create new instance
403- createResp , err := r .client .CreateInstance (ctx , projectId , region ).CreateInstancePayload (* payload ).Execute ()
405+ createResp , err := r .client .DefaultAPI . CreateInstance (ctx , projectId , region ).CreateInstancePayload (* payload ).Execute ()
404406 if err != nil {
405407 core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating instance" , fmt .Sprintf ("Calling API: %v" , err ))
406408 return
@@ -425,7 +427,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
425427 }
426428 // The creation waiter sometimes returns an error from the API: "instance with id xxx has unexpected status Failure"
427429 // which can be avoided by sleeping before wait
428- waitResp , err := wait .CreateInstanceWaitHandler (ctx , r .client , projectId , instanceId , region ).SetSleepBeforeWait (30 * time .Second ).WaitWithContext (ctx )
430+ waitResp , err := wait .CreateInstanceWaitHandler (ctx , r .client . DefaultAPI , projectId , instanceId , region ).SetSleepBeforeWait (30 * time .Second ).WaitWithContext (ctx )
429431 if err != nil {
430432 core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating instance" , fmt .Sprintf ("Instance creation waiting: %v" , err ))
431433 return
@@ -496,7 +498,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
496498 }
497499 }
498500
499- instanceResp , err := r .client .GetInstance (ctx , projectId , instanceId , region ).Execute ()
501+ instanceResp , err := r .client .DefaultAPI . GetInstance (ctx , projectId , instanceId , region ).Execute ()
500502 if err != nil {
501503 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
502504 if ok && oapiErr .StatusCode == http .StatusNotFound {
@@ -559,7 +561,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
559561 if resp .Diagnostics .HasError () {
560562 return
561563 }
562- err := loadFlavorId (ctx , r .client , & model , flavor )
564+ err := loadFlavorId (ctx , r .client . DefaultAPI , & model , flavor )
563565 if err != nil {
564566 core .LogAndAddError (ctx , & resp .Diagnostics , "Error updating instance" , fmt .Sprintf ("Loading flavor ID: %v" , err ))
565567 return
@@ -590,15 +592,15 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
590592 return
591593 }
592594 // Update existing instance
593- _ , err = r .client .PartialUpdateInstance (ctx , projectId , instanceId , region ).PartialUpdateInstancePayload (* payload ).Execute ()
595+ _ , err = r .client .DefaultAPI . PartialUpdateInstance (ctx , projectId , instanceId , region ).PartialUpdateInstancePayload (* payload ).Execute ()
594596 if err != nil {
595597 core .LogAndAddError (ctx , & resp .Diagnostics , "Error updating instance" , err .Error ())
596598 return
597599 }
598600
599601 ctx = core .LogResponse (ctx )
600602
601- waitResp , err := wait .UpdateInstanceWaitHandler (ctx , r .client , projectId , instanceId , region ).WaitWithContext (ctx )
603+ waitResp , err := wait .UpdateInstanceWaitHandler (ctx , r .client . DefaultAPI , projectId , instanceId , region ).WaitWithContext (ctx )
602604 if err != nil {
603605 core .LogAndAddError (ctx , & resp .Diagnostics , "Error updating instance" , fmt .Sprintf ("Instance update waiting: %v" , err ))
604606 return
@@ -638,15 +640,15 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques
638640 ctx = tflog .SetField (ctx , "region" , region )
639641
640642 // Delete existing instance
641- err := r .client .DeleteInstance (ctx , projectId , instanceId , region ).Execute ()
643+ err := r .client .DefaultAPI . DeleteInstance (ctx , projectId , instanceId , region ).Execute ()
642644 if err != nil {
643645 core .LogAndAddError (ctx , & resp .Diagnostics , "Error deleting instance" , fmt .Sprintf ("Calling API: %v" , err ))
644646 return
645647 }
646648
647649 ctx = core .LogResponse (ctx )
648650
649- _ , err = wait .DeleteInstanceWaitHandler (ctx , r .client , projectId , instanceId , region ).WaitWithContext (ctx )
651+ _ , err = wait .DeleteInstanceWaitHandler (ctx , r .client . DefaultAPI , projectId , instanceId , region ).WaitWithContext (ctx )
650652 if err != nil {
651653 core .LogAndAddError (ctx , & resp .Diagnostics , "Error deleting instance" , fmt .Sprintf ("Instance deletion waiting: %v" , err ))
652654 return
@@ -701,7 +703,7 @@ func mapFields(ctx context.Context, resp *sqlserverflex.GetInstanceResponse, mod
701703 if instance .Acl == nil || instance .Acl .Items == nil {
702704 aclList = types .ListNull (types .StringType )
703705 } else {
704- respACL := * instance .Acl .Items
706+ respACL := instance .Acl .Items
705707 modelACL , err := utils .ListValuetoStringSlice (model .ACL )
706708 if err != nil {
707709 return err
@@ -727,8 +729,8 @@ func mapFields(ctx context.Context, resp *sqlserverflex.GetInstanceResponse, mod
727729 flavorValues = map [string ]attr.Value {
728730 "id" : types .StringValue (* instance .Flavor .Id ),
729731 "description" : types .StringValue (* instance .Flavor .Description ),
730- "cpu" : types .Int64PointerValue (instance .Flavor .Cpu ),
731- "ram" : types .Int64PointerValue (instance .Flavor .Memory ),
732+ "cpu" : types .Int32PointerValue (instance .Flavor .Cpu ),
733+ "ram" : types .Int32PointerValue (instance .Flavor .Memory ),
732734 }
733735 }
734736 flavorObject , diags := types .ObjectValue (flavorTypes , flavorValues )
@@ -799,7 +801,7 @@ func mapFields(ctx context.Context, resp *sqlserverflex.GetInstanceResponse, mod
799801 model .Name = types .StringPointerValue (instance .Name )
800802 model .ACL = aclList
801803 model .Flavor = flavorObject
802- model .Replicas = types .Int64PointerValue (instance .Replicas )
804+ model .Replicas = types .Int32PointerValue (instance .Replicas )
803805 model .Storage = storageObject
804806 model .Version = types .StringPointerValue (instance .Version )
805807 model .Options = optionsObject
@@ -811,19 +813,19 @@ func toCreatePayload(model *Model, acl []string, flavor *flavorModel, storage *s
811813 if model == nil {
812814 return nil , fmt .Errorf ("nil model" )
813815 }
814- aclPayload := & sqlserverflex.CreateInstancePayloadAcl {}
816+ aclPayload := & sqlserverflex.InstanceDocumentationACL {}
815817 if acl != nil {
816- aclPayload .Items = & acl
818+ aclPayload .Items = acl
817819 }
818820 if flavor == nil {
819821 return nil , fmt .Errorf ("nil flavor" )
820822 }
821- storagePayload := & sqlserverflex.CreateInstancePayloadStorage {}
823+ storagePayload := & sqlserverflex.InstanceDocumentationStorage {}
822824 if storage != nil {
823825 storagePayload .Class = conversion .StringValueToPointer (storage .Class )
824826 storagePayload .Size = conversion .Int64ValueToPointer (storage .Size )
825827 }
826- optionsPayload := & sqlserverflex.CreateInstancePayloadOptions {}
828+ optionsPayload := & sqlserverflex.InstanceDocumentationOptions {}
827829 if options != nil {
828830 optionsPayload .Edition = conversion .StringValueToPointer (options .Edition )
829831 retentionDaysInt := conversion .Int64ValueToPointer (options .RetentionDays )
@@ -837,8 +839,8 @@ func toCreatePayload(model *Model, acl []string, flavor *flavorModel, storage *s
837839 return & sqlserverflex.CreateInstancePayload {
838840 Acl : aclPayload ,
839841 BackupSchedule : conversion .StringValueToPointer (model .BackupSchedule ),
840- FlavorId : conversion . StringValueToPointer ( flavor .Id ),
841- Name : conversion . StringValueToPointer ( model .Name ),
842+ FlavorId : flavor .Id . ValueString ( ),
843+ Name : model .Name . ValueString ( ),
842844 Storage : storagePayload ,
843845 Version : conversion .StringValueToPointer (model .Version ),
844846 Options : optionsPayload ,
@@ -849,9 +851,9 @@ func toUpdatePayload(model *Model, acl []string, flavor *flavorModel) (*sqlserve
849851 if model == nil {
850852 return nil , fmt .Errorf ("nil model" )
851853 }
852- aclPayload := & sqlserverflex.CreateInstancePayloadAcl {}
854+ aclPayload := & sqlserverflex.InstanceDocumentationACL {}
853855 if acl != nil {
854- aclPayload .Items = & acl
856+ aclPayload .Items = acl
855857 }
856858 if flavor == nil {
857859 return nil , fmt .Errorf ("nil flavor" )
@@ -867,7 +869,8 @@ func toUpdatePayload(model *Model, acl []string, flavor *flavorModel) (*sqlserve
867869}
868870
869871type sqlserverflexClient interface {
870- ListFlavorsExecute (ctx context.Context , projectId , region string ) (* sqlserverflex.ListFlavorsResponse , error )
872+ ListFlavors (ctx context.Context , projectId , region string ) sqlserverflex.ApiListFlavorsRequest
873+ ListFlavorsExecute (r sqlserverflex.ApiListFlavorsRequest ) (* sqlserverflex.ListFlavorsResponse , error )
871874}
872875
873876func loadFlavorId (ctx context.Context , client sqlserverflexClient , model * Model , flavor * flavorModel ) error {
@@ -877,18 +880,19 @@ func loadFlavorId(ctx context.Context, client sqlserverflexClient, model *Model,
877880 if flavor == nil {
878881 return fmt .Errorf ("nil flavor" )
879882 }
880- cpu := conversion .Int64ValueToPointer (flavor .CPU )
883+ cpu := conversion .Int32ValueToPointer (flavor .CPU )
881884 if cpu == nil {
882885 return fmt .Errorf ("nil CPU" )
883886 }
884- ram := conversion .Int64ValueToPointer (flavor .RAM )
887+ ram := conversion .Int32ValueToPointer (flavor .RAM )
885888 if ram == nil {
886889 return fmt .Errorf ("nil RAM" )
887890 }
888891
889892 projectId := model .ProjectId .ValueString ()
890893 region := model .Region .ValueString ()
891- res , err := client .ListFlavorsExecute (ctx , projectId , region )
894+ req := client .ListFlavors (ctx , projectId , region )
895+ res , err := client .ListFlavorsExecute (req )
892896 if err != nil {
893897 return fmt .Errorf ("listing sqlserverflex flavors: %w" , err )
894898 }
@@ -897,7 +901,7 @@ func loadFlavorId(ctx context.Context, client sqlserverflexClient, model *Model,
897901 if res .Flavors == nil {
898902 return fmt .Errorf ("finding flavors for project %s" , projectId )
899903 }
900- for _ , f := range * res .Flavors {
904+ for _ , f := range res .Flavors {
901905 if f .Id == nil || f .Cpu == nil || f .Memory == nil {
902906 continue
903907 }
0 commit comments