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"
@@ -16,7 +17,8 @@ import (
16
17
)
17
18
18
19
const (
19
- partitionSize = 20000000000
20
+ partitionSize = 20000000000
21
+ defaultMountpoint = "/data"
20
22
)
21
23
22
24
func DataEasyPartitioning () * schema.Resource {
@@ -50,7 +52,7 @@ func DataEasyPartitioning() *schema.Resource {
50
52
"ext_4_mountpoint" : {
51
53
Type : schema .TypeString ,
52
54
Optional : true ,
53
- Default : "/data" ,
55
+ Default : defaultMountpoint ,
54
56
Description : "Mount point must be an absolute path with alphanumeric characters and underscores" ,
55
57
},
56
58
"json_partition" : {
@@ -105,30 +107,21 @@ func dataEasyPartitioningRead(ctx context.Context, d *schema.ResourceData, m int
105
107
return diag .FromErr (err )
106
108
}
107
109
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
110
+ hasSwap := d .Get ("hasSwap" ).(bool )
111
+ if ! hasSwap {
112
+ removeSwap (defaultPartitioningSchema .Disks )
121
113
}
122
114
123
- resizeRootPartition (defaultPartitioningSchema .Disks , swap , extraPart )
124
- defaultPartitioningSchema .Disks = handleSwapPartitions (defaultPartitioningSchema .Disks , extraPart , swap )
125
-
126
115
mountpoint := d .Get ("ext_4_mountpoint" ).(string )
127
- addExtraExt4Partition ( mountpoint , defaultPartitioningSchema , extraPart )
116
+ _ , hasExtraPartition := d . GetOk ( "extra_partition" )
128
117
129
- if ! extraPart && ! swap {
130
- defaultPartitioningSchema .Filesystems = defaultPartitioningSchema .Filesystems [:len (defaultPartitioningSchema .Filesystems )- 1 ]
131
- defaultPartitioningSchema .Raids = defaultPartitioningSchema .Raids [:len (defaultPartitioningSchema .Raids )- 1 ]
118
+ if hasExtraPartition {
119
+ addExtraExt4Partition (mountpoint , defaultPartitioningSchema )
120
+ updateRaid (defaultPartitioningSchema )
121
+ }
122
+
123
+ if ! hasSwap || hasExtraPartition {
124
+ updateSizeRoot (defaultPartitioningSchema .Disks , hasExtraPartition )
132
125
}
133
126
134
127
err = api .ValidatePartitioningSchema (& baremetal.ValidatePartitioningSchemaRequest {
@@ -155,55 +148,27 @@ func dataEasyPartitioningRead(ctx context.Context, d *schema.ResourceData, m int
155
148
return nil
156
149
}
157
150
158
- func handleSwapPartitions (originalDisks []* baremetal.SchemaDisk , withExtraPartition bool , swap bool ) []* baremetal.SchemaDisk {
159
- if swap {
160
- return originalDisks
161
- }
162
-
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
- })
151
+ func updateRaid (partitionSchema * baremetal.Schema ) {
152
+ lenDisk0Partition := len (partitionSchema .Disks [0 ].Partitions )
153
+ lenDisk1Partition := len (partitionSchema .Disks [1 ].Partitions )
154
+ raid := & baremetal.SchemaRAID {
155
+ Name : "/dev/md2" ,
156
+ Level : "raid_level_1" ,
157
+ Devices : []string {
158
+ fmt .Sprintf ("%sp%d" , partitionSchema .Disks [0 ].Device , lenDisk0Partition ),
159
+ fmt .Sprintf ("%sp%d" , partitionSchema .Disks [1 ].Device , lenDisk1Partition ),
160
+ },
193
161
}
194
-
195
- return result
162
+ partitionSchema .Raids = append (partitionSchema .Raids , raid )
196
163
}
197
164
198
- func addExtraExt4Partition (mountpoint string , defaultPartitionSchema * baremetal.Schema , extraPart bool ) {
199
- if ! extraPart {
200
- return
201
- }
165
+ func addExtraExt4Partition (mountpoint string , defaultPartitionSchema * baremetal.Schema ) {
166
+ label := strings .TrimPrefix (mountpoint , "/" )
202
167
203
168
for _ , disk := range defaultPartitionSchema .Disks {
204
169
partIndex := uint32 (len (disk .Partitions )) + 1
205
170
data := & baremetal.SchemaPartition {
206
- Label : baremetal .SchemaPartitionLabel ("data" ),
171
+ Label : baremetal .SchemaPartitionLabel (label ),
207
172
Number : partIndex ,
208
173
Size : 0 ,
209
174
UseAllAvailableSpace : true ,
@@ -219,18 +184,33 @@ func addExtraExt4Partition(mountpoint string, defaultPartitionSchema *baremetal.
219
184
defaultPartitionSchema .Filesystems = append (defaultPartitionSchema .Filesystems , filesystem )
220
185
}
221
186
222
- func resizeRootPartition (originalDisks []* baremetal.SchemaDisk , withSwap bool , withExtraPartition bool ) {
187
+ func updateSizeRoot (originalDisks []* baremetal.SchemaDisk , hasExtraPartition bool ) {
223
188
for _ , disk := range originalDisks {
224
189
for _ , partition := range disk .Partitions {
225
190
if partition .Label == "root" {
226
- if ! withSwap && ! withExtraPartition {
191
+ if hasExtraPartition {
192
+ partition .Size = partitionSize
193
+ partition .UseAllAvailableSpace = false
194
+ } else {
227
195
partition .Size = 0
228
196
partition .UseAllAvailableSpace = true
229
197
}
198
+ }
199
+ }
200
+ }
201
+ }
230
202
231
- if withExtraPartition {
232
- partition .Size = partitionSize
233
- }
203
+ func removeSwap (originalDisks []* baremetal.SchemaDisk ) {
204
+ for _ , disk := range originalDisks {
205
+ for i , partition := range disk .Partitions {
206
+ if partition .Label == "swap" {
207
+ disk .Partitions = append (disk .Partitions [:i ], disk .Partitions [i + 1 :]... )
208
+
209
+ break
210
+ }
211
+
212
+ if partition .Label != "uefi" {
213
+ partition .Number --
234
214
}
235
215
}
236
216
}
0 commit comments