Skip to content

Commit 54de226

Browse files
committed
feat(apple-silicon): commitment
1 parent cc21f51 commit 54de226

File tree

2 files changed

+85
-4
lines changed

2 files changed

+85
-4
lines changed

internal/services/applesilicon/server.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ func ResourceServer() *schema.Resource {
5050
Default: false,
5151
Description: "Whether or not to enable VPC access",
5252
},
53+
"commitment": {
54+
Type: schema.TypeString,
55+
Optional: true,
56+
Default: "duration_24h",
57+
Description: "The commitment period of the server",
58+
ValidateDiagFunc: verify.ValidateEnum[applesilicon.CommitmentType](),
59+
},
5360
"private_network": {
5461
Type: schema.TypeSet,
5562
Optional: true,
@@ -151,10 +158,11 @@ func ResourceAppleSiliconServerCreate(ctx context.Context, d *schema.ResourceDat
151158
}
152159

153160
createReq := &applesilicon.CreateServerRequest{
154-
Name: types.ExpandOrGenerateString(d.Get("name"), "m1"),
155-
Type: d.Get("type").(string),
156-
ProjectID: d.Get("project_id").(string),
157-
EnableVpc: d.Get("enable_vpc").(bool),
161+
Name: types.ExpandOrGenerateString(d.Get("name"), "m1"),
162+
Type: d.Get("type").(string),
163+
ProjectID: d.Get("project_id").(string),
164+
EnableVpc: d.Get("enable_vpc").(bool),
165+
CommitmentType: applesilicon.CommitmentType(d.Get("commitment").(string)),
158166
}
159167

160168
res, err := asAPI.CreateServer(createReq, scw.WithContext(ctx))
@@ -264,6 +272,10 @@ func ResourceAppleSiliconServerUpdate(ctx context.Context, d *schema.ResourceDat
264272
req.Name = types.ExpandStringPtr(d.Get("name"))
265273
}
266274

275+
if d.HasChange("commitment") {
276+
req.CommitmentType = &applesilicon.CommitmentTypeValue{CommitmentType: applesilicon.CommitmentType(d.Get("commitment").(string))}
277+
}
278+
267279
if d.HasChange("enable_vpc") {
268280
enableVpc := d.Get("enable_vpc").(bool)
269281
req.EnableVpc = &enableVpc

internal/services/applesilicon/server_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package applesilicon_test
22

33
import (
44
"fmt"
5+
"regexp"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -128,6 +129,7 @@ func TestAccServer_EnableVPC(t *testing.T) {
128129
isServerPresent(tt, "scaleway_apple_silicon_server.main"),
129130
resource.TestCheckResourceAttr("scaleway_apple_silicon_server.main", "name", "TestAccServerEnableVPC"),
130131
resource.TestCheckResourceAttr("scaleway_apple_silicon_server.main", "type", "M2-M"),
132+
resource.TestCheckResourceAttr("scaleway_apple_silicon_server.main", "commitment", "duration_24h"),
131133
// Computed
132134
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "ip"),
133135
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "vnc_url"),
@@ -225,6 +227,73 @@ func TestAccServer_EnableVPC(t *testing.T) {
225227
})
226228
}
227229

230+
func TestAccServer_Commitment(t *testing.T) {
231+
tt := acctest.NewTestTools(t)
232+
defer tt.Cleanup()
233+
resource.ParallelTest(t, resource.TestCase{
234+
PreCheck: func() { acctest.PreCheck(t) },
235+
ProviderFactories: tt.ProviderFactories,
236+
CheckDestroy: isServerDestroyed(tt),
237+
Steps: []resource.TestStep{
238+
{
239+
Config: `
240+
241+
resource scaleway_apple_silicon_server main {
242+
name = "TestAccServerEnableDisableVPC"
243+
type = "M2-M"
244+
zone = "fr-par-3"
245+
}
246+
`,
247+
Check: resource.ComposeTestCheckFunc(
248+
isServerPresent(tt, "scaleway_apple_silicon_server.main"),
249+
resource.TestCheckResourceAttr("scaleway_apple_silicon_server.main", "name", "TestAccServerEnableDisableVPC"),
250+
resource.TestCheckResourceAttr("scaleway_apple_silicon_server.main", "type", "M2-M"),
251+
resource.TestCheckResourceAttr("scaleway_apple_silicon_server.main", "commitment", "duration_24h"),
252+
// Computed
253+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "ip"),
254+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "vnc_url"),
255+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "created_at"),
256+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "deletable_at"),
257+
),
258+
},
259+
{
260+
Config: `
261+
262+
resource scaleway_apple_silicon_server main {
263+
name = "TestAccServerEnableDisableVPC"
264+
type = "M2-M"
265+
commitment = "renewed_monthly"
266+
zone = "fr-par-3"
267+
}
268+
`,
269+
Check: resource.ComposeTestCheckFunc(
270+
isServerPresent(tt, "scaleway_apple_silicon_server.main"),
271+
resource.TestCheckResourceAttr("scaleway_apple_silicon_server.main", "name", "TestAccServerEnableDisableVPC"),
272+
resource.TestCheckResourceAttr("scaleway_apple_silicon_server.main", "type", "M2-M"),
273+
resource.TestCheckResourceAttr("scaleway_apple_silicon_server.main", "commitment", "renewed_monthly"),
274+
// Computed
275+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "ip"),
276+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "vnc_url"),
277+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "created_at"),
278+
resource.TestCheckResourceAttrSet("scaleway_apple_silicon_server.main", "deletable_at"),
279+
),
280+
},
281+
{
282+
Config: `
283+
284+
resource scaleway_apple_silicon_server main {
285+
name = "TestAccServerEnableDisableVPC"
286+
type = "M2-M"
287+
commitment = "duration_24h"
288+
zone = "fr-par-3"
289+
}
290+
`,
291+
ExpectError: regexp.MustCompile("can not commit from monthly to hourly changes to the server"),
292+
},
293+
},
294+
})
295+
}
296+
228297
func isServerPresent(tt *acctest.TestTools, n string) resource.TestCheckFunc {
229298
return func(s *terraform.State) error {
230299
rs, ok := s.RootModule().Resources[n]

0 commit comments

Comments
 (0)