55 "context"
66 "errors"
77 "fmt"
8- "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
98 "sort"
109 "time"
1110
@@ -15,6 +14,7 @@ import (
1514 "github.com/scaleway/scaleway-sdk-go/scw"
1615 "github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1716 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
17+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1818 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
1919 "github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
2020 "github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
@@ -231,6 +231,7 @@ func customDiffOffer() func(ctx context.Context, diff *schema.ResourceDiff, i in
231231 if diff .Get ("offer" ) == "" || ! diff .HasChange ("offer" ) || diff .Id () == "" {
232232 return nil
233233 }
234+
234235 api , zone , err := NewAPIWithZoneAndID (i , diff .Id ())
235236 if err != nil {
236237 return err
@@ -239,6 +240,7 @@ func customDiffOffer() func(ctx context.Context, diff *schema.ResourceDiff, i in
239240 oldOffer , newOffer := diff .GetChange ("offer" )
240241 newOfferID := regional .ExpandID (newOffer .(string ))
241242 oldOfferID := regional .ExpandID (oldOffer .(string ))
243+
242244 oldOfferDetails , err := FindOfferByID (ctx , api , zone .Zone , oldOfferID .ID )
243245 if err != nil {
244246 return errors .New ("can not find the offer by id" + err .Error ())
@@ -248,20 +250,25 @@ func customDiffOffer() func(ctx context.Context, diff *schema.ResourceDiff, i in
248250 if err != nil {
249251 return errors .New ("can not find the offer by id" + err .Error ())
250252 }
253+
251254 if oldOfferDetails .Name != newOfferDetails .Name {
252255 return diff .ForceNew ("offer" )
253256 }
257+
254258 if oldOfferDetails .SubscriptionPeriod == baremetal .OfferSubscriptionPeriodMonthly && newOfferDetails .SubscriptionPeriod == baremetal .OfferSubscriptionPeriodHourly {
255259 return errors .New ("invalid plan transition: you cannot transition from a monthly plan to an hourly plan. Only the reverse (hourly to monthly) is supported. Please update your configuration accordingly" )
256260 }
261+
257262 ServerID := regional .ExpandID (diff .Id ())
263+
258264 _ , err = api .MigrateServerToMonthlyOffer (& baremetal.MigrateServerToMonthlyOfferRequest {
259265 Zone : zone .Zone ,
260266 ServerID : ServerID .ID ,
261267 }, scw .WithContext (ctx ))
262268 if err != nil {
263269 return errors .New ("migration to monthly plan failed: " + err .Error ())
264270 }
271+
265272 return nil
266273 }
267274}
0 commit comments