Skip to content

Commit 38f1112

Browse files
committed
feat(baremetal): add check compatible
1 parent 5576851 commit 38f1112

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

internal/services/baremetal/easy_partitioning_data_source.go

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,31 @@ package baremetal
22

33
import (
44
"context"
5+
"fmt"
56
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
67
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
9+
"github.com/scaleway/scaleway-sdk-go/scw"
10+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
11+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
712
)
813

914
func DataEasyPartitioning() *schema.Resource {
1015
return &schema.Resource{
1116
ReadContext: dataEasyPartitioningRead,
1217
Schema: map[string]*schema.Schema{
18+
"offer_id": {
19+
Type: schema.TypeString,
20+
Required: true,
21+
Description: "ID of the server offer",
22+
},
23+
"os": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
Description: "The base image of the server",
27+
DiffSuppressFunc: dsf.Locality,
28+
ValidateDiagFunc: verify.IsUUIDorUUIDWithLocality(),
29+
},
1330
"swap": {
1431
Type: schema.TypeBool,
1532
Optional: true,
@@ -26,13 +43,67 @@ func DataEasyPartitioning() *schema.Resource {
2643
Type: schema.TypeString,
2744
Optional: true,
2845
Default: "/data",
29-
Description: "ext_4 partition's name",
46+
Description: "Mount point must be an absolute path with alphanumeric characters and underscores",
47+
},
48+
"custom_partition": {
49+
Type: schema.TypeString,
50+
Optional: true,
51+
Description: "The partitioning schema in json format",
3052
},
3153
},
3254
}
3355
}
3456

3557
func dataEasyPartitioningRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
58+
api, fallBackZone, err := newAPIWithZone(d, m)
59+
if err != nil {
60+
return diag.FromErr(err)
61+
}
62+
63+
osID := d.Get("os").(string)
64+
65+
os, err := api.GetOS(&baremetal.GetOSRequest{
66+
Zone: fallBackZone,
67+
OsID: osID,
68+
}, scw.WithContext(ctx))
69+
if err != nil {
70+
return diag.FromErr(err)
71+
}
72+
73+
if !os.CustomPartitioningSupported {
74+
return diag.FromErr(fmt.Errorf("custom partitioning is not supported with this OS"))
75+
}
76+
77+
offerID := d.Get("offer_id").(string)
78+
79+
offer, err := api.GetOffer(&baremetal.GetOfferRequest{
80+
Zone: fallBackZone,
81+
OfferID: offerID,
82+
}, scw.WithContext(ctx))
83+
if err != nil {
84+
return diag.FromErr(err)
85+
}
86+
87+
if !isOSCompatible(offer, os) {
88+
return diag.FromErr(fmt.Errorf("os and offer are not compatible"))
89+
}
90+
91+
defaultPartitioningSchema, err := api.GetDefaultPartitioningSchema(&baremetal.GetDefaultPartitioningSchemaRequest{
92+
Zone: fallBackZone,
93+
OfferID: offerID,
94+
OsID: osID,
95+
}, scw.WithContext(ctx))
96+
if err != nil {
97+
return diag.FromErr(err)
98+
}
99+
100+
//TODO checker si offer custom partitoning
101+
//TODO checker si offer et os compatible
102+
//TODO get default partitioning
103+
//TODO unmarshall
104+
//TODO replacer les valeurs
105+
//TODO marshal
106+
36107
var diags diag.Diagnostics
37108
return diags
38109
}

internal/services/baremetal/helpers.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,13 @@ func privateNetworkSetHash(v interface{}) int {
224224

225225
return schema.HashString(buf.String())
226226
}
227+
228+
func isOSCompatible(offer *baremetal.Offer, os *baremetal.OS) bool {
229+
for _, incompatible := range offer.IncompatibleOsIDs {
230+
if os.ID == incompatible {
231+
return true
232+
}
233+
}
234+
235+
return false
236+
}

0 commit comments

Comments
 (0)