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,20 @@ 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" )
117+ if ok {
118+ addExtraExt4Partition (mountpoint , defaultPartitioningSchema )
119+ updateRaid (defaultPartitioningSchema )
120+ }
128121
129- if ! extraPart && ! swap {
130- defaultPartitioningSchema .Filesystems = defaultPartitioningSchema .Filesystems [:len (defaultPartitioningSchema .Filesystems )- 1 ]
131- defaultPartitioningSchema .Raids = defaultPartitioningSchema .Raids [:len (defaultPartitioningSchema .Raids )- 1 ]
122+ if ! swap || ok {
123+ updateSizeRoot (defaultPartitioningSchema .Disks , ok )
132124 }
133125
134126 err = api .ValidatePartitioningSchema (& baremetal.ValidatePartitioningSchemaRequest {
@@ -155,55 +147,25 @@ func dataEasyPartitioningRead(ctx context.Context, d *schema.ResourceData, m int
155147 return nil
156148}
157149
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- })
150+ func updateRaid (partitionSchema * baremetal.Schema ) {
151+ lenDisk0Partition := len (partitionSchema .Disks [0 ].Partitions )
152+ lenDisk1Partition := len (partitionSchema .Disks [1 ].Partitions )
153+ raid := & baremetal.SchemaRAID {
154+ Name : "/dev/md2" ,
155+ Level : "raid_level_1" ,
156+ Devices : []string {fmt .Sprintf ("%sp%d" , partitionSchema .Disks [0 ].Device , lenDisk0Partition ),
157+ fmt .Sprintf ("%sp%d" , partitionSchema .Disks [1 ].Device , lenDisk1Partition ),
158+ },
193159 }
194-
195- return result
160+ partitionSchema .Raids = append (partitionSchema .Raids , raid )
196161}
197162
198- func addExtraExt4Partition (mountpoint string , defaultPartitionSchema * baremetal.Schema , extraPart bool ) {
199- if ! extraPart {
200- return
201- }
202-
163+ func addExtraExt4Partition (mountpoint string , defaultPartitionSchema * baremetal.Schema ) {
164+ label := strings .TrimPrefix (mountpoint , "/" )
203165 for _ , disk := range defaultPartitionSchema .Disks {
204166 partIndex := uint32 (len (disk .Partitions )) + 1
205167 data := & baremetal.SchemaPartition {
206- Label : baremetal .SchemaPartitionLabel ("data" ),
168+ Label : baremetal .SchemaPartitionLabel (label ),
207169 Number : partIndex ,
208170 Size : 0 ,
209171 UseAllAvailableSpace : true ,
@@ -219,19 +181,34 @@ func addExtraExt4Partition(mountpoint string, defaultPartitionSchema *baremetal.
219181 defaultPartitionSchema .Filesystems = append (defaultPartitionSchema .Filesystems , filesystem )
220182}
221183
222- func resizeRootPartition (originalDisks []* baremetal.SchemaDisk , withSwap bool , withExtraPartition bool ) {
184+ func updateSizeRoot (originalDisks []* baremetal.SchemaDisk , extraPart bool ) {
223185 for _ , disk := range originalDisks {
224186 for _ , partition := range disk .Partitions {
225187 if partition .Label == "root" {
226- if ! withSwap && ! withExtraPartition {
188+ if extraPart {
189+ partition .Size = partitionSize
190+ partition .UseAllAvailableSpace = false
191+ } else {
227192 partition .Size = 0
228193 partition .UseAllAvailableSpace = true
229194 }
195+ }
196+ }
197+ }
230198
231- if withExtraPartition {
232- partition .Size = partitionSize
233- }
199+ }
200+
201+ func removeSwap (originalDisks []* baremetal.SchemaDisk ) {
202+ for _ , disk := range originalDisks {
203+ for i , partition := range disk .Partitions {
204+ if partition .Label == "swap" {
205+ disk .Partitions = append (disk .Partitions [:i ], disk .Partitions [i + 1 :]... )
206+ break
234207 }
208+ if partition .Label != "uefi" {
209+ partition .Number --
210+ }
211+
235212 }
236213 }
237214}
0 commit comments