Skip to content

Commit d873d3a

Browse files
committed
feat(baremetal): migration to monthly
1 parent b8dca3e commit d873d3a

File tree

5 files changed

+157
-3
lines changed

5 files changed

+157
-3
lines changed

internal/services/baremetal/helpers.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"context"
66
"errors"
77
"fmt"
8+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
9+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/logging"
810
"sort"
911
"time"
1012

@@ -224,3 +226,46 @@ func privateNetworkSetHash(v interface{}) int {
224226

225227
return schema.HashString(buf.String())
226228
}
229+
230+
func customDiffOffer() func(ctx context.Context, diff *schema.ResourceDiff, i interface{}) error {
231+
return func(ctx context.Context, diff *schema.ResourceDiff, i interface{}) error {
232+
logging.L.Debugf("customDiffOffer() function")
233+
234+
if diff.Get("offer") == "" || !diff.HasChange("offer") || diff.Id() == "" {
235+
return nil
236+
}
237+
api, zone, err := NewAPIWithZoneAndID(i, diff.Id())
238+
logging.L.Debugf("value of api is %v and zone is %v", api, zone)
239+
if err != nil {
240+
return err
241+
}
242+
243+
oldOffer, newOffer := diff.GetChange("offer")
244+
newOfferID := regional.ExpandID(newOffer.(string))
245+
oldOfferID := regional.ExpandID(oldOffer.(string))
246+
oldOfferDetails, err := FindOfferByID(ctx, api, zone.Zone, oldOfferID.ID)
247+
if err != nil {
248+
return errors.New("can not find the offer by id" + err.Error())
249+
}
250+
251+
newOfferDetails, err := FindOfferByID(ctx, api, zone.Zone, newOfferID.ID)
252+
if err != nil {
253+
return errors.New("can not find the offer by id" + err.Error())
254+
}
255+
if oldOfferDetails.Name != newOfferDetails.Name {
256+
return diff.ForceNew("offer")
257+
}
258+
if oldOfferDetails.SubscriptionPeriod == baremetal.OfferSubscriptionPeriodMonthly && newOfferDetails.SubscriptionPeriod == baremetal.OfferSubscriptionPeriodHourly {
259+
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")
260+
}
261+
ServerID := regional.ExpandID(diff.Id())
262+
_, err = api.MigrateServerToMonthlyOffer(&baremetal.MigrateServerToMonthlyOfferRequest{
263+
Zone: zone.Zone,
264+
ServerID: ServerID.ID,
265+
}, scw.WithContext(ctx))
266+
if err != nil {
267+
return errors.New("migration to monthly plan failed: " + err.Error())
268+
}
269+
return nil
270+
}
271+
}

internal/services/baremetal/offer_data_source_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ func TestAccDataSourceOffer_SubscriptionPeriodMonthly(t *testing.T) {
131131
tt := acctest.NewTestTools(t)
132132
defer tt.Cleanup()
133133

134-
if !IsOfferAvailable(OfferID, Zone, tt) {
135-
t.Skip("Offer is out of stock")
136-
}
134+
//if !IsOfferAvailable(OfferID, Zone, tt) {
135+
// t.Skip("Offer is out of stock")
136+
//}
137137

138138
resource.ParallelTest(t, resource.TestCase{
139139
PreCheck: func() { acctest.PreCheck(t) },

internal/services/baremetal/server.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func ResourceServer() *schema.Resource {
5454
Description: "Hostname of the server",
5555
},
5656
"offer": {
57+
<<<<<<< Updated upstream
5758
Type: schema.TypeString,
5859
Required: true,
5960
ForceNew: true,
@@ -68,6 +69,13 @@ func ResourceServer() *schema.Resource {
6869

6970
return ok && newValue == offerName
7071
},
72+
=======
73+
Type: schema.TypeString,
74+
Required: true,
75+
ForceNew: true,
76+
Description: "ID or name of the server offer",
77+
ValidateDiagFunc: verify.IsUUIDOrNameOffer(),
78+
>>>>>>> Stashed changes
7179
},
7280
"offer_id": {
7381
Type: schema.TypeString,
@@ -266,6 +274,7 @@ If this behaviour is wanted, please set 'reinstall_on_ssh_key_changes' argument
266274

267275
CustomizeDiff: customdiff.Sequence(
268276
cdf.LocalityCheck("private_network.#.id"),
277+
customDiffOffer(),
269278
customDiffPrivateNetworkOption(),
270279
),
271280
}

internal/services/baremetal/server_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,103 @@ func TestAccServer_WithIPAMPrivateNetwork(t *testing.T) {
10391039
})
10401040
}
10411041

1042+
func TestAccServer_UpdateSubscriptionPeriod(t *testing.T) {
1043+
tt := acctest.NewTestTools(t)
1044+
defer tt.Cleanup()
1045+
1046+
//if !IsOfferAvailable(OfferID, "fr-par-2", tt) {
1047+
// t.Skip("Offer is out of stock")
1048+
//}
1049+
1050+
resource.ParallelTest(t, resource.TestCase{
1051+
PreCheck: func() { acctest.PreCheck(t) },
1052+
ProviderFactories: tt.ProviderFactories,
1053+
CheckDestroy: resource.ComposeTestCheckFunc(
1054+
baremetalchecks.CheckServerDestroy(tt),
1055+
),
1056+
Steps: []resource.TestStep{
1057+
{
1058+
Config: fmt.Sprintf(`
1059+
1060+
data "scaleway_baremetal_offer" "my_offer" {
1061+
zone = "fr-par-2"
1062+
name = "EM-B112X-SSD"
1063+
subscription_period = "hourly"
1064+
1065+
}
1066+
1067+
resource "scaleway_baremetal_server" "server01" {
1068+
name = "TestAccServer_UpdateSubscriptionPeriod"
1069+
offer = data.scaleway_baremetal_offer.my_offer.offer_id
1070+
zone = "fr-par-2"
1071+
install_config_afterward = true
1072+
1073+
}`,
1074+
),
1075+
Check: resource.ComposeTestCheckFunc(
1076+
resource.TestCheckResourceAttr("scaleway_baremetal_server.server01", "subscription_period", "hourly"),
1077+
resource.TestCheckResourceAttr("scaleway_baremetal_server.server01", "zone", "fr-par-2"),
1078+
),
1079+
},
1080+
{
1081+
Config: fmt.Sprintf(`
1082+
data "scaleway_baremetal_offer" "my_offer" {
1083+
zone = "fr-par-2"
1084+
name = "EM-B112X-SSD"
1085+
subscription_period = "hourly"
1086+
1087+
}
1088+
1089+
data "scaleway_baremetal_offer" "my_offer_monthly" {
1090+
zone = "fr-par-2"
1091+
name = "EM-B112X-SSD"
1092+
subscription_period = "monthly"
1093+
1094+
}
1095+
1096+
resource "scaleway_baremetal_server" "server01" {
1097+
name = "TestAccServer_UpdateSubscriptionPeriod"
1098+
offer = data.scaleway_baremetal_offer.my_offer_monthly.offer_id
1099+
zone = "fr-par-2"
1100+
install_config_afterward = true
1101+
1102+
}`,
1103+
),
1104+
Check: resource.ComposeTestCheckFunc(
1105+
resource.TestCheckResourceAttr("scaleway_baremetal_server.server01", "subscription_period", "monthly"),
1106+
resource.TestCheckResourceAttr("scaleway_baremetal_server.server01", "zone", "fr-par-2"),
1107+
),
1108+
},
1109+
{
1110+
Config: fmt.Sprintf(`
1111+
data "scaleway_baremetal_offer" "my_offer" {
1112+
zone = "fr-par-2"
1113+
name = "EM-B112X-SSD"
1114+
subscription_period = "hourly"
1115+
1116+
}
1117+
data "scaleway_baremetal_offer" "my_offer_monthly" {
1118+
zone = "fr-par-2"
1119+
name = "EM-B112X-SSD"
1120+
subscription_period = "monthly"
1121+
1122+
}
1123+
1124+
resource "scaleway_baremetal_server" "server01" {
1125+
name = "Test_UpdateSubscriptionPeriod"
1126+
offer = data.scaleway_baremetal_offer.my_offer.offer_id
1127+
zone = "fr-par-2"
1128+
install_config_afterward = true
1129+
1130+
}`,
1131+
),
1132+
ExpectError: regexp.MustCompile("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"),
1133+
},
1134+
},
1135+
})
1136+
1137+
}
1138+
10421139
func testAccCheckBaremetalServerExists(tt *acctest.TestTools, n string) resource.TestCheckFunc {
10431140
return func(s *terraform.State) error {
10441141
rs, ok := s.RootModule().Resources[n]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
version: 2
3+
interactions: []

0 commit comments

Comments
 (0)