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 stringplanmodifierCustom "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/planmodifiers/stringplanmodifier"
1415
@@ -32,8 +33,8 @@ import (
3233 "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
3334 "github.com/hashicorp/terraform-plugin-framework/types"
3435 "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
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 {
6364type 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
7172var 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
@@ -251,18 +252,18 @@ func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, r
251252 stringplanmodifier .UseStateForUnknown (),
252253 },
253254 },
254- "cpu" : schema.Int64Attribute {
255+ "cpu" : schema.Int32Attribute {
255256 Required : true ,
256257 },
257- "ram" : schema.Int64Attribute {
258+ "ram" : schema.Int32Attribute {
258259 Required : true ,
259260 },
260261 },
261262 },
262- "replicas" : schema.Int64Attribute {
263+ "replicas" : schema.Int32Attribute {
263264 Computed : true ,
264- PlanModifiers : []planmodifier.Int64 {
265- int64planmodifier .UseStateForUnknown (),
265+ PlanModifiers : []planmodifier.Int32 {
266+ int32planmodifier .UseStateForUnknown (),
266267 },
267268 },
268269 "storage" : schema.SingleNestedAttribute {
@@ -368,7 +369,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
368369 if resp .Diagnostics .HasError () {
369370 return
370371 }
371- err := loadFlavorId (ctx , r .client , & model , flavor )
372+ err := loadFlavorId (ctx , r .client . DefaultAPI , & model , flavor )
372373 if err != nil {
373374 core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating instance" , fmt .Sprintf ("Loading flavor ID: %v" , err ))
374375 return
@@ -399,7 +400,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
399400 return
400401 }
401402 // Create new instance
402- createResp , err := r .client .CreateInstance (ctx , projectId , region ).CreateInstancePayload (* payload ).Execute ()
403+ createResp , err := r .client .DefaultAPI . CreateInstance (ctx , projectId , region ).CreateInstancePayload (* payload ).Execute ()
403404 if err != nil {
404405 core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating instance" , fmt .Sprintf ("Calling API: %v" , err ))
405406 return
@@ -424,7 +425,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
424425 }
425426 // The creation waiter sometimes returns an error from the API: "instance with id xxx has unexpected status Failure"
426427 // which can be avoided by sleeping before wait
427- waitResp , err := wait .CreateInstanceWaitHandler (ctx , r .client , projectId , instanceId , region ).SetSleepBeforeWait (30 * time .Second ).WaitWithContext (ctx )
428+ waitResp , err := wait .CreateInstanceWaitHandler (ctx , r .client . DefaultAPI , projectId , instanceId , region ).SetSleepBeforeWait (30 * time .Second ).WaitWithContext (ctx )
428429 if err != nil {
429430 core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating instance" , fmt .Sprintf ("Instance creation waiting: %v" , err ))
430431 return
@@ -495,7 +496,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
495496 }
496497 }
497498
498- instanceResp , err := r .client .GetInstance (ctx , projectId , instanceId , region ).Execute ()
499+ instanceResp , err := r .client .DefaultAPI . GetInstance (ctx , projectId , instanceId , region ).Execute ()
499500 if err != nil {
500501 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
501502 if ok && oapiErr .StatusCode == http .StatusNotFound {
@@ -558,7 +559,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
558559 if resp .Diagnostics .HasError () {
559560 return
560561 }
561- err := loadFlavorId (ctx , r .client , & model , flavor )
562+ err := loadFlavorId (ctx , r .client . DefaultAPI , & model , flavor )
562563 if err != nil {
563564 core .LogAndAddError (ctx , & resp .Diagnostics , "Error updating instance" , fmt .Sprintf ("Loading flavor ID: %v" , err ))
564565 return
@@ -589,15 +590,15 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
589590 return
590591 }
591592 // Update existing instance
592- _ , err = r .client .PartialUpdateInstance (ctx , projectId , instanceId , region ).PartialUpdateInstancePayload (* payload ).Execute ()
593+ _ , err = r .client .DefaultAPI . PartialUpdateInstance (ctx , projectId , instanceId , region ).PartialUpdateInstancePayload (* payload ).Execute ()
593594 if err != nil {
594595 core .LogAndAddError (ctx , & resp .Diagnostics , "Error updating instance" , err .Error ())
595596 return
596597 }
597598
598599 ctx = core .LogResponse (ctx )
599600
600- waitResp , err := wait .UpdateInstanceWaitHandler (ctx , r .client , projectId , instanceId , region ).WaitWithContext (ctx )
601+ waitResp , err := wait .UpdateInstanceWaitHandler (ctx , r .client . DefaultAPI , projectId , instanceId , region ).WaitWithContext (ctx )
601602 if err != nil {
602603 core .LogAndAddError (ctx , & resp .Diagnostics , "Error updating instance" , fmt .Sprintf ("Instance update waiting: %v" , err ))
603604 return
@@ -637,15 +638,15 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques
637638 ctx = tflog .SetField (ctx , "region" , region )
638639
639640 // Delete existing instance
640- err := r .client .DeleteInstance (ctx , projectId , instanceId , region ).Execute ()
641+ err := r .client .DefaultAPI . DeleteInstance (ctx , projectId , instanceId , region ).Execute ()
641642 if err != nil {
642643 core .LogAndAddError (ctx , & resp .Diagnostics , "Error deleting instance" , fmt .Sprintf ("Calling API: %v" , err ))
643644 return
644645 }
645646
646647 ctx = core .LogResponse (ctx )
647648
648- _ , err = wait .DeleteInstanceWaitHandler (ctx , r .client , projectId , instanceId , region ).WaitWithContext (ctx )
649+ _ , err = wait .DeleteInstanceWaitHandler (ctx , r .client . DefaultAPI , projectId , instanceId , region ).WaitWithContext (ctx )
649650 if err != nil {
650651 core .LogAndAddError (ctx , & resp .Diagnostics , "Error deleting instance" , fmt .Sprintf ("Instance deletion waiting: %v" , err ))
651652 return
@@ -700,7 +701,7 @@ func mapFields(ctx context.Context, resp *sqlserverflex.GetInstanceResponse, mod
700701 if instance .Acl == nil || instance .Acl .Items == nil {
701702 aclList = types .ListNull (types .StringType )
702703 } else {
703- respACL := * instance .Acl .Items
704+ respACL := instance .Acl .Items
704705 modelACL , err := utils .ListValuetoStringSlice (model .ACL )
705706 if err != nil {
706707 return err
@@ -726,8 +727,8 @@ func mapFields(ctx context.Context, resp *sqlserverflex.GetInstanceResponse, mod
726727 flavorValues = map [string ]attr.Value {
727728 "id" : types .StringValue (* instance .Flavor .Id ),
728729 "description" : types .StringValue (* instance .Flavor .Description ),
729- "cpu" : types .Int64PointerValue (instance .Flavor .Cpu ),
730- "ram" : types .Int64PointerValue (instance .Flavor .Memory ),
730+ "cpu" : types .Int32PointerValue (instance .Flavor .Cpu ),
731+ "ram" : types .Int32PointerValue (instance .Flavor .Memory ),
731732 }
732733 }
733734 flavorObject , diags := types .ObjectValue (flavorTypes , flavorValues )
@@ -798,7 +799,7 @@ func mapFields(ctx context.Context, resp *sqlserverflex.GetInstanceResponse, mod
798799 model .Name = types .StringPointerValue (instance .Name )
799800 model .ACL = aclList
800801 model .Flavor = flavorObject
801- model .Replicas = types .Int64PointerValue (instance .Replicas )
802+ model .Replicas = types .Int32PointerValue (instance .Replicas )
802803 model .Storage = storageObject
803804 model .Version = types .StringPointerValue (instance .Version )
804805 model .Options = optionsObject
@@ -810,19 +811,19 @@ func toCreatePayload(model *Model, acl []string, flavor *flavorModel, storage *s
810811 if model == nil {
811812 return nil , fmt .Errorf ("nil model" )
812813 }
813- aclPayload := & sqlserverflex.CreateInstancePayloadAcl {}
814+ aclPayload := & sqlserverflex.InstanceDocumentationACL {}
814815 if acl != nil {
815- aclPayload .Items = & acl
816+ aclPayload .Items = acl
816817 }
817818 if flavor == nil {
818819 return nil , fmt .Errorf ("nil flavor" )
819820 }
820- storagePayload := & sqlserverflex.CreateInstancePayloadStorage {}
821+ storagePayload := & sqlserverflex.InstanceDocumentationStorage {}
821822 if storage != nil {
822823 storagePayload .Class = conversion .StringValueToPointer (storage .Class )
823824 storagePayload .Size = conversion .Int64ValueToPointer (storage .Size )
824825 }
825- optionsPayload := & sqlserverflex.CreateInstancePayloadOptions {}
826+ optionsPayload := & sqlserverflex.InstanceDocumentationOptions {}
826827 if options != nil {
827828 optionsPayload .Edition = conversion .StringValueToPointer (options .Edition )
828829 retentionDaysInt := conversion .Int64ValueToPointer (options .RetentionDays )
@@ -836,8 +837,8 @@ func toCreatePayload(model *Model, acl []string, flavor *flavorModel, storage *s
836837 return & sqlserverflex.CreateInstancePayload {
837838 Acl : aclPayload ,
838839 BackupSchedule : conversion .StringValueToPointer (model .BackupSchedule ),
839- FlavorId : conversion . StringValueToPointer ( flavor .Id ),
840- Name : conversion . StringValueToPointer ( model .Name ),
840+ FlavorId : flavor .Id . ValueString ( ),
841+ Name : model .Name . ValueString ( ),
841842 Storage : storagePayload ,
842843 Version : conversion .StringValueToPointer (model .Version ),
843844 Options : optionsPayload ,
@@ -848,9 +849,9 @@ func toUpdatePayload(model *Model, acl []string, flavor *flavorModel) (*sqlserve
848849 if model == nil {
849850 return nil , fmt .Errorf ("nil model" )
850851 }
851- aclPayload := & sqlserverflex.CreateInstancePayloadAcl {}
852+ aclPayload := & sqlserverflex.InstanceDocumentationACL {}
852853 if acl != nil {
853- aclPayload .Items = & acl
854+ aclPayload .Items = acl
854855 }
855856 if flavor == nil {
856857 return nil , fmt .Errorf ("nil flavor" )
@@ -866,7 +867,8 @@ func toUpdatePayload(model *Model, acl []string, flavor *flavorModel) (*sqlserve
866867}
867868
868869type sqlserverflexClient interface {
869- ListFlavorsExecute (ctx context.Context , projectId , region string ) (* sqlserverflex.ListFlavorsResponse , error )
870+ ListFlavors (ctx context.Context , projectId , region string ) sqlserverflex.ApiListFlavorsRequest
871+ ListFlavorsExecute (r sqlserverflex.ApiListFlavorsRequest ) (* sqlserverflex.ListFlavorsResponse , error )
870872}
871873
872874func loadFlavorId (ctx context.Context , client sqlserverflexClient , model * Model , flavor * flavorModel ) error {
@@ -876,18 +878,19 @@ func loadFlavorId(ctx context.Context, client sqlserverflexClient, model *Model,
876878 if flavor == nil {
877879 return fmt .Errorf ("nil flavor" )
878880 }
879- cpu := conversion .Int64ValueToPointer (flavor .CPU )
881+ cpu := conversion .Int32ValueToPointer (flavor .CPU )
880882 if cpu == nil {
881883 return fmt .Errorf ("nil CPU" )
882884 }
883- ram := conversion .Int64ValueToPointer (flavor .RAM )
885+ ram := conversion .Int32ValueToPointer (flavor .RAM )
884886 if ram == nil {
885887 return fmt .Errorf ("nil RAM" )
886888 }
887889
888890 projectId := model .ProjectId .ValueString ()
889891 region := model .Region .ValueString ()
890- res , err := client .ListFlavorsExecute (ctx , projectId , region )
892+ req := client .ListFlavors (ctx , projectId , region )
893+ res , err := client .ListFlavorsExecute (req )
891894 if err != nil {
892895 return fmt .Errorf ("listing sqlserverflex flavors: %w" , err )
893896 }
@@ -896,7 +899,7 @@ func loadFlavorId(ctx context.Context, client sqlserverflexClient, model *Model,
896899 if res .Flavors == nil {
897900 return fmt .Errorf ("finding flavors for project %s" , projectId )
898901 }
899- for _ , f := range * res .Flavors {
902+ for _ , f := range res .Flavors {
900903 if f .Id == nil || f .Cpu == nil || f .Memory == nil {
901904 continue
902905 }
0 commit comments