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(server): Return nil instead of "<nil>" with IPv4/IPv6 disabled #723

Merged
merged 1 commit into from
Aug 14, 2023

Conversation

akosiaris
Copy link
Contributor

Why:

"<nil>" isn't very easy to work with in terraform. e.g. there are functions to work with null or the empty string (""), like compact() or coalesce(), but nothing exists to handle "<nil>" natively, making it an odd one and requiring specific custom filtering constructs to work with it.

"<nil>" is a pretty common thing to return in golang, with tens of usages even in the main repo[1] but HCL is not golang

hcloud-go has a very nice IsUnspecified()[1] function to detect if the API returned an unspecified IP address and a quick check says that the hcloud cli command uses it to provide custom text output.

What:

Switch from using unconditionally the output of net.IP.String() function, which returns "<nil>"[2] if the IP has length 0, to a conditional usage of the function, setting the address to nil otherwise.

Setting the entire struct to nil apparently locally appeases unit tests just fine and even works in some, very simple, scenarios just fine, returning the empty string ("") instead of "<nil>"

This is an API breaking change. It might not be desirable to merge and/or require some careful handling depending on how widespread the usage of handling "<nil>" is. A quick search on my side, did not find any usages[3], but I might have very well failed.

[1] https://github.com/search?q=repo%3Agolang%2Fgo%20%22%3Cnil%3E%22&type=code
[2] https://pkg.go.dev/net#IP.String
[3] https://github.com/search?q=%22%3Cnil%3E%22+language%3AHCL&type=code

Why:

`"<nil>"` isn't very easy to work with in terraform. e.g. there are
functions to work with null or the empty string (""), like `compact()` or
`coalesce()`, but nothing exists to handle `"<nil>"` natively, making it an
odd one and requiring specific custom filtering constructs to work with it.

`"<nil>"` is a pretty common thing to return in golang, with tens of
usages even in the main repo[1] but HCL is not golang

hcloud-go has a very nice IsUnspecified()[1] function to detect if the
API returned an unspecified IP address and a quick check says that the
hcloud cli command uses it to provide custom text output.

What:

Switch from using unconditionally the output of net.IP.String()
function, which returns `"<nil>"`[2] if the IP has length 0, to a
conditional usage of the function, setting the address to nil otherwise.

Setting the entire struct to nil apparently locally appeases unit tests
just fine and even works in some, very simple, scenarios just fine,
returning the empty string ("") instead of `"<nil>"`

This is an API breaking change. It might not be desirable to merge
and/or require some careful handling depending on how widespread the
usage of handling `"<nil>"` is. A quick search on my side, did not find
any usages[3], but I might have very well failed.

[1] https://github.com/search?q=repo%3Agolang%2Fgo%20%22%3Cnil%3E%22&type=code
[2] https://pkg.go.dev/net#IP.String
[3] https://github.com/search?q=%22%3Cnil%3E%22+language%3AHCL&type=code

Signed-off-by: Alexandros Kosiaris <akosiaris@gmail.com>
@apricote
Copy link
Member

Thank you very much for this improvement @akosiaris! I started the CI checks, do not worry about the integrations tests failing, our API access is currently not setup well for external contributors. I ran the full test suite locally and everything worked as expected.

This is an API breaking change. It might not be desirable to merge and/or require some careful handling depending on how widespread the usage of handling "" is. A quick search on my side, did not find any usages[3], but I might have very well failed.

I would consider this buggy behaviour of the existing implementation. IMO we can merge and publish as is, if anyone stumbles upon this, they should easily find this issue by searching for and learn about it.

@apricote apricote merged commit 6cd2a37 into hetznercloud:main Aug 14, 2023
2 of 3 checks passed
@apricote apricote changed the title Return nil instead of "<nil>" with IPv4/IPv6 disabled fix(server): Return nil instead of "<nil>" with IPv4/IPv6 disabled Aug 14, 2023
@akosiaris
Copy link
Contributor Author

@apricote. Happy to be of help! Thanks for reviewing and merging!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants