@@ -5,9 +5,11 @@ import (
5
5
"encoding/json"
6
6
"errors"
7
7
"fmt"
8
+ "strings"
8
9
9
10
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10
11
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
11
13
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
12
14
"github.com/scaleway/scaleway-sdk-go/scw"
13
15
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
@@ -16,12 +18,25 @@ import (
16
18
)
17
19
18
20
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"
20
35
)
21
36
22
- func DataEasyPartitioning () * schema.Resource {
37
+ func DataPartitionSchema () * schema.Resource {
23
38
return & schema.Resource {
24
- ReadContext : dataEasyPartitioningRead ,
39
+ ReadContext : dataPartitionSchemaRead ,
25
40
Schema : map [string ]* schema.Schema {
26
41
"offer_id" : {
27
42
Type : schema .TypeString ,
@@ -48,10 +63,11 @@ func DataEasyPartitioning() *schema.Resource {
48
63
Description : "set extra ext_4 partition" ,
49
64
},
50
65
"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" ,
55
71
},
56
72
"json_partition" : {
57
73
Type : schema .TypeString ,
@@ -62,7 +78,7 @@ func DataEasyPartitioning() *schema.Resource {
62
78
}
63
79
}
64
80
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 {
66
82
api , fallBackZone , err := newAPIWithZone (d , m )
67
83
if err != nil {
68
84
return diag .FromErr (err )
@@ -105,30 +121,22 @@ func dataEasyPartitioningRead(ctx context.Context, d *schema.ResourceData, m int
105
121
return diag .FromErr (err )
106
122
}
107
123
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 )
121
128
}
122
129
123
- resizeRootPartition (defaultPartitioningSchema .Disks , swap , extraPart )
124
- defaultPartitioningSchema .Disks = handleSwapPartitions (defaultPartitioningSchema .Disks , extraPart , swap )
125
-
126
130
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
+ }
128
137
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 )
132
140
}
133
141
134
142
err = api .ValidatePartitioningSchema (& baremetal.ValidatePartitioningSchemaRequest {
@@ -155,55 +163,49 @@ func dataEasyPartitioningRead(ctx context.Context, d *schema.ResourceData, m int
155
163
return nil
156
164
}
157
165
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
+ },
161
184
}
185
+ partitionSchema .Raids = raidSchema
186
+ }
162
187
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
+ },
193
198
}
194
-
195
- return result
199
+ partitionSchema .Raids = append (partitionSchema .Raids , raid )
196
200
}
197
201
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 , "/" )
202
204
203
205
for _ , disk := range defaultPartitionSchema .Disks {
204
206
partIndex := uint32 (len (disk .Partitions )) + 1
205
207
data := & baremetal.SchemaPartition {
206
- Label : baremetal .SchemaPartitionLabel ("data" ),
208
+ Label : baremetal .SchemaPartitionLabel (label ),
207
209
Number : partIndex ,
208
210
Size : 0 ,
209
211
UseAllAvailableSpace : true ,
@@ -212,26 +214,45 @@ func addExtraExt4Partition(mountpoint string, defaultPartitionSchema *baremetal.
212
214
}
213
215
214
216
filesystem := & baremetal.SchemaFilesystem {
215
- Device : "/dev/ md2" ,
216
- Format : " ext4" ,
217
+ Device : md2 ,
218
+ Format : ext4 ,
217
219
Mountpoint : mountpoint ,
218
220
}
219
221
defaultPartitionSchema .Filesystems = append (defaultPartitionSchema .Filesystems , filesystem )
220
222
}
221
223
222
- func resizeRootPartition (originalDisks []* baremetal.SchemaDisk , withSwap bool , withExtraPartition bool ) {
224
+ func updateSizeRoot (originalDisks []* baremetal.SchemaDisk , hasExtraPartition bool ) {
223
225
for _ , disk := range originalDisks {
224
226
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
230
230
231
- if withExtraPartition {
231
+ if hasExtraPartition {
232
232
partition .Size = partitionSize
233
+ partition .UseAllAvailableSpace = false
233
234
}
234
235
}
235
236
}
236
237
}
237
238
}
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