@@ -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
1820const (
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