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: Fix empty value check to handle default numeric values correctly #286

Merged

Conversation

dokmic
Copy link
Contributor

@dokmic dokmic commented Nov 1, 2023

While working on #283, I discovered that optional numeric fields with 0 as a default value are skipped even though they appear in the planned changes. That also takes place when we use 0 in the configuration, and the related field either has no default value or has 0 in there:

"tunnel_id": {
	Type:        schema.TypeInt,
	Optional:    true,
	Default:     0,
	Description: "Unique tunnel identifier, which must match the other side of the tunnel.",
},
resource "routeros_interface_eoip" "eoip2" {
  name           = "eoip2"
}
Terraform will perform the following actions:

  # routeros_interface_eoip.eoip2 will be created
  + resource "routeros_interface_eoip" "eoip2" {
      + actual_mtu                 = (known after apply)
      + allow_fast_path            = true
      + arp                        = "enabled"
      + arp_timeout                = "auto"
      + clamp_tcp_mss              = true
      + disabled                   = false
      + dont_fragment              = "no"
      + dscp                       = "inherit"
      + id                         = (known after apply)
      + keepalive                  = "10s,10"
      + l2mtu                      = (known after apply)
      + local_address              = "0.0.0.0"
      + loop_protect               = "default"
      + loop_protect_disable_time  = "5m"
      + loop_protect_send_interval = "5s"
      + loop_protect_status        = (known after apply)
      + mac_address                = (known after apply)
      + mtu                        = (known after apply)
      + name                       = "eoip2"
      + remote_address             = "0.0.0.0"
      + running                    = (known after apply)
      + tunnel_id                  = 0
    }

Plan: 1 to add, 0 to change, 0 to destroy.

...

Error: from RouterOS device: missing =tunnel-id=
│ 
│   with routeros_interface_eoip.eoip2,
│   on main.tf line 236, in resource "routeros_interface_eoip" "eoip2":

The problem in the provider's code is that HasChange returns false when the default is 0:

return !d.HasChange(propName)

After drilling down into the origin of this problem, I found out that terraform SDK casts null to 0, and therefore, HasChange compares two zeros.

hashicorp/terraform-plugin-sdk#817 (comment) explained the nature of such behavior and convinced me that it doesn't make much sense to try to fix that on the SDK side. So, I slightly changed the empty check logic not to skip such values.

Unfortunately, I could not cover that with a unit test in mikrotik_serialize_test.go because it uses the GetRawConfig method, which always returns a null value when using either TestResourceData or TestResourceDataRaw. And I couldn't find a clean way to set the raw config in the resource data state.

@dokmic dokmic requested a review from a team as a code owner November 1, 2023 21:47
Copy link
Collaborator

@vaerh vaerh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the code, it works as expected and we won't try to cover it with tests.

Yes, it's such small things that you can't see at once, but then they cause a lot of inconvenience at the most unexpected moment. Thank you for your hard work and patience!

P.S. You can exclude the documentation from the commit, as it is automatically updated in each release.

@vaerh vaerh merged commit 661e49c into terraform-routeros:main Nov 2, 2023
3 checks passed
@vaerh
Copy link
Collaborator

vaerh commented Nov 2, 2023

🎉 This PR is included in version 1.20.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@vaerh vaerh added the released label Nov 2, 2023
@dokmic dokmic deleted the bugfix/default-numeric-values branch November 2, 2023 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants