smutel / terraform-provider-netbox

Terraform provider for Netbox
ISC License
58 stars 19 forks source link

feat: Be able to use IPv6 as primary #148

Closed smutel closed 1 year ago

smutel commented 1 year ago

@amhn , still have the race condition and the change of primary_ip4 is still there every terraform run.

I think that on another PR you gave me the link to some code to avoid race condition, right ?

amhn commented 1 year ago

I was trying to figure that out. The solution I came up with is this:

diff --git a/netbox/resource_netbox_ipam_ip_addresses.go b/netbox/resource_netbox_ipam_ip_addresses.go
index bdf68208..d7e6d394 100644
--- a/netbox/resource_netbox_ipam_ip_addresses.go
+++ b/netbox/resource_netbox_ipam_ip_addresses.go
@@ -98,6 +98,10 @@ func resourceNetboxIpamIPAddresses() *schema.Resource {
                                Description:   "Set this resource as primary IPv4 (false by default).",
                                ConflictsWith: []string{"primary_ip"},
                                Deprecated:    "use primary_ip instead",
+                               DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
+                                       return d.GetRawConfig().GetAttr("primary_ip4").IsNull()
+                               },
+
                        },
                        "primary_ip": {
                                Type:          schema.TypeBool,
@@ -105,6 +109,9 @@ func resourceNetboxIpamIPAddresses() *schema.Resource {
                                Default:       false,
                                Description:   "Set this resource as primary IP (false by default).",
                                ConflictsWith: []string{"primary_ip4"},
+                               DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
+                                       return d.GetRawConfig().GetAttr("primary_ip").IsNull()
+                               },
                        },
                        "role": {
                                Type:     schema.TypeString,
@@ -330,6 +337,9 @@ func resourceNetboxIpamIPAddressesRead(ctx context.Context, d *schema.ResourceDa
                        if err = d.Set("primary_ip4", isPrimary); err != nil {
                                return diag.FromErr(err)
                        }
+                       if err = d.Set("primary_ip", isPrimary); err != nil {
+                               return diag.FromErr(err)
+                       }

                        if err = d.Set("object_id", resource.AssignedObjectID); err != nil {
                                return diag.FromErr(err)

This ignores the diff if the key is not present in the config. Maybe the DiffSuppressFunc for primary_ip needs to check for the existence of the "primary_ip4" key in the config. So we can be sure one of them is used. Otherwise deleting the key will not create a change.

If we could merge the testing branch first, this would be good. Then we can write some test cases and check the functionality.