Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(instance): add a diffSuppressFunc on image server attr #412

Merged
merged 1 commit into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions scaleway/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,6 @@ func newRandomName(prefix string) string {

const gb uint64 = 1000 * 1000 * 1000

// suppressLocality is a SuppressDiffFunc to remove the locality from an ID when checking diff.
// e.g. 2c1a1716-5570-4668-a50a-860c90beabf6 == fr-par/2c1a1716-5570-4668-a50a-860c90beabf6
func suppressLocality(k, old, new string, d *schema.ResourceData) bool {
return expandID(old) == expandID(new)
}

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}`)

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

func diffSuppressFuncDuration(k, old, new string, d *schema.ResourceData) bool {
if old == new {
return true
}
d1, err1 := time.ParseDuration(old)
d2, err2 := time.ParseDuration(new)
if err1 != nil || err2 != nil {
return false
}
return d1 == d2
}

func validateDuration() schema.SchemaValidateFunc {
return func(i interface{}, s string) (strings []string, errors []error) {
str, isStr := i.(string)
Expand Down Expand Up @@ -632,6 +614,18 @@ func validateHour() schema.SchemaValidateFunc {
}
}

func diffSuppressFuncDuration(k, old, new string, d *schema.ResourceData) bool {
if old == new {
return true
}
d1, err1 := time.ParseDuration(old)
d2, err2 := time.ParseDuration(new)
if err1 != nil || err2 != nil {
return false
}
return d1 == d2
}

func diffSuppressFuncIgnoreCase(k, old, new string, d *schema.ResourceData) bool {
return strings.EqualFold(old, new)
}
Expand All @@ -650,3 +644,9 @@ func diffSuppressFuncLabelUUID(k, old, new string, d *schema.ResourceData) bool

return new == label || new == uuid
}

// diffSuppressFuncLocality is a SuppressDiffFunc to remove the locality from an ID when checking diff.
// e.g. 2c1a1716-5570-4668-a50a-860c90beabf6 == fr-par/2c1a1716-5570-4668-a50a-860c90beabf6
func diffSuppressFuncLocality(k, old, new string, d *schema.ResourceData) bool {
return expandID(old) == expandID(new)
}
17 changes: 9 additions & 8 deletions scaleway/resource_instance_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ func resourceScalewayInstanceServer() *schema.Resource {
Description: "The name of the server",
},
"image": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The UUID or the label of the base image used by the server",
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The UUID or the label of the base image used by the server",
DiffSuppressFunc: diffSuppressFuncLocality,
},
"type": {
Type: schema.TypeString,
Expand All @@ -56,13 +57,13 @@ func resourceScalewayInstanceServer() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
DiffSuppressFunc: suppressLocality,
DiffSuppressFunc: diffSuppressFuncLocality,
Description: "The security group the server is attached to",
},
"placement_group_id": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: suppressLocality,
DiffSuppressFunc: diffSuppressFuncLocality,
Description: "The placement group the server is attached to",
},
"placement_group_policy_respected": {
Expand Down Expand Up @@ -104,7 +105,7 @@ func resourceScalewayInstanceServer() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validationUUIDorUUIDWithLocality(),
DiffSuppressFunc: suppressLocality,
DiffSuppressFunc: diffSuppressFuncLocality,
},
Optional: true,
Description: "The additional volumes attached to the server",
Expand All @@ -129,7 +130,7 @@ func resourceScalewayInstanceServer() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Description: "The ID of the reserved IP for the server",
DiffSuppressFunc: suppressLocality,
DiffSuppressFunc: diffSuppressFuncLocality,
},
"ipv6_address": {
Type: schema.TypeString,
Expand Down
40 changes: 40 additions & 0 deletions scaleway/resource_instance_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,46 @@ func TestAccScalewayInstanceServerWithReservedIP(t *testing.T) {
})
}

func TestAccScalewayInstanceServerImageDataSource(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckScalewayInstanceServerDestroy,
Steps: []resource.TestStep{
{
Config: `
data "scaleway_instance_image" "ubuntu_bionic" {
name = "ubuntu_bionic"
}

resource "scaleway_instance_server" "base" {
type = "DEV1-S"
image = data.scaleway_instance_image.ubuntu_bionic.id
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayInstanceServerExists("scaleway_instance_server.base"),
resource.TestCheckResourceAttr("scaleway_instance_server.base", "image", "6651f88f-45af-4b93-95e8-3931b74f6acc"),
),
},
// Ensure that image diffSuppressFunc results in no plan.
{
Config: `
data "scaleway_instance_image" "ubuntu_bionic" {
name = "ubuntu_bionic"
}

resource "scaleway_instance_server" "base" {
type = "DEV1-S"
image = data.scaleway_instance_image.ubuntu_bionic.id
}
`,
PlanOnly: true,
},
},
})
}

func testAccCheckScalewayInstanceServerExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down
2 changes: 1 addition & 1 deletion scaleway/resource_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func resourceScalewayIP() *schema.Resource {
Optional: true,
Computed: true,
Description: "The server associated with the ip",
DiffSuppressFunc: suppressLocality,
DiffSuppressFunc: diffSuppressFuncLocality,
},
"ip": {
Type: schema.TypeString,
Expand Down