Skip to content

Commit 3caad29

Browse files
authored
fix(baremetal): refacto logic and debug raid (#3098)
* fix(baremetal): refacto logic and debug raid * fix index issue removeSwap and manage RAID * update cassettes * fix golangci-lint * refactor for loop updateRootSize and use const * rename datasource and update cassette * fix golangci-lin * correct provider * update documentation
1 parent ce4cc76 commit 3caad29

14 files changed

+24982
-3711
lines changed

docs/data-sources/baremetal_easy_partitioning.md renamed to docs/data-sources/baremetal_partition_schema.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
---
22
subcategory: "Elastic Metal"
3-
page_title: "Scaleway: scaleway_baremetal_easy_partitioning"
3+
page_title: "Scaleway: scaleway_baremetal_partition_schema"
44
---
55

6-
# scaleway_baremetal_easy_partitioning
6+
# scaleway_baremetal_partition_schema
77

8-
The scaleway_easy_partitioning data source allows you to retrieve a ready-to-use partitioning schema for a BareMetal server. This schema can be used for custom installations with optional swap and extra partitions.
8+
The scaleway_baremetal_partition_schema data source allows you to retrieve a ready-to-use partitioning schema for a BareMetal server. This schema can be used for custom installations with optional swap and extra partitions.
99

1010
This data source simplifies the process of generating valid partitioning configurations, especially useful when dealing with OS and offer compatibility requirements.
1111

12+
## Partitioning Details
13+
14+
The partitioning schema generated by the `scaleway_baremetal_partition_schema` data source includes a root (`/`) partition that is **20GB** in size by default.
15+
16+
If additional storage is required, you can enable the `extra_partition` option to mount extra space on a custom path (e.g., `/data`).
17+
18+
1219
## Example Usage
1320

1421
```hcl
15-
data "scaleway_easy_partitioning" "default" {
22+
data "scaleway_baremetal_partition_schema" "default" {
1623
offer_id = "11111111-1111-1111-1111-111111111111"
1724
os_id = "22222222-2222-2222-2222-222222222222"
1825
swap = true

internal/provider/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func Provider(config *Config) plugin.ProviderFunc {
250250
"scaleway_account_ssh_key": iam.DataSourceSSHKey(),
251251
"scaleway_availability_zones": az.DataSourceAvailabilityZones(),
252252
"scaleway_baremetal_offer": baremetal.DataSourceOffer(),
253-
"scaleway_baremetal_easy_partitioning": baremetal.DataEasyPartitioning(),
253+
"scaleway_baremetal_partition_schema": baremetal.DataPartitionSchema(),
254254
"scaleway_baremetal_option": baremetal.DataSourceOption(),
255255
"scaleway_baremetal_os": baremetal.DataSourceOS(),
256256
"scaleway_baremetal_server": baremetal.DataSourceServer(),

internal/services/baremetal/easy_partitioning_data_source.go renamed to internal/services/baremetal/partition_schema_data_source.go

Lines changed: 98 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"strings"
89

910
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1113
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
1214
"github.com/scaleway/scaleway-sdk-go/scw"
1315
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
@@ -16,12 +18,25 @@ import (
1618
)
1719

1820
const (
19-
partitionSize = 20000000000
21+
partitionSize = 20000000000
22+
defaultMountpoint = "/data"
23+
md0 = "/dev/md0"
24+
md1 = "/dev/md1"
25+
md2 = "/dev/md2"
26+
ext4 = "ext4"
27+
raidLevel1 = "raid_level_1"
28+
nvme0p2 = "/dev/nvme0n1p2"
29+
nvme0p3 = "/dev/nvme0n1p3"
30+
nvme1p1 = "/dev/nvme1n1p1"
31+
nvme1p2 = "/dev/nvme1n1p2"
32+
uefi = "uefi"
33+
swap = "swap"
34+
root = "root"
2035
)
2136

22-
func DataEasyPartitioning() *schema.Resource {
37+
func DataPartitionSchema() *schema.Resource {
2338
return &schema.Resource{
24-
ReadContext: dataEasyPartitioningRead,
39+
ReadContext: dataPartitionSchemaRead,
2540
Schema: map[string]*schema.Schema{
2641
"offer_id": {
2742
Type: schema.TypeString,
@@ -48,10 +63,11 @@ func DataEasyPartitioning() *schema.Resource {
4863
Description: "set extra ext_4 partition",
4964
},
5065
"ext_4_mountpoint": {
51-
Type: schema.TypeString,
52-
Optional: true,
53-
Default: "/data",
54-
Description: "Mount point must be an absolute path with alphanumeric characters and underscores",
66+
Type: schema.TypeString,
67+
Optional: true,
68+
Default: defaultMountpoint,
69+
ValidateFunc: validation.StringInSlice([]string{"/data", "/home"}, false),
70+
Description: "Mount point must be an absolute path",
5571
},
5672
"json_partition": {
5773
Type: schema.TypeString,
@@ -62,7 +78,7 @@ func DataEasyPartitioning() *schema.Resource {
6278
}
6379
}
6480

65-
func dataEasyPartitioningRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
81+
func dataPartitionSchemaRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
6682
api, fallBackZone, err := newAPIWithZone(d, m)
6783
if err != nil {
6884
return diag.FromErr(err)
@@ -105,30 +121,22 @@ func dataEasyPartitioningRead(ctx context.Context, d *schema.ResourceData, m int
105121
return diag.FromErr(err)
106122
}
107123

108-
extraPart := d.Get("extra_partition").(bool)
109-
swap := d.Get("swap").(bool)
110-
111-
if swap && !extraPart {
112-
jsonSchema, err := json.Marshal(defaultPartitioningSchema)
113-
if err != nil {
114-
return diag.FromErr(err)
115-
}
116-
117-
d.SetId(fmt.Sprintf("%s-%s", offerID, osID))
118-
_ = d.Set("json_partition", string(jsonSchema))
119-
120-
return nil
124+
hasSwap := d.Get("swap").(bool)
125+
if !hasSwap {
126+
removeSwap(defaultPartitioningSchema.Disks)
127+
updateRaidRemoveSwap(defaultPartitioningSchema)
121128
}
122129

123-
resizeRootPartition(defaultPartitioningSchema.Disks, swap, extraPart)
124-
defaultPartitioningSchema.Disks = handleSwapPartitions(defaultPartitioningSchema.Disks, extraPart, swap)
125-
126130
mountpoint := d.Get("ext_4_mountpoint").(string)
127-
addExtraExt4Partition(mountpoint, defaultPartitioningSchema, extraPart)
131+
_, hasExtraPartition := d.GetOk("extra_partition")
132+
133+
if hasExtraPartition {
134+
addExtraExt4Partition(mountpoint, defaultPartitioningSchema)
135+
updateRaidNewPartition(defaultPartitioningSchema)
136+
}
128137

129-
if !extraPart && !swap {
130-
defaultPartitioningSchema.Filesystems = defaultPartitioningSchema.Filesystems[:len(defaultPartitioningSchema.Filesystems)-1]
131-
defaultPartitioningSchema.Raids = defaultPartitioningSchema.Raids[:len(defaultPartitioningSchema.Raids)-1]
138+
if !hasSwap || hasExtraPartition {
139+
updateSizeRoot(defaultPartitioningSchema.Disks, hasExtraPartition)
132140
}
133141

134142
err = api.ValidatePartitioningSchema(&baremetal.ValidatePartitioningSchemaRequest{
@@ -155,55 +163,49 @@ func dataEasyPartitioningRead(ctx context.Context, d *schema.ResourceData, m int
155163
return nil
156164
}
157165

158-
func handleSwapPartitions(originalDisks []*baremetal.SchemaDisk, withExtraPartition bool, swap bool) []*baremetal.SchemaDisk {
159-
if swap {
160-
return originalDisks
166+
func updateRaidRemoveSwap(partitionSchema *baremetal.Schema) {
167+
raidSchema := []*baremetal.SchemaRAID{
168+
{
169+
Name: md0,
170+
Level: raidLevel1,
171+
Devices: []string{
172+
nvme0p2,
173+
nvme1p1,
174+
},
175+
},
176+
{
177+
Name: md1,
178+
Level: raidLevel1,
179+
Devices: []string{
180+
nvme0p3,
181+
nvme1p2,
182+
},
183+
},
161184
}
185+
partitionSchema.Raids = raidSchema
186+
}
162187

163-
result := make([]*baremetal.SchemaDisk, 0)
164-
165-
for _, disk := range originalDisks {
166-
i := 1
167-
newPartitions := []*baremetal.SchemaPartition{}
168-
169-
for _, p := range disk.Partitions {
170-
if p.Label == "swap" {
171-
continue
172-
}
173-
174-
if p.Label == "root" {
175-
if !withExtraPartition {
176-
p.Size = 0
177-
p.UseAllAvailableSpace = true
178-
} else {
179-
p.Size = partitionSize
180-
}
181-
}
182-
183-
p.Number = uint32(i)
184-
i++
185-
186-
newPartitions = append(newPartitions, p)
187-
}
188-
189-
result = append(result, &baremetal.SchemaDisk{
190-
Device: disk.Device,
191-
Partitions: newPartitions,
192-
})
188+
func updateRaidNewPartition(partitionSchema *baremetal.Schema) {
189+
lenDisk0Partition := len(partitionSchema.Disks[0].Partitions)
190+
lenDisk1Partition := len(partitionSchema.Disks[1].Partitions)
191+
raid := &baremetal.SchemaRAID{
192+
Name: md2,
193+
Level: raidLevel1,
194+
Devices: []string{
195+
fmt.Sprintf("%sp%d", partitionSchema.Disks[0].Device, lenDisk0Partition),
196+
fmt.Sprintf("%sp%d", partitionSchema.Disks[1].Device, lenDisk1Partition),
197+
},
193198
}
194-
195-
return result
199+
partitionSchema.Raids = append(partitionSchema.Raids, raid)
196200
}
197201

198-
func addExtraExt4Partition(mountpoint string, defaultPartitionSchema *baremetal.Schema, extraPart bool) {
199-
if !extraPart {
200-
return
201-
}
202+
func addExtraExt4Partition(mountpoint string, defaultPartitionSchema *baremetal.Schema) {
203+
label := strings.TrimPrefix(mountpoint, "/")
202204

203205
for _, disk := range defaultPartitionSchema.Disks {
204206
partIndex := uint32(len(disk.Partitions)) + 1
205207
data := &baremetal.SchemaPartition{
206-
Label: baremetal.SchemaPartitionLabel("data"),
208+
Label: baremetal.SchemaPartitionLabel(label),
207209
Number: partIndex,
208210
Size: 0,
209211
UseAllAvailableSpace: true,
@@ -212,26 +214,45 @@ func addExtraExt4Partition(mountpoint string, defaultPartitionSchema *baremetal.
212214
}
213215

214216
filesystem := &baremetal.SchemaFilesystem{
215-
Device: "/dev/md2",
216-
Format: "ext4",
217+
Device: md2,
218+
Format: ext4,
217219
Mountpoint: mountpoint,
218220
}
219221
defaultPartitionSchema.Filesystems = append(defaultPartitionSchema.Filesystems, filesystem)
220222
}
221223

222-
func resizeRootPartition(originalDisks []*baremetal.SchemaDisk, withSwap bool, withExtraPartition bool) {
224+
func updateSizeRoot(originalDisks []*baremetal.SchemaDisk, hasExtraPartition bool) {
223225
for _, disk := range originalDisks {
224226
for _, partition := range disk.Partitions {
225-
if partition.Label == "root" {
226-
if !withSwap && !withExtraPartition {
227-
partition.Size = 0
228-
partition.UseAllAvailableSpace = true
229-
}
227+
if partition.Label == root {
228+
partition.Size = 0
229+
partition.UseAllAvailableSpace = true
230230

231-
if withExtraPartition {
231+
if hasExtraPartition {
232232
partition.Size = partitionSize
233+
partition.UseAllAvailableSpace = false
233234
}
234235
}
235236
}
236237
}
237238
}
239+
240+
func removeSwap(originalDisks []*baremetal.SchemaDisk) {
241+
for _, disk := range originalDisks {
242+
newPartitions := make([]*baremetal.SchemaPartition, 0, len(disk.Partitions))
243+
244+
for _, partition := range disk.Partitions {
245+
if partition.Label == swap {
246+
continue
247+
}
248+
249+
if partition.Label != uefi {
250+
partition.Number--
251+
}
252+
253+
newPartitions = append(newPartitions, partition)
254+
}
255+
256+
disk.Partitions = newPartitions
257+
}
258+
}

0 commit comments

Comments
 (0)