@@ -7,17 +7,17 @@ import (
77 "strconv"
88 "strings"
99
10+ "github.com/hashicorp/terraform-plugin-framework-validators/int32validator"
1011 "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
1112 "github.com/hashicorp/terraform-plugin-framework/diag"
13+ "github.com/hashicorp/terraform-plugin-framework/resource/schema/int32planmodifier"
1214
1315 serverbackupUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/serverbackup/utils"
1416
15- "github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
1617 "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1718 "github.com/hashicorp/terraform-plugin-framework/path"
1819 "github.com/hashicorp/terraform-plugin-framework/resource"
1920 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
20- "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
2121 "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
2222 "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
2323 "github.com/hashicorp/terraform-plugin-framework/schema/validator"
@@ -30,7 +30,7 @@ import (
3030 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
3131
3232 "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
33- "github.com/stackitcloud/stackit-sdk-go/services/serverbackup"
33+ serverbackup "github.com/stackitcloud/stackit-sdk-go/services/serverbackup/v2api "
3434)
3535
3636// Ensure the implementation satisfies the expected interfaces.
@@ -45,7 +45,7 @@ type Model struct {
4545 ID types.String `tfsdk:"id"`
4646 ProjectId types.String `tfsdk:"project_id"`
4747 ServerId types.String `tfsdk:"server_id"`
48- BackupScheduleId types.Int64 `tfsdk:"backup_schedule_id"`
48+ BackupScheduleId types.Int32 `tfsdk:"backup_schedule_id"`
4949 Name types.String `tfsdk:"name"`
5050 Rrule types.String `tfsdk:"rrule"`
5151 Enabled types.Bool `tfsdk:"enabled"`
@@ -56,7 +56,7 @@ type Model struct {
5656// scheduleBackupPropertiesModel maps schedule backup_properties data
5757type scheduleBackupPropertiesModel struct {
5858 BackupName types.String `tfsdk:"name"`
59- RetentionPeriod types.Int64 `tfsdk:"retention_period"`
59+ RetentionPeriod types.Int32 `tfsdk:"retention_period"`
6060 VolumeIds types.List `tfsdk:"volume_ids"`
6161}
6262
@@ -145,14 +145,14 @@ func (r *scheduleResource) Schema(_ context.Context, _ resource.SchemaRequest, r
145145 stringvalidator .LengthBetween (1 , 255 ),
146146 },
147147 },
148- "backup_schedule_id" : schema.Int64Attribute {
148+ "backup_schedule_id" : schema.Int32Attribute {
149149 Description : "Backup schedule ID." ,
150150 Computed : true ,
151- PlanModifiers : []planmodifier.Int64 {
152- int64planmodifier .UseStateForUnknown (),
151+ PlanModifiers : []planmodifier.Int32 {
152+ int32planmodifier .UseStateForUnknown (),
153153 },
154- Validators : []validator.Int64 {
155- int64validator .AtLeast (1 ),
154+ Validators : []validator.Int32 {
155+ int32validator .AtLeast (1 ),
156156 },
157157 },
158158 "project_id" : schema.StringAttribute {
@@ -209,10 +209,10 @@ func (r *scheduleResource) Schema(_ context.Context, _ resource.SchemaRequest, r
209209 "name" : schema.StringAttribute {
210210 Required : true ,
211211 },
212- "retention_period" : schema.Int64Attribute {
212+ "retention_period" : schema.Int32Attribute {
213213 Required : true ,
214- Validators : []validator.Int64 {
215- int64validator .AtLeast (1 ),
214+ Validators : []validator.Int32 {
215+ int32validator .AtLeast (1 ),
216216 },
217217 },
218218 },
@@ -268,15 +268,15 @@ func (r *scheduleResource) Create(ctx context.Context, req resource.CreateReques
268268 core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating server backup schedule" , fmt .Sprintf ("Creating API payload: %v" , err ))
269269 return
270270 }
271- scheduleResp , err := r .client .CreateBackupSchedule (ctx , projectId , serverId , region ).CreateBackupSchedulePayload (* payload ).Execute ()
271+ scheduleResp , err := r .client .DefaultAPI . CreateBackupSchedule (ctx , projectId , serverId , region ).CreateBackupSchedulePayload (* payload ).Execute ()
272272 if err != nil {
273273 core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating server backup schedule" , fmt .Sprintf ("Calling API: %v" , err ))
274274 return
275275 }
276276
277277 ctx = core .LogResponse (ctx )
278278
279- ctx = tflog .SetField (ctx , "backup_schedule_id" , * scheduleResp .Id )
279+ ctx = tflog .SetField (ctx , "backup_schedule_id" , scheduleResp .Id )
280280
281281 // Map response body to schema
282282 err = mapFields (ctx , scheduleResp , & model , region )
@@ -305,15 +305,15 @@ func (r *scheduleResource) Read(ctx context.Context, req resource.ReadRequest, r
305305
306306 projectId := model .ProjectId .ValueString ()
307307 serverId := model .ServerId .ValueString ()
308- backupScheduleId := model .BackupScheduleId .ValueInt64 ()
308+ backupScheduleId := model .BackupScheduleId .ValueInt32 ()
309309 region := r .providerData .GetRegionWithOverride (model .Region )
310310
311311 ctx = tflog .SetField (ctx , "project_id" , projectId )
312312 ctx = tflog .SetField (ctx , "server_id" , serverId )
313313 ctx = tflog .SetField (ctx , "backup_schedule_id" , backupScheduleId )
314314 ctx = tflog .SetField (ctx , "region" , region )
315315
316- scheduleResp , err := r .client .GetBackupSchedule (ctx , projectId , serverId , region , strconv .FormatInt (backupScheduleId , 10 )).Execute ()
316+ scheduleResp , err := r .client .DefaultAPI . GetBackupSchedule (ctx , projectId , serverId , region , strconv .FormatInt (int64 ( backupScheduleId ) , 10 )).Execute ()
317317 if err != nil {
318318 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
319319 if ok && oapiErr .StatusCode == http .StatusNotFound {
@@ -355,7 +355,7 @@ func (r *scheduleResource) Update(ctx context.Context, req resource.UpdateReques
355355
356356 projectId := model .ProjectId .ValueString ()
357357 serverId := model .ServerId .ValueString ()
358- backupScheduleId := model .BackupScheduleId .ValueInt64 ()
358+ backupScheduleId := model .BackupScheduleId .ValueInt32 ()
359359 region := r .providerData .GetRegionWithOverride (model .Region )
360360
361361 ctx = tflog .SetField (ctx , "project_id" , projectId )
@@ -370,7 +370,7 @@ func (r *scheduleResource) Update(ctx context.Context, req resource.UpdateReques
370370 return
371371 }
372372
373- scheduleResp , err := r .client .UpdateBackupSchedule (ctx , projectId , serverId , region , strconv .FormatInt (backupScheduleId , 10 )).UpdateBackupSchedulePayload (* payload ).Execute ()
373+ scheduleResp , err := r .client .DefaultAPI . UpdateBackupSchedule (ctx , projectId , serverId , region , strconv .FormatInt (int64 ( backupScheduleId ) , 10 )).UpdateBackupSchedulePayload (* payload ).Execute ()
374374 if err != nil {
375375 core .LogAndAddError (ctx , & resp .Diagnostics , "Error updating server backup schedule" , fmt .Sprintf ("Calling API: %v" , err ))
376376 return
@@ -405,15 +405,15 @@ func (r *scheduleResource) Delete(ctx context.Context, req resource.DeleteReques
405405
406406 projectId := model .ProjectId .ValueString ()
407407 serverId := model .ServerId .ValueString ()
408- backupScheduleId := model .BackupScheduleId .ValueInt64 ()
408+ backupScheduleId := model .BackupScheduleId .ValueInt32 ()
409409 region := r .providerData .GetRegionWithOverride (model .Region )
410410
411411 ctx = tflog .SetField (ctx , "project_id" , projectId )
412412 ctx = tflog .SetField (ctx , "server_id" , serverId )
413413 ctx = tflog .SetField (ctx , "backup_schedule_id" , backupScheduleId )
414414 ctx = tflog .SetField (ctx , "region" , region )
415415
416- err := r .client .DeleteBackupSchedule (ctx , projectId , serverId , region , strconv .FormatInt (backupScheduleId , 10 )).Execute ()
416+ err := r .client .DefaultAPI . DeleteBackupSchedule (ctx , projectId , serverId , region , strconv .FormatInt (int64 ( backupScheduleId ) , 10 )).Execute ()
417417 if err != nil {
418418 core .LogAndAddError (ctx , & resp .Diagnostics , "Error deleting server backup schedule" , fmt .Sprintf ("Calling API: %v" , err ))
419419 return
@@ -466,18 +466,15 @@ func mapFields(ctx context.Context, schedule *serverbackup.BackupSchedule, model
466466 if model == nil {
467467 return fmt .Errorf ("model input is nil" )
468468 }
469- if schedule .Id == nil {
470- return fmt .Errorf ("response id is nil" )
471- }
472469
473- model .BackupScheduleId = types .Int64PointerValue (schedule .Id )
470+ model .BackupScheduleId = types .Int32Value (schedule .Id )
474471 model .ID = utils .BuildInternalTerraformId (
475472 model .ProjectId .ValueString (), region , model .ServerId .ValueString (),
476- strconv .FormatInt (model .BackupScheduleId .ValueInt64 ( ), 10 ),
473+ strconv .FormatInt (int64 ( model .BackupScheduleId .ValueInt32 () ), 10 ),
477474 )
478- model .Name = types .StringPointerValue (schedule .Name )
479- model .Rrule = types .StringPointerValue (schedule .Rrule )
480- model .Enabled = types .BoolPointerValue (schedule .Enabled )
475+ model .Name = types .StringValue (schedule .Name )
476+ model .Rrule = types .StringValue (schedule .Rrule )
477+ model .Enabled = types .BoolValue (schedule .Enabled )
481478 if schedule .BackupProperties == nil {
482479 model .BackupProperties = nil
483480 return nil
@@ -494,7 +491,7 @@ func mapFields(ctx context.Context, schedule *serverbackup.BackupSchedule, model
494491 }
495492 }
496493
497- respVolIds := * schedule .BackupProperties .VolumeIds
494+ respVolIds := schedule .BackupProperties .VolumeIds
498495 reconciledVolIds := utils .ReconcileStringSlices (modelVolIds , respVolIds )
499496
500497 var diags diag.Diagnostics
@@ -504,8 +501,8 @@ func mapFields(ctx context.Context, schedule *serverbackup.BackupSchedule, model
504501 }
505502 }
506503 model .BackupProperties = & scheduleBackupPropertiesModel {
507- BackupName : types .StringValue (* schedule .BackupProperties .Name ),
508- RetentionPeriod : types .Int64Value ( * schedule .BackupProperties .RetentionPeriod ),
504+ BackupName : types .StringValue (schedule .BackupProperties .Name ),
505+ RetentionPeriod : types .Int32Value ( schedule .BackupProperties .RetentionPeriod ),
509506 VolumeIds : volIds ,
510507 }
511508 model .Region = types .StringValue (region )
@@ -521,7 +518,7 @@ func (r *scheduleResource) enableBackupsService(ctx context.Context, model *Mode
521518 region := r .providerData .GetRegionWithOverride (model .Region )
522519
523520 tflog .Debug (ctx , "Enabling server backup service" )
524- request := r .client .EnableServiceResource (ctx , projectId , serverId , region ).
521+ request := r .client .DefaultAPI . EnableServiceResource (ctx , projectId , serverId , region ).
525522 EnableServiceResourcePayload (serverbackup.EnableServiceResourcePayload {})
526523
527524 if err := request .Execute (); err != nil {
@@ -546,16 +543,16 @@ func (r *scheduleResource) disableBackupsService(ctx context.Context, model *Mod
546543 region := r .providerData .GetRegionWithOverride (model .Region )
547544
548545 tflog .Debug (ctx , "Checking for existing backups" )
549- backups , err := r .client .ListBackups (ctx , projectId , serverId , region ).Execute ()
546+ backups , err := r .client .DefaultAPI . ListBackups (ctx , projectId , serverId , region ).Execute ()
550547 if err != nil {
551548 return fmt .Errorf ("list backups: %w" , err )
552549 }
553- if len (* backups .Items ) > 0 {
550+ if len (backups .Items ) > 0 {
554551 tflog .Debug (ctx , "Backups found - will not disable server backup service" )
555552 return nil
556553 }
557554
558- err = r .client .DisableServiceResourceExecute (ctx , projectId , serverId , region )
555+ err = r .client .DefaultAPI . DisableServiceResource (ctx , projectId , serverId , region ). Execute ( )
559556 if err != nil {
560557 return fmt .Errorf ("disable server backup service: %w" , err )
561558 }
@@ -583,15 +580,15 @@ func toCreatePayload(model *Model) (*serverbackup.CreateBackupSchedulePayload, e
583580 ids = nil
584581 }
585582 backupProperties = serverbackup.BackupProperties {
586- Name : conversion . StringValueToPointer ( model .BackupProperties .BackupName ),
587- RetentionPeriod : conversion . Int64ValueToPointer ( model .BackupProperties .RetentionPeriod ),
588- VolumeIds : & ids ,
583+ Name : model .BackupProperties .BackupName . ValueString ( ),
584+ RetentionPeriod : model .BackupProperties .RetentionPeriod . ValueInt32 ( ),
585+ VolumeIds : ids ,
589586 }
590587 }
591588 return & serverbackup.CreateBackupSchedulePayload {
592- Enabled : conversion . BoolValueToPointer ( model .Enabled ),
593- Name : conversion . StringValueToPointer ( model .Name ),
594- Rrule : conversion . StringValueToPointer ( model .Rrule ),
589+ Enabled : model .Enabled . ValueBool ( ),
590+ Name : model .Name . ValueString ( ),
591+ Rrule : model .Rrule . ValueString ( ),
595592 BackupProperties : & backupProperties ,
596593 }, nil
597594}
@@ -616,16 +613,16 @@ func toUpdatePayload(model *Model) (*serverbackup.UpdateBackupSchedulePayload, e
616613 ids = nil
617614 }
618615 backupProperties = serverbackup.BackupProperties {
619- Name : conversion . StringValueToPointer ( model .BackupProperties .BackupName ),
620- RetentionPeriod : conversion . Int64ValueToPointer ( model .BackupProperties .RetentionPeriod ),
621- VolumeIds : & ids ,
616+ Name : model .BackupProperties .BackupName . ValueString ( ),
617+ RetentionPeriod : model .BackupProperties .RetentionPeriod . ValueInt32 ( ),
618+ VolumeIds : ids ,
622619 }
623620 }
624621
625622 return & serverbackup.UpdateBackupSchedulePayload {
626- Enabled : conversion . BoolValueToPointer ( model .Enabled ),
627- Name : conversion . StringValueToPointer ( model .Name ),
628- Rrule : conversion . StringValueToPointer ( model .Rrule ),
623+ Enabled : model .Enabled . ValueBool ( ),
624+ Name : model .Name . ValueString ( ),
625+ Rrule : model .Rrule . ValueString ( ),
629626 BackupProperties : & backupProperties ,
630627 }, nil
631628}
0 commit comments