Skip to content

Commit 234e82e

Browse files
committed
feat(baremetal): handle file cloud-init
bump sdk-go
1 parent 5c65640 commit 234e82e

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ require (
3131
github.com/nats-io/jwt/v2 v2.8.0
3232
github.com/nats-io/nats.go v1.46.1
3333
github.com/robfig/cron/v3 v3.0.1
34-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.35.0.20251017125838-3eb0f2adaa94
34+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.35.0.20251126083441-c306e91d6b43
3535
github.com/stretchr/testify v1.11.1
3636
golang.org/x/crypto v0.45.0
3737
golang.org/x/sync v0.18.0
3838
gopkg.in/dnaeon/go-vcr.v3 v3.2.0
39-
gopkg.in/dnaeon/go-vcr.v4 v4.0.5
39+
gopkg.in/dnaeon/go-vcr.v4 v4.0.6
4040
)
4141

4242
require (

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7
461461
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
462462
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.35.0.20251017125838-3eb0f2adaa94 h1:39j+3zweuYVml1Ozahl7RVb0AtpaiQtRztjq4W3GvNM=
463463
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.35.0.20251017125838-3eb0f2adaa94/go.mod h1:SVm1Zk6UpZtqZN6KtEQpjC+v+Lir4tyVfhQTU19q3PA=
464+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.35.0.20251126083441-c306e91d6b43 h1:MXwUsRKtGFvYZxVJz2Z8A+9l81Ag4TKoL6LpMLrNc+I=
465+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.35.0.20251126083441-c306e91d6b43/go.mod h1:h/elW4JdNo225GP5FZHJ/RPyCNz79lxaeub3PyGzE9o=
464466
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
465467
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
466468
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
@@ -899,6 +901,7 @@ gopkg.in/dnaeon/go-vcr.v3 v3.2.0 h1:Rltp0Vf+Aq0u4rQXgmXgtgoRDStTnFN83cWgSGSoRzM=
899901
gopkg.in/dnaeon/go-vcr.v3 v3.2.0/go.mod h1:2IMOnnlx9I6u9x+YBsM3tAMx6AlOxnJ0pWxQAzZ79Ag=
900902
gopkg.in/dnaeon/go-vcr.v4 v4.0.5 h1:I0hpTIvD5rII+8LgYGrHMA2d4SQPoL6u7ZvJakWKsiA=
901903
gopkg.in/dnaeon/go-vcr.v4 v4.0.5/go.mod h1:dRos81TkW9C1WJt6tTaE+uV2Lo8qJT3AG2b35+CB/nQ=
904+
gopkg.in/dnaeon/go-vcr.v4 v4.0.6/go.mod h1:sbq5oMEcM4PXngbcNbHhzfCP9OdZodLhrbRYoyg09HY=
902905
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
903906
gopkg.in/ini.v1 v1.66.4 h1:SsAcf+mM7mRZo2nJNGt8mZCjG8ZRaNGMURJw7BsIST4=
904907
gopkg.in/ini.v1 v1.66.4/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=

internal/services/baremetal/server.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package baremetal
22

33
import (
44
"context"
5+
"encoding/base64"
56
"encoding/json"
7+
"errors"
68
"fmt"
9+
"os"
710

811
"github.com/hashicorp/go-cty/cty"
912
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -297,6 +300,13 @@ If this behaviour is wanted, please set 'reinstall_on_ssh_key_changes' argument
297300
},
298301
},
299302
},
303+
"cloud_init": {
304+
Type: schema.TypeString,
305+
Optional: true,
306+
Computed: true,
307+
Description: "Configuration data to pass to cloud-init such as a YAML cloud config data or a user-data script",
308+
ValidateFunc: validation.StringLenBetween(0, 127998),
309+
},
300310
},
301311
CustomizeDiff: customdiff.Sequence(
302312
customDiffOffer(),
@@ -367,6 +377,14 @@ func ResourceServerCreate(ctx context.Context, d *schema.ResourceData, m any) di
367377
Protected: d.Get("protected").(bool),
368378
}
369379

380+
if cloudInit, ok := d.GetOk("cloud_init"); ok {
381+
userData, err := LoadUserDataBase64(cloudInit)
382+
if err != nil {
383+
return diag.FromErr(err)
384+
}
385+
req.UserData = &userData
386+
}
387+
370388
partitioningSchema := baremetal.Schema{}
371389

372390
if file, ok := d.GetOk("partitioning"); ok || !d.Get("install_config_afterward").(bool) {
@@ -457,6 +475,21 @@ func ResourceServerCreate(ctx context.Context, d *schema.ResourceData, m any) di
457475
return ResourceServerRead(ctx, d, m)
458476
}
459477

478+
func LoadUserDataBase64(cloudInit interface{}) ([]byte, error) {
479+
value := cloudInit.(string)
480+
var content []byte
481+
if data, err := os.ReadFile(value); errors.Is(err, os.ErrNotExist) {
482+
content = []byte(value)
483+
} else if err == nil {
484+
content = data
485+
} else {
486+
return nil, err
487+
}
488+
encoded := base64.StdEncoding.EncodeToString(content)
489+
userData := []byte(encoded)
490+
return userData, nil
491+
}
492+
460493
func ResourceServerRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
461494
api, zonedID, err := NewAPIWithZoneAndID(m, d.Id())
462495
if err != nil {
@@ -514,6 +547,7 @@ func ResourceServerRead(ctx context.Context, d *schema.ResourceData, m any) diag
514547
_ = d.Set("ipv4", flattenIPv4s(server.IPs))
515548
_ = d.Set("ipv6", flattenIPv6s(server.IPs))
516549
_ = d.Set("protected", server.Protected)
550+
_ = d.Set("cloud_init", server.UserData)
517551

518552
if server.Install != nil {
519553
_ = d.Set("os", zonal.NewIDString(server.Zone, os.ID))
@@ -696,6 +730,16 @@ func ResourceServerUpdate(ctx context.Context, d *schema.ResourceData, m any) di
696730

697731
hasChanged := false
698732

733+
if d.HasChange("cloud_init") {
734+
cloudInit := d.Get("cloud_init").(string)
735+
userData, err := LoadUserDataBase64(cloudInit)
736+
if err != nil {
737+
return diag.FromErr(err)
738+
}
739+
req.UserData = &userData
740+
hasChanged = true
741+
}
742+
699743
if d.HasChange("name") {
700744
req.Name = types.ExpandUpdatedStringPtr(d.Get("name"))
701745
hasChanged = true

0 commit comments

Comments
 (0)