Skip to content

Commit c53d946

Browse files
committed
fix logic helper
1 parent 65354f8 commit c53d946

File tree

2 files changed

+481
-505
lines changed

2 files changed

+481
-505
lines changed

internal/services/baremetal/helpers.go

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -227,64 +227,40 @@ func privateNetworkSetHash(v interface{}) int {
227227
return schema.HashString(buf.String())
228228
}
229229

230+
func getOfferInformations(ctx context.Context, offer interface{}, id string, i interface{}) (*baremetal.Offer, error) {
231+
api, zone, err := NewAPIWithZoneAndID(i, id)
232+
if err != nil {
233+
return nil, err
234+
}
235+
if validation.IsUUID(offer.(string)) {
236+
return api.GetOfferByName(&baremetal.GetOfferByNameRequest{
237+
OfferName: offer.(string),
238+
Zone: zone.Zone,
239+
})
240+
} else {
241+
offerID := regional.ExpandID(offer.(string))
242+
return FindOfferByID(ctx, api, zone.Zone, offerID.ID)
243+
}
244+
}
245+
230246
func customDiffOffer() func(ctx context.Context, diff *schema.ResourceDiff, i interface{}) error {
231247
return func(ctx context.Context, diff *schema.ResourceDiff, i interface{}) error {
232248
if diff.Get("offer") == "" || !diff.HasChange("offer") || diff.Id() == "" {
233249
return nil
234250
}
235251

236-
api, zone, err := NewAPIWithZoneAndID(i, diff.Id())
237-
if err != nil {
238-
return err
239-
}
240-
241252
oldOffer, newOffer := diff.GetChange("offer")
242-
if oldOffer == newOffer {
243-
return nil
244-
}
245-
246-
newOfferID := regional.ExpandID(newOffer.(string))
247-
oldOfferID := regional.ExpandID(oldOffer.(string))
248-
249-
if validation.IsUUID(oldOffer.(string)) {
250-
oldOfferInfo, err := api.GetOfferByName(&baremetal.GetOfferByNameRequest{
251-
OfferName: oldOffer.(string),
252-
Zone: zone.Zone,
253-
})
254-
if err != nil {
255-
return errors.New("can not find offer" + err.Error())
256-
}
257-
258-
oldOfferID = regional.ExpandID(oldOfferInfo.ID)
259-
}
260-
261-
if validation.IsUUID(newOffer.(string)) {
262-
newOfferInfo, err := api.GetOfferByName(&baremetal.GetOfferByNameRequest{
263-
OfferName: newOffer.(string),
264-
Zone: zone.Zone,
265-
})
266-
if err != nil {
267-
return errors.New("can not find offer" + err.Error())
268-
}
269-
270-
oldOfferID = regional.ExpandID(newOfferInfo.ID)
271-
}
272-
273-
oldOfferDetails, err := FindOfferByID(ctx, api, zone.Zone, oldOfferID.ID)
274-
if err != nil {
275-
return errors.New("can not find the offer by id" + err.Error())
276-
}
277-
278-
newOfferDetails, err := FindOfferByID(ctx, api, zone.Zone, newOfferID.ID)
253+
oldOfferInfo, err := getOfferInformations(ctx, oldOffer, diff.Id(), i)
254+
newOfferInfo, err := getOfferInformations(ctx, newOffer, diff.Id(), i)
279255
if err != nil {
280-
return errors.New("can not find the offer by id" + err.Error())
256+
return errors.New("can not find offer" + err.Error())
281257
}
282258

283-
if oldOfferDetails.Name != newOfferDetails.Name {
259+
if oldOfferInfo.Name != newOfferInfo.Name {
284260
return diff.ForceNew("offer")
285261
}
286262

287-
if oldOfferDetails.SubscriptionPeriod == baremetal.OfferSubscriptionPeriodMonthly && newOfferDetails.SubscriptionPeriod == baremetal.OfferSubscriptionPeriodHourly {
263+
if oldOfferInfo.SubscriptionPeriod == baremetal.OfferSubscriptionPeriodMonthly && newOfferInfo.SubscriptionPeriod == baremetal.OfferSubscriptionPeriodHourly {
288264
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")
289265
}
290266

internal/services/baremetal/testdata/server-update-subscription-period.cassette.yaml

Lines changed: 460 additions & 460 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)