Skip to content

Commit 3dbf76f

Browse files
fix(instance): add a diffSuppressFunc on image server attr (scaleway#412)
1 parent 1e4cad0 commit 3dbf76f

File tree

4 files changed

+68
-27
lines changed

4 files changed

+68
-27
lines changed

scaleway/helpers.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -415,12 +415,6 @@ func newRandomName(prefix string) string {
415415

416416
const gb uint64 = 1000 * 1000 * 1000
417417

418-
// suppressLocality is a SuppressDiffFunc to remove the locality from an ID when checking diff.
419-
// e.g. 2c1a1716-5570-4668-a50a-860c90beabf6 == fr-par/2c1a1716-5570-4668-a50a-860c90beabf6
420-
func suppressLocality(k, old, new string, d *schema.ResourceData) bool {
421-
return expandID(old) == expandID(new)
422-
}
423-
424418
var UUIDRegex = regexp.MustCompile(`[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}`)
425419

426420
// isUUID returns true if the given string have an UUID format.
@@ -593,18 +587,6 @@ func flattenIpNet(ipNet scw.IPNet) string {
593587
return string(raw[1 : len(raw)-1])
594588
}
595589

596-
func diffSuppressFuncDuration(k, old, new string, d *schema.ResourceData) bool {
597-
if old == new {
598-
return true
599-
}
600-
d1, err1 := time.ParseDuration(old)
601-
d2, err2 := time.ParseDuration(new)
602-
if err1 != nil || err2 != nil {
603-
return false
604-
}
605-
return d1 == d2
606-
}
607-
608590
func validateDuration() schema.SchemaValidateFunc {
609591
return func(i interface{}, s string) (strings []string, errors []error) {
610592
str, isStr := i.(string)
@@ -632,6 +614,18 @@ func validateHour() schema.SchemaValidateFunc {
632614
}
633615
}
634616

617+
func diffSuppressFuncDuration(k, old, new string, d *schema.ResourceData) bool {
618+
if old == new {
619+
return true
620+
}
621+
d1, err1 := time.ParseDuration(old)
622+
d2, err2 := time.ParseDuration(new)
623+
if err1 != nil || err2 != nil {
624+
return false
625+
}
626+
return d1 == d2
627+
}
628+
635629
func diffSuppressFuncIgnoreCase(k, old, new string, d *schema.ResourceData) bool {
636630
return strings.EqualFold(old, new)
637631
}
@@ -650,3 +644,9 @@ func diffSuppressFuncLabelUUID(k, old, new string, d *schema.ResourceData) bool
650644

651645
return new == label || new == uuid
652646
}
647+
648+
// diffSuppressFuncLocality is a SuppressDiffFunc to remove the locality from an ID when checking diff.
649+
// e.g. 2c1a1716-5570-4668-a50a-860c90beabf6 == fr-par/2c1a1716-5570-4668-a50a-860c90beabf6
650+
func diffSuppressFuncLocality(k, old, new string, d *schema.ResourceData) bool {
651+
return expandID(old) == expandID(new)
652+
}

scaleway/resource_instance_server.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ func resourceScalewayInstanceServer() *schema.Resource {
3232
Description: "The name of the server",
3333
},
3434
"image": {
35-
Type: schema.TypeString,
36-
Required: true,
37-
ForceNew: true,
38-
Description: "The UUID or the label of the base image used by the server",
35+
Type: schema.TypeString,
36+
Required: true,
37+
ForceNew: true,
38+
Description: "The UUID or the label of the base image used by the server",
39+
DiffSuppressFunc: diffSuppressFuncLocality,
3940
},
4041
"type": {
4142
Type: schema.TypeString,
@@ -56,13 +57,13 @@ func resourceScalewayInstanceServer() *schema.Resource {
5657
Type: schema.TypeString,
5758
Optional: true,
5859
Computed: true,
59-
DiffSuppressFunc: suppressLocality,
60+
DiffSuppressFunc: diffSuppressFuncLocality,
6061
Description: "The security group the server is attached to",
6162
},
6263
"placement_group_id": {
6364
Type: schema.TypeString,
6465
Optional: true,
65-
DiffSuppressFunc: suppressLocality,
66+
DiffSuppressFunc: diffSuppressFuncLocality,
6667
Description: "The placement group the server is attached to",
6768
},
6869
"placement_group_policy_respected": {
@@ -104,7 +105,7 @@ func resourceScalewayInstanceServer() *schema.Resource {
104105
Elem: &schema.Schema{
105106
Type: schema.TypeString,
106107
ValidateFunc: validationUUIDorUUIDWithLocality(),
107-
DiffSuppressFunc: suppressLocality,
108+
DiffSuppressFunc: diffSuppressFuncLocality,
108109
},
109110
Optional: true,
110111
Description: "The additional volumes attached to the server",
@@ -129,7 +130,7 @@ func resourceScalewayInstanceServer() *schema.Resource {
129130
Type: schema.TypeString,
130131
Optional: true,
131132
Description: "The ID of the reserved IP for the server",
132-
DiffSuppressFunc: suppressLocality,
133+
DiffSuppressFunc: diffSuppressFuncLocality,
133134
},
134135
"ipv6_address": {
135136
Type: schema.TypeString,

scaleway/resource_instance_server_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,46 @@ func TestAccScalewayInstanceServerWithReservedIP(t *testing.T) {
563563
})
564564
}
565565

566+
func TestAccScalewayInstanceServerImageDataSource(t *testing.T) {
567+
resource.ParallelTest(t, resource.TestCase{
568+
PreCheck: func() { testAccPreCheck(t) },
569+
Providers: testAccProviders,
570+
CheckDestroy: testAccCheckScalewayInstanceServerDestroy,
571+
Steps: []resource.TestStep{
572+
{
573+
Config: `
574+
data "scaleway_instance_image" "ubuntu_bionic" {
575+
name = "ubuntu_bionic"
576+
}
577+
578+
resource "scaleway_instance_server" "base" {
579+
type = "DEV1-S"
580+
image = data.scaleway_instance_image.ubuntu_bionic.id
581+
}
582+
`,
583+
Check: resource.ComposeTestCheckFunc(
584+
testAccCheckScalewayInstanceServerExists("scaleway_instance_server.base"),
585+
resource.TestCheckResourceAttr("scaleway_instance_server.base", "image", "6651f88f-45af-4b93-95e8-3931b74f6acc"),
586+
),
587+
},
588+
// Ensure that image diffSuppressFunc results in no plan.
589+
{
590+
Config: `
591+
data "scaleway_instance_image" "ubuntu_bionic" {
592+
name = "ubuntu_bionic"
593+
}
594+
595+
resource "scaleway_instance_server" "base" {
596+
type = "DEV1-S"
597+
image = data.scaleway_instance_image.ubuntu_bionic.id
598+
}
599+
`,
600+
PlanOnly: true,
601+
},
602+
},
603+
})
604+
}
605+
566606
func testAccCheckScalewayInstanceServerExists(n string) resource.TestCheckFunc {
567607
return func(s *terraform.State) error {
568608
rs, ok := s.RootModule().Resources[n]

scaleway/resource_ip.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func resourceScalewayIP() *schema.Resource {
2626
Optional: true,
2727
Computed: true,
2828
Description: "The server associated with the ip",
29-
DiffSuppressFunc: suppressLocality,
29+
DiffSuppressFunc: diffSuppressFuncLocality,
3030
},
3131
"ip": {
3232
Type: schema.TypeString,

0 commit comments

Comments
 (0)