Skip to content

Commit e72905d

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

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

go.mod

Lines changed: 3 additions & 3 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.20251126163841-f8e137ded5a4
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 (
@@ -87,7 +87,6 @@ require (
8787
github.com/fsnotify/fsnotify v1.8.0 // indirect
8888
github.com/go-logr/logr v1.4.3 // indirect
8989
github.com/go-logr/stdr v1.2.2 // indirect
90-
github.com/goccy/go-yaml v1.18.0 // indirect
9190
github.com/golang/protobuf v1.5.4 // indirect
9291
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
9392
github.com/gookit/color v1.5.1 // indirect
@@ -162,6 +161,7 @@ require (
162161
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.38.0 // indirect
163162
go.opentelemetry.io/otel/metric v1.38.0 // indirect
164163
go.opentelemetry.io/otel/trace v1.38.0 // indirect
164+
go.yaml.in/yaml/v4 v4.0.0-rc.3 // indirect
165165
golang.org/x/exp v0.0.0-20251009144603-d2f985daa21b // indirect
166166
golang.org/x/mod v0.29.0 // indirect
167167
golang.org/x/net v0.47.0 // indirect

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,6 @@ github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre
203203
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
204204
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
205205
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
206-
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
207-
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
208206
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
209207
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
210208
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
@@ -459,8 +457,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
459457
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
460458
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
461459
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
462-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.35.0.20251017125838-3eb0f2adaa94 h1:39j+3zweuYVml1Ozahl7RVb0AtpaiQtRztjq4W3GvNM=
463-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.35.0.20251017125838-3eb0f2adaa94/go.mod h1:SVm1Zk6UpZtqZN6KtEQpjC+v+Lir4tyVfhQTU19q3PA=
460+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.35.0.20251126163841-f8e137ded5a4 h1:6/n7fmgdMmeWXcnXdIMWhfvNQWg7EFpzP/roBAPItR4=
461+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.35.0.20251126163841-f8e137ded5a4/go.mod h1:h/elW4JdNo225GP5FZHJ/RPyCNz79lxaeub3PyGzE9o=
464462
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
465463
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
466464
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
@@ -548,6 +546,8 @@ go.opentelemetry.io/otel/trace v1.38.0 h1:Fxk5bKrDZJUH+AMyyIXGcFAPah0oRcT+LuNtJr
548546
go.opentelemetry.io/otel/trace v1.38.0/go.mod h1:j1P9ivuFsTceSWe1oY+EeW3sc+Pp42sO++GHkg4wwhs=
549547
go.opentelemetry.io/proto/otlp v1.7.1 h1:gTOMpGDb0WTBOP8JaO72iL3auEZhVmAQg4ipjOVAtj4=
550548
go.opentelemetry.io/proto/otlp v1.7.1/go.mod h1:b2rVh6rfI/s2pHWNlB7ILJcRALpcNDzKhACevjI+ZnE=
549+
go.yaml.in/yaml/v4 v4.0.0-rc.3 h1:3h1fjsh1CTAPjW7q/EMe+C8shx5d8ctzZTrLcs/j8Go=
550+
go.yaml.in/yaml/v4 v4.0.0-rc.3/go.mod h1:aZqd9kCMsGL7AuUv/m/PvWLdg5sjJsZ4oHDEnfPPfY0=
551551
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
552552
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
553553
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
@@ -897,8 +897,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
897897
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
898898
gopkg.in/dnaeon/go-vcr.v3 v3.2.0 h1:Rltp0Vf+Aq0u4rQXgmXgtgoRDStTnFN83cWgSGSoRzM=
899899
gopkg.in/dnaeon/go-vcr.v3 v3.2.0/go.mod h1:2IMOnnlx9I6u9x+YBsM3tAMx6AlOxnJ0pWxQAzZ79Ag=
900-
gopkg.in/dnaeon/go-vcr.v4 v4.0.5 h1:I0hpTIvD5rII+8LgYGrHMA2d4SQPoL6u7ZvJakWKsiA=
901-
gopkg.in/dnaeon/go-vcr.v4 v4.0.5/go.mod h1:dRos81TkW9C1WJt6tTaE+uV2Lo8qJT3AG2b35+CB/nQ=
900+
gopkg.in/dnaeon/go-vcr.v4 v4.0.6 h1:PiJkrakkmzc5s7EfBnZOnyiLwi7o7A9fwPzN0X2uwe0=
901+
gopkg.in/dnaeon/go-vcr.v4 v4.0.6/go.mod h1:sbq5oMEcM4PXngbcNbHhzfCP9OdZodLhrbRYoyg09HY=
902902
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
903903
gopkg.in/ini.v1 v1.66.4 h1:SsAcf+mM7mRZo2nJNGt8mZCjG8ZRaNGMURJw7BsIST4=
904904
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)