Skip to content

Commit c598194

Browse files
committed
manage baremetal custom partitioning schema
1 parent 0e5d5f9 commit c598194

File tree

2 files changed

+161
-123
lines changed

2 files changed

+161
-123
lines changed

internal/services/baremetal/server.go

Lines changed: 84 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -319,67 +319,48 @@ func ResourceServerCreate(ctx context.Context, d *schema.ResourceData, m interfa
319319
offerID = zonal.NewID(zone, o.ID)
320320
}
321321

322-
if !d.Get("install_config_afterward").(bool) {
323-
if diags := validateInstallConfig(ctx, d, m); len(diags) > 0 {
324-
return diags
325-
}
326-
}
327-
328-
partitioningSchema := baremetal.Schema{}
329-
if file, ok := d.GetOk("partitioning"); ok {
330-
todecode, _ := json.Marshal(file)
331-
err = json.Unmarshal(todecode, &partitioningSchema)
332-
}
333-
334-
serverRequestInstall := baremetal.CreateServerRequestInstall{
335-
OsID: zonal.ExpandID(d.Get("os")).ID,
336-
Hostname: d.Get("hostname").(string),
337-
SSHKeyIDs: types.ExpandStrings(d.Get("ssh_key_ids")),
338-
User: types.ExpandStringPtr(d.Get("user")),
339-
Password: types.ExpandStringPtr(d.Get("password")),
340-
PartitioningSchema: &partitioningSchema,
341-
}
342-
343-
server, err := api.CreateServer(&baremetal.CreateServerRequest{
322+
req := &baremetal.CreateServerRequest{
344323
Zone: zone,
345324
Name: types.ExpandOrGenerateString(d.Get("name"), "bm"),
346325
ProjectID: types.ExpandStringPtr(d.Get("project_id")),
347326
Description: d.Get("description").(string),
348327
OfferID: offerID.ID,
349328
Tags: types.ExpandStrings(d.Get("tags")),
350-
Install: &serverRequestInstall,
351-
}, scw.WithContext(ctx))
352-
if err != nil {
353-
return diag.FromErr(err)
354329
}
355330

356-
d.SetId(zonal.NewID(server.Zone, server.ID).String())
331+
partitioningSchema := baremetal.Schema{}
332+
if file, ok := d.GetOk("partitioning"); ok || !d.Get("install_config_afterward").(bool) {
333+
if diags := validateInstallConfig(ctx, d, m); len(diags) > 0 {
334+
return diags
335+
}
336+
if file != "" {
337+
todecode, _ := file.(string)
338+
err = json.Unmarshal([]byte(todecode), &partitioningSchema)
339+
}
340+
req.Install = &baremetal.CreateServerRequestInstall{
341+
OsID: zonal.ExpandID(d.Get("os")).ID,
342+
Hostname: d.Get("hostname").(string),
343+
SSHKeyIDs: types.ExpandStrings(d.Get("ssh_key_ids")),
344+
User: types.ExpandStringPtr(d.Get("user")),
345+
Password: types.ExpandStringPtr(d.Get("password")),
346+
PartitioningSchema: &partitioningSchema,
347+
}
348+
}
357349

358-
_, err = waitForServer(ctx, api, zone, server.ID, d.Timeout(schema.TimeoutCreate))
350+
server, err := api.CreateServer(req, scw.WithContext(ctx))
359351
if err != nil {
360352
return diag.FromErr(err)
361353
}
362354

363-
if !d.Get("install_config_afterward").(bool) {
364-
_, err = api.InstallServer(&baremetal.InstallServerRequest{
365-
Zone: server.Zone,
366-
ServerID: server.ID,
367-
OsID: zonal.ExpandID(d.Get("os")).ID,
368-
Hostname: types.ExpandStringWithDefault(d.Get("hostname"), server.Name),
369-
SSHKeyIDs: types.ExpandStrings(d.Get("ssh_key_ids")),
370-
User: types.ExpandStringPtr(d.Get("user")),
371-
Password: types.ExpandStringPtr(d.Get("password")),
372-
ServiceUser: types.ExpandStringPtr(d.Get("service_user")),
373-
ServicePassword: types.ExpandStringPtr(d.Get("service_password")),
374-
}, scw.WithContext(ctx))
375-
if err != nil {
376-
return diag.FromErr(err)
377-
}
355+
d.SetId(zonal.NewID(server.Zone, server.ID).String())
378356

357+
if d.Get("install_config_afterward").(bool) {
358+
_, err = waitForServer(ctx, api, zone, server.ID, d.Timeout(schema.TimeoutCreate))
359+
} else {
379360
_, err = waitForServerInstall(ctx, api, zone, server.ID, d.Timeout(schema.TimeoutCreate))
380-
if err != nil {
381-
return diag.FromErr(err)
382-
}
361+
}
362+
if err != nil {
363+
return diag.FromErr(err)
383364
}
384365

385366
options, optionsExist := d.GetOk("options")
@@ -506,6 +487,63 @@ func ResourceServerRead(ctx context.Context, d *schema.ResourceData, m interface
506487
return nil
507488
}
508489

490+
//func schemaToStringList(schema *baremetal.Schema) string {
491+
// var result string
492+
//
493+
// if schema.Disks != nil {
494+
// for _, disk := range schema.Disks {
495+
// if disk != nil {
496+
// result += "Disk: " + disk.Device
497+
// if disk.Partitions != nil {
498+
// for _, partition := range disk.Partitions {
499+
// if partition != nil {
500+
// result += fmt.Sprintf(" Partition: %s Number: %d Size: %s", partition.Label, partition.Number, partition.Size.String())
501+
// }
502+
// }
503+
// }
504+
// }
505+
// }
506+
// }
507+
//
508+
// if schema.Raids != nil {
509+
// for _, raid := range schema.Raids {
510+
// if raid != nil {
511+
// result += fmt.Sprintf("RAID: %s Level: %s", raid.Name, raid.Level)
512+
// for _, device := range raid.Devices {
513+
// result += " Device: " + device
514+
// }
515+
// }
516+
// }
517+
// }
518+
//
519+
// if schema.Filesystems != nil {
520+
// for _, fs := range schema.Filesystems {
521+
// if fs != nil {
522+
// result += fmt.Sprintf("Filesystem: %s Format: %s Mountpoint: %s", fs.Device, fs.Format, fs.Mountpoint)
523+
// }
524+
// }
525+
// }
526+
//
527+
// if schema.Zfs != nil {
528+
// for _, pool := range schema.Zfs.Pools {
529+
// if pool != nil {
530+
// result += fmt.Sprintf("ZFS Pool: %s Type: %s", pool.Name, pool.Type)
531+
// for _, device := range pool.Devices {
532+
// result += " Device: " + device
533+
// }
534+
// for _, option := range pool.Options {
535+
// result += " Option: " + option
536+
// }
537+
// for _, fsOption := range pool.FilesystemOptions {
538+
// result += " Filesystem Option: " + fsOption
539+
// }
540+
// }
541+
// }
542+
// }
543+
//
544+
// return result
545+
//}
546+
509547
//gocyclo:ignore
510548
func ResourceServerUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
511549
api, zonedID, err := NewAPIWithZoneAndID(m, d.Id())

internal/services/baremetal/server_test.go

Lines changed: 77 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package baremetal_test
33
import (
44
"encoding/json"
55
"fmt"
6+
"reflect"
67
"regexp"
78
"strings"
89
"testing"
@@ -89,7 +90,7 @@ func TestAccServer_Basic(t *testing.T) {
8990
name = "%s"
9091
zone = "fr-par-1"
9192
description = "test a description"
92-
offer = %s
93+
offer = "%s"
9394
os = data.scaleway_baremetal_os.my_os.os_id
9495
9596
tags = [ "terraform-test", "scaleway_baremetal_server", "minimal", "edited" ]
@@ -225,8 +226,7 @@ func TestAccServer_CreateServerWithCustomInstallConfig(t *testing.T) {
225226
resource.TestCheckResourceAttr("scaleway_baremetal_server.base", "tags.0", "terraform-test"),
226227
resource.TestCheckResourceAttr("scaleway_baremetal_server.base", "tags.1", "scaleway_baremetal_server"),
227228
resource.TestCheckResourceAttr("scaleway_baremetal_server.base", "tags.2", "minimal"),
228-
testAccChechPartitioning(tt, "scaleway_baremetal_server.base", "partitioning_schema.disks.0.partition.0.size", "536870912", jsonConfigPartitioning),
229-
//resource.TestCheckResourceAttr("scaleway_baremetal_server.base", "install.partitioning_schema.disks.0.partition.0.size", "536870912"),
229+
testAccChechPartitioning(tt, "scaleway_baremetal_server.base", jsonConfigPartitioning),
230230
acctest.CheckResourceAttrUUID("scaleway_baremetal_server.base", "ssh_key_ids.0"),
231231
),
232232
},
@@ -948,82 +948,82 @@ func TestAccServer_WithIPAMPrivateNetwork(t *testing.T) {
948948
},
949949
{
950950
Config: fmt.Sprintf(`
951-
resource "scaleway_vpc" "vpc01" {
952-
name = "TestAccScalewayBaremetalIPAM"
953-
}
954-
955-
resource "scaleway_vpc_private_network" "pn01" {
956-
name = "TestAccScalewayBaremetalIPAM"
957-
ipv4_subnet {
958-
subnet = "172.16.64.0/22"
959-
}
960-
vpc_id = scaleway_vpc.vpc01.id
961-
}
962-
963-
resource "scaleway_ipam_ip" "ip01" {
964-
address = "172.16.64.7"
965-
source {
966-
private_network_id = scaleway_vpc_private_network.pn01.id
967-
}
968-
}
969-
970-
resource "scaleway_ipam_ip" "ip02" {
971-
address = "172.16.64.9"
972-
source {
973-
private_network_id = scaleway_vpc_private_network.pn01.id
974-
}
975-
}
976-
977-
data "scaleway_baremetal_os" "my_os" {
978-
zone = "fr-par-1"
979-
name = "Ubuntu"
980-
version = "22.04 LTS (Jammy Jellyfish)"
981-
}
982-
983-
data "scaleway_baremetal_offer" "my_offer" {
984-
zone = "fr-par-1"
985-
name = "%s"
986-
}
987-
988-
data "scaleway_baremetal_option" "private_network" {
989-
zone = "fr-par-1"
990-
name = "Private Network"
991-
}
992-
993-
resource "scaleway_iam_ssh_key" "base" {
994-
name = "%s"
995-
public_key = "%s"
996-
}
997-
998-
resource "scaleway_baremetal_server" "base" {
999-
name = "%s"
1000-
zone = "fr-par-1"
1001-
offer = data.scaleway_baremetal_offer.my_offer.offer_id
1002-
os = data.scaleway_baremetal_os.my_os.os_id
1003-
1004-
ssh_key_ids = [ scaleway_iam_ssh_key.base.id ]
1005-
options {
1006-
id = data.scaleway_baremetal_option.private_network.option_id
951+
resource "scaleway_vpc" "vpc01" {
952+
name = "TestAccScalewayBaremetalIPAM"
1007953
}
1008-
private_network {
1009-
id = scaleway_vpc_private_network.pn01.id
1010-
ipam_ip_ids = [scaleway_ipam_ip.ip01.id, scaleway_ipam_ip.ip02.id]
954+
955+
resource "scaleway_vpc_private_network" "pn01" {
956+
name = "TestAccScalewayBaremetalIPAM"
957+
ipv4_subnet {
958+
subnet = "172.16.64.0/22"
959+
}
960+
vpc_id = scaleway_vpc.vpc01.id
1011961
}
1012-
}
1013-
1014-
data "scaleway_ipam_ips" "base" {
1015-
resource {
1016-
name = scaleway_baremetal_server.base.name
1017-
type = "baremetal_private_nic"
1018-
}
1019-
type = "ipv4"
1020-
}
1021-
`, OfferName, SSHKeyName, SSHKeyBaremetal, name),
962+
963+
resource "scaleway_ipam_ip" "ip01" {
964+
address = "172.16.64.7"
965+
source {
966+
private_network_id = scaleway_vpc_private_network.pn01.id
967+
}
968+
}
969+
970+
resource "scaleway_ipam_ip" "ip02" {
971+
address = "172.16.64.9"
972+
source {
973+
private_network_id = scaleway_vpc_private_network.pn01.id
974+
}
975+
}
976+
977+
data "scaleway_baremetal_os" "my_os" {
978+
zone = "fr-par-1"
979+
name = "Ubuntu"
980+
version = "22.04 LTS (Jammy Jellyfish)"
981+
}
982+
983+
data "scaleway_baremetal_offer" "my_offer" {
984+
zone = "fr-par-1"
985+
name = "%s"
986+
}
987+
988+
data "scaleway_baremetal_option" "private_network" {
989+
zone = "fr-par-1"
990+
name = "Private Network"
991+
}
992+
993+
resource "scaleway_iam_ssh_key" "base" {
994+
name = "%s"
995+
public_key = "%s"
996+
}
997+
998+
resource "scaleway_baremetal_server" "base" {
999+
name = "%s"
1000+
zone = "fr-par-1"
1001+
offer = data.scaleway_baremetal_offer.my_offer.offer_id
1002+
os = data.scaleway_baremetal_os.my_os.os_id
1003+
1004+
ssh_key_ids = [ scaleway_iam_ssh_key.base.id ]
1005+
options {
1006+
id = data.scaleway_baremetal_option.private_network.option_id
1007+
}
1008+
private_network {
1009+
id = scaleway_vpc_private_network.pn01.id
1010+
ipam_ip_ids = [scaleway_ipam_ip.ip01.id, scaleway_ipam_ip.ip02.id]
1011+
}
1012+
}
1013+
1014+
data "scaleway_ipam_ips" "base" {
1015+
resource {
1016+
name = scaleway_baremetal_server.base.name
1017+
type = "baremetal_private_nic"
1018+
}
1019+
type = "ipv4"
1020+
}
1021+
`, OfferName, SSHKeyName, SSHKeyBaremetal, name),
10221022
Check: resource.ComposeTestCheckFunc(
10231023
testAccCheckBaremetalServerExists(tt, "scaleway_baremetal_server.base"),
10241024
testAccCheckBaremetalServerHasPrivateNetwork(tt, "scaleway_baremetal_server.base"),
1025-
resource.TestCheckResourceAttrPair("scaleway_ipam_ip.ip01", "address", "data.scaleway_ipam_ips.base", "ips.1.address"),
1026-
resource.TestCheckResourceAttrPair("scaleway_ipam_ip.ip02", "address", "data.scaleway_ipam_ips.base", "ips.0.address"),
1025+
resource.TestCheckResourceAttrPair("scaleway_ipam_ip.ip01", "address", "data.scaleway_ipam_ips.base", "ips.0.address"),
1026+
resource.TestCheckResourceAttrPair("scaleway_ipam_ip.ip02", "address", "data.scaleway_ipam_ips.base", "ips.1.address"),
10271027
),
10281028
},
10291029
},
@@ -1054,7 +1054,7 @@ func testAccCheckBaremetalServerExists(tt *acctest.TestTools, n string) resource
10541054
}
10551055
}
10561056

1057-
func testAccChechPartitioning(tt *acctest.TestTools, n string, key string, value string, source string) resource.TestCheckFunc {
1057+
func testAccChechPartitioning(tt *acctest.TestTools, n string, source string) resource.TestCheckFunc {
10581058
return func(s *terraform.State) error {
10591059
rs, ok := s.RootModule().Resources[n]
10601060
if !ok {
@@ -1075,13 +1075,13 @@ func testAccChechPartitioning(tt *acctest.TestTools, n string, key string, value
10751075
if server.Install.PartitioningSchema == nil {
10761076
return fmt.Errorf("server %s has no partitioning schema", n)
10771077
}
1078-
var schema baremetalSDK.Schema
1078+
schema := baremetalSDK.Schema{}
10791079
//partitioning, _ := json.Marshal(source)
10801080
err = json.Unmarshal([]byte(source), &schema)
10811081
if err != nil {
10821082
return err
10831083
}
1084-
if &schema != server.Install.PartitioningSchema {
1084+
if !reflect.DeepEqual(&schema, server.Install.PartitioningSchema) {
10851085
return fmt.Errorf("server %s has not custom partitioning install", n)
10861086
}
10871087
return nil

0 commit comments

Comments
 (0)