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"
@@ -16,7 +17,8 @@ import (
1617)
1718
1819const (
19- partitionSize = 20000000000
20+ partitionSize = 20000000000
21+ defaultMountpoint = "/data"
2022)
2123
2224func DataEasyPartitioning () * schema.Resource {
@@ -50,7 +52,7 @@ func DataEasyPartitioning() *schema.Resource {
5052 "ext_4_mountpoint" : {
5153 Type : schema .TypeString ,
5254 Optional : true ,
53- Default : "/data" ,
55+ Default : defaultMountpoint ,
5456 Description : "Mount point must be an absolute path with alphanumeric characters and underscores" ,
5557 },
5658 "json_partition" : {
@@ -105,30 +107,21 @@ func dataEasyPartitioningRead(ctx context.Context, d *schema.ResourceData, m int
105107 return diag .FromErr (err )
106108 }
107109
108- extraPart := d .Get ("extra_partition" ).(bool )
109110 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
111+ if ! swap {
112+ removeSwap (defaultPartitioningSchema .Disks )
121113 }
122114
123- resizeRootPartition (defaultPartitioningSchema .Disks , swap , extraPart )
124- defaultPartitioningSchema .Disks = handleSwapPartitions (defaultPartitioningSchema .Disks , extraPart , swap )
125-
126115 mountpoint := d .Get ("ext_4_mountpoint" ).(string )
127- addExtraExt4Partition ( mountpoint , defaultPartitioningSchema , extraPart )
116+ _ , ok := d . GetOk ( "extra_partition" )
128117
129- if ! extraPart && ! swap {
130- defaultPartitioningSchema .Filesystems = defaultPartitioningSchema .Filesystems [:len (defaultPartitioningSchema .Filesystems )- 1 ]
131- defaultPartitioningSchema .Raids = defaultPartitioningSchema .Raids [:len (defaultPartitioningSchema .Raids )- 1 ]
118+ if ok {
119+ addExtraExt4Partition (mountpoint , defaultPartitioningSchema )
120+ updateRaid (defaultPartitioningSchema )
121+ }
122+
123+ if ! swap || ok {
124+ updateSizeRoot (defaultPartitioningSchema .Disks , ok )
132125 }
133126
134127 err = api .ValidatePartitioningSchema (& baremetal.ValidatePartitioningSchemaRequest {
@@ -155,55 +148,27 @@ func dataEasyPartitioningRead(ctx context.Context, d *schema.ResourceData, m int
155148 return nil
156149}
157150
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+ },
193161 }
194-
195- return result
162+ partitionSchema .Raids = append (partitionSchema .Raids , raid )
196163}
197164
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 , "/" )
202167
203168 for _ , disk := range defaultPartitionSchema .Disks {
204169 partIndex := uint32 (len (disk .Partitions )) + 1
205170 data := & baremetal.SchemaPartition {
206- Label : baremetal .SchemaPartitionLabel ("data" ),
171+ Label : baremetal .SchemaPartitionLabel (label ),
207172 Number : partIndex ,
208173 Size : 0 ,
209174 UseAllAvailableSpace : true ,
@@ -219,18 +184,33 @@ func addExtraExt4Partition(mountpoint string, defaultPartitionSchema *baremetal.
219184 defaultPartitionSchema .Filesystems = append (defaultPartitionSchema .Filesystems , filesystem )
220185}
221186
222- func resizeRootPartition (originalDisks []* baremetal.SchemaDisk , withSwap bool , withExtraPartition bool ) {
187+ func updateSizeRoot (originalDisks []* baremetal.SchemaDisk , extraPart bool ) {
223188 for _ , disk := range originalDisks {
224189 for _ , partition := range disk .Partitions {
225190 if partition .Label == "root" {
226- if ! withSwap && ! withExtraPartition {
191+ if extraPart {
192+ partition .Size = partitionSize
193+ partition .UseAllAvailableSpace = false
194+ } else {
227195 partition .Size = 0
228196 partition .UseAllAvailableSpace = true
229197 }
198+ }
199+ }
200+ }
201+ }
230202
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 --
234214 }
235215 }
236216 }
0 commit comments