Skip to content

Commit 61c998c

Browse files
committed
issue with partition
1 parent 53e4ae0 commit 61c998c

File tree

3 files changed

+481
-71
lines changed

3 files changed

+481
-71
lines changed

internal/services/baremetal/easy_partitioning_data_source.go

Lines changed: 252 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package baremetal
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
89
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
910
"github.com/scaleway/scaleway-sdk-go/scw"
1011
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
1112
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
12-
"strings"
1313
)
1414

1515
func DataEasyPartitioning() *schema.Resource {
@@ -34,86 +34,243 @@ func DataEasyPartitioning() *schema.Resource {
3434
Default: true,
3535
Description: "set swap partition",
3636
},
37-
"ext_4": {
37+
"extra_partition": {
3838
Type: schema.TypeBool,
3939
Optional: true,
4040
Default: true,
4141
Description: "set extra ext_4 partition",
4242
},
43-
"ext_4_name": { //TODO change to mount point
43+
"ext_4_mountpoint": { //TODO change to mount point
4444
Type: schema.TypeString,
4545
Optional: true,
46-
Default: "/data",
46+
Default: "/hello",
4747
Description: "Mount point must be an absolute path with alphanumeric characters and underscores",
4848
},
49-
"custom_partition": {
49+
"json_partition": {
5050
Type: schema.TypeString,
5151
Optional: true,
5252
Description: "The partitioning schema in json format",
5353
},
54+
"disks": {
55+
Type: schema.TypeList,
56+
Computed: true,
57+
Elem: &schema.Resource{
58+
Schema: map[string]*schema.Schema{
59+
"device": {
60+
Type: schema.TypeString,
61+
Computed: true,
62+
},
63+
"partitions": {
64+
Type: schema.TypeList,
65+
Computed: true,
66+
Elem: &schema.Resource{
67+
Schema: map[string]*schema.Schema{
68+
"label": {
69+
Type: schema.TypeString,
70+
Computed: true,
71+
},
72+
"number": {
73+
Type: schema.TypeInt,
74+
Computed: true,
75+
},
76+
"size": {
77+
Type: schema.TypeString, // scw.Size implements String()
78+
Computed: true,
79+
},
80+
"use_all_available_space": {
81+
Type: schema.TypeBool,
82+
Computed: true,
83+
},
84+
},
85+
},
86+
},
87+
},
88+
},
89+
},
90+
"raids": {
91+
Type: schema.TypeList,
92+
Computed: true,
93+
Elem: &schema.Resource{
94+
Schema: map[string]*schema.Schema{
95+
"name": {
96+
Type: schema.TypeString,
97+
Computed: true,
98+
},
99+
"level": {
100+
Type: schema.TypeString,
101+
Computed: true,
102+
},
103+
"devices": {
104+
Type: schema.TypeList,
105+
Computed: true,
106+
Elem: &schema.Schema{Type: schema.TypeString},
107+
},
108+
},
109+
},
110+
},
111+
"filesystems": {
112+
Type: schema.TypeList,
113+
Computed: true,
114+
Elem: &schema.Resource{
115+
Schema: map[string]*schema.Schema{
116+
"device": {
117+
Type: schema.TypeString,
118+
Computed: true,
119+
},
120+
"format": {
121+
Type: schema.TypeString,
122+
Computed: true,
123+
},
124+
"mountpoint": {
125+
Type: schema.TypeString,
126+
Computed: true,
127+
},
128+
},
129+
},
130+
},
54131
},
55132
}
56133
}
57134

58-
func removeSwap(defaultDisks []*baremetal.SchemaDisk, extraPartition bool) []*baremetal.SchemaDisk {
59-
var swapSize scw.Size
60-
var newPartition []*baremetal.SchemaPartition
61-
var newDisks []*baremetal.SchemaDisk
62-
var disk *baremetal.SchemaDisk
135+
func removeSwap(originalDisks []*baremetal.SchemaDisk, withExtraPartition bool) []*baremetal.SchemaDisk {
136+
var result []*baremetal.SchemaDisk
63137

64-
for _, oldDisk := range defaultDisks {
65-
for _, partition := range oldDisk.Partitions {
66-
if partition.Label == "swap" {
67-
swapSize = partition.Size
138+
for _, disk := range originalDisks {
139+
i := 1
140+
newPartitions := []*baremetal.SchemaPartition{}
141+
for _, p := range disk.Partitions {
142+
if p.Label == "swap" {
68143
continue
69144
}
70-
if partition.Label == "boot" && !extraPartition {
71-
partition.Size += swapSize
72-
} else if partition.Label == "boot" && extraPartition {
73-
partition.Size = 20000000000
145+
if p.Label == "root" {
146+
if !withExtraPartition {
147+
p.Size = 0
148+
p.UseAllAvailableSpace = true
149+
} else {
150+
p.Size = 20000000000
151+
}
74152
}
75-
newPartition = append(newPartition, partition)
153+
p.Number = uint32(i)
154+
i++
155+
newPartitions = append(newPartitions, p)
76156
}
77-
disk.Device = oldDisk.Device
78-
disk.Partitions = newPartition
79-
newDisks = append(newDisks, oldDisk)
157+
result = append(result, &baremetal.SchemaDisk{
158+
Device: disk.Device,
159+
Partitions: newPartitions,
160+
})
80161
}
81-
return newDisks
162+
return result
82163
}
83164

84-
"raids": [
85-
{
86-
"name": "/dev/md2",
87-
"level": "raid_level_1",
88-
"devices": [
89-
"/dev/nvme0n1p5",
90-
"/dev/nvme1n1p4"
91-
]
165+
func addExtraPartition(mountpoint string, newDisksSchema []*baremetal.SchemaDisk, defaultPartitionSchema *baremetal.Schema) *baremetal.Schema {
166+
raidDevices := []string{}
167+
168+
for _, disk := range newDisksSchema {
169+
partIndex := uint32(len(disk.Partitions)) + 1
170+
deviceIndex := partIndex + 1
171+
data := &baremetal.SchemaPartition{
172+
Label: baremetal.SchemaPartitionLabel("data"),
173+
Number: partIndex,
174+
Size: 0,
175+
UseAllAvailableSpace: true,
176+
}
177+
disk.Partitions = append(disk.Partitions, data)
178+
179+
device := fmt.Sprintf("%sp%d", disk.Device, deviceIndex)
180+
raidDevices = append(raidDevices, device)
181+
deviceIndex--
182+
}
183+
184+
filesystem := &baremetal.SchemaFilesystem{
185+
Device: "/dev/md2",
186+
Format: "ext4",
187+
Mountpoint: mountpoint,
188+
}
189+
defaultPartitionSchema.Filesystems = append(defaultPartitionSchema.Filesystems, filesystem)
190+
191+
raid := &baremetal.SchemaRAID{
192+
Name: "/dev/md2",
193+
Level: baremetal.SchemaRAIDLevelRaidLevel1,
194+
Devices: raidDevices,
195+
}
196+
defaultPartitionSchema.Raids = append(defaultPartitionSchema.Raids, raid)
197+
defaultPartitionSchema.Disks = newDisksSchema
198+
199+
return defaultPartitionSchema
92200
}
93-
],
94-
"filesystems": [
95-
{
96-
"device": "/dev/md2",
97-
"format": "ext4",
98-
"mountpoint": "/home"
201+
202+
func manageRootSize(originalDisks []*baremetal.SchemaDisk, withSwap bool, withExtraPartition bool) {
203+
for _, disk := range originalDisks {
204+
for _, partition := range disk.Partitions {
205+
if partition.Label == "root" {
206+
if !withSwap && !withExtraPartition {
207+
partition.Size = 0
208+
partition.UseAllAvailableSpace = true
209+
}
210+
if withExtraPartition {
211+
partition.Size = 20000000000
212+
}
213+
}
214+
}
215+
}
99216
}
100-
],
101217

102-
{
103-
"label": "data",
104-
"number": 4,
105-
"size": 0,
106-
"use_all_available_space": true
218+
func flattenDisksSchema(disks []*baremetal.SchemaDisk) []map[string]interface{} {
219+
var out []map[string]interface{}
220+
for _, d := range disks {
221+
if d == nil {
222+
continue
223+
}
224+
225+
parts := make([]map[string]interface{}, 0, len(d.Partitions))
226+
for _, p := range d.Partitions {
227+
if p == nil {
228+
continue
229+
}
230+
parts = append(parts, map[string]interface{}{
231+
"label": string(p.Label),
232+
"number": int(p.Number),
233+
"size": p.Size.String(),
234+
"use_all_available_space": p.UseAllAvailableSpace,
235+
})
236+
}
237+
238+
out = append(out, map[string]interface{}{
239+
"device": d.Device,
240+
"partitions": parts,
241+
})
242+
}
243+
return out
107244
}
108245

109-
func addExtraPartition(name string, extraPartition []*baremetal.SchemaDisk, defaultPartitionSchema *baremetal.Schema) *baremetal.Schema {
110-
_, label, _ := strings.Cut(name, "/")
111-
data := &baremetal.SchemaPartition{
112-
Label: "",
113-
Number: 0,
114-
Size: 0,
115-
UseAllAvailableSpace: false,
246+
func flattenRaids(raids []*baremetal.SchemaRAID) []map[string]interface{} {
247+
var out []map[string]interface{}
248+
for _, r := range raids {
249+
if r == nil {
250+
continue
251+
}
252+
out = append(out, map[string]interface{}{
253+
"name": r.Name,
254+
"level": string(r.Level),
255+
"devices": r.Devices,
256+
})
257+
}
258+
return out
259+
}
260+
261+
func flattenFilesystems(fsList []*baremetal.SchemaFilesystem) []map[string]interface{} {
262+
var out []map[string]interface{}
263+
for _, fs := range fsList {
264+
if fs == nil {
265+
continue
266+
}
267+
out = append(out, map[string]interface{}{
268+
"device": fs.Device,
269+
"format": string(fs.Format),
270+
"mountpoint": fs.Mountpoint,
271+
})
116272
}
273+
return out
117274
}
118275

119276
func dataEasyPartitioningRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
@@ -161,27 +318,57 @@ func dataEasyPartitioningRead(ctx context.Context, d *schema.ResourceData, m int
161318
}
162319

163320
extraPart := d.Get("extra_partition").(bool)
321+
swap := d.Get("swap").(bool)
322+
323+
if swap && !extraPart {
324+
jsonSchema, _ := json.Marshal(defaultPartitioningSchema)
325+
d.SetId(fmt.Sprintf("%s-%s", offerID, osID))
326+
_ = d.Set("json_partition", string(jsonSchema))
327+
_ = d.Set("disks", flattenDisksSchema(defaultPartitioningSchema.Disks))
328+
_ = d.Set("raids", flattenRaids(defaultPartitioningSchema.Raids))
329+
_ = d.Set("filesystems", flattenFilesystems(defaultPartitioningSchema.Filesystems))
330+
331+
return nil
332+
}
333+
334+
manageRootSize(defaultPartitioningSchema.Disks, swap, extraPart)
164335

165336
var newDiskSchema []*baremetal.SchemaDisk
166-
if swap := d.Get("swap"); !swap.(bool) {
337+
if !swap {
167338
newDiskSchema = removeSwap(defaultPartitioningSchema.Disks, extraPart)
168339
}
169340

170-
var newCustomPartition []*baremetal.Schema
341+
if newDiskSchema == nil {
342+
newDiskSchema = defaultPartitioningSchema.Disks
343+
}
344+
345+
var newCustomPartition *baremetal.Schema
171346
if extraPart {
172-
name := d.Get("ext_4_name").(string)
173-
newCustomPartition = addExtraPartition(name, newDiskSchema, defaultPartitioningSchema)
174-
}
175-
176-
defaultPartitioningSchema.Disks = append(defaultPartitioningSchema.Disks)
177-
//TODO checker si offer custom partitoning2l;
178-
//TODO checker si offer et os compatible
179-
//TODO get default partitioning
180-
//TODO remove swap and increase boot size
181-
//TODO
182-
//TODO unmarshall
183-
//TODO replacer les valeurs
184-
//TODO marshal
347+
mountpoint := d.Get("ext_4_mountpoint").(string)
348+
newCustomPartition = addExtraPartition(mountpoint, newDiskSchema, defaultPartitioningSchema)
349+
}
350+
351+
err = api.ValidatePartitioningSchema(&baremetal.ValidatePartitioningSchemaRequest{
352+
Zone: fallBackZone,
353+
OfferID: offerID,
354+
OsID: osID,
355+
PartitioningSchema: defaultPartitioningSchema,
356+
})
357+
358+
if err != nil {
359+
return diag.FromErr(err)
360+
}
361+
362+
jsonSchema, err := json.Marshal(newCustomPartition)
363+
if err != nil {
364+
return diag.FromErr(err)
365+
}
366+
367+
d.SetId(fmt.Sprintf("%s-%s", offerID, osID))
368+
_ = d.Set("json_partition", string(jsonSchema))
369+
_ = d.Set("disks", flattenDisksSchema(newCustomPartition.Disks))
370+
_ = d.Set("raids", flattenRaids(newCustomPartition.Raids))
371+
_ = d.Set("filesystems", flattenFilesystems(newCustomPartition.Filesystems))
185372

186373
return nil
187374
}

0 commit comments

Comments
 (0)