Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -euo pipefail

bundle exec rake spec
10 changes: 9 additions & 1 deletion lib/netbox_client_ruby/entities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,15 @@ def find_by(attributes)
netbox_object.custom_fields[custom_field].to_s == filter_value.to_s
else
if netbox_object.respond_to?(filter_key)
netbox_object.public_send(filter_key).to_s == filter_value.to_s
# ruby's IPAddress class has a to_s and to_string method.
# the to_s will return just the ip address.
# the to_string will return the ip address with the subnet mask.
# we want to use the to_string method if it is available.
if netbox_object.public_send(filter_key).respond_to?(:to_string)
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if there is a better solution for this issue 🤔 . This would apply for all resources and it's not very clear if I just look at the code why this happens.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would've added a comment into the code, but I was trying to stick to your style.

I can do that if you want.

Copy link
Member

Choose a reason for hiding this comment

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

I think more about checking for IPAddress explicitly and use to_string there instead of to_s.

case netbox_object
when IPAddress
...

Copy link
Contributor Author

@fr3nch13 fr3nch13 Apr 22, 2025

Choose a reason for hiding this comment

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

Yeah, I was thinking that too. I just haven't updated the PR yet.

The only downside is if you decide to swap IpAddress for something like ipaddr, which also uses to_string.

netbox_object.public_send(filter_key).to_string == filter_value.to_s
else
netbox_object.public_send(filter_key).to_s == filter_value.to_s
end
else
false
end
Expand Down
6 changes: 6 additions & 0 deletions spec/netbox_client_ruby/api/ipam/ip_addresses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@
end
end
end

describe '#find_by' do
it 'should return a match with to_string' do
expect(subject.find_by({ address: '10.0.0.1/8' })['address']).to eq('10.0.0.1/8')
end
end
end