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 cables status choice being provided by OPTIONS #457

Merged
merged 5 commits into from Jun 9, 2021

Conversation

FragmentedPacket
Copy link
Contributor

Fixes: #452 452

Remove the metadata_class as StatusViewSetMixin uses it to properly set the metadata returned.

@FragmentedPacket
Copy link
Contributor Author

I would like to add some tests to make sure any model that uses the Status model will be tested that it's rendered metadata works and prevent any regression. Not sure if this should happen in two separate places (model or viewset tests and API tests) as the Ansible modules rely on this data.

@glennmatthews
Copy link
Contributor

Looks like the metadata_class was added in Netbox netbox-community/netbox#4695 to handle a different issue with the OPTIONS content on Cables - are we sure that removing this is the right thing to do, or do we need to instead extend with a custom metadata_class that supports both status and termination_a_type information?

@FragmentedPacket
Copy link
Contributor Author

I thought I had responded, but I guess I didn't hit comment. I don't think I have an opinion on this so whichever way y'all think is beneficial and I will implement it.

@jathanism
Copy link
Contributor

This should be fine see

Looks like the metadata_class was added in Netbox netbox-community/netbox#4695 to handle a different issue with the OPTIONS content on Cables - are we sure that removing this is the right thing to do, or do we need to instead extend with a custom metadata_class that supports both status and termination_a_type information?

If you look at the code, despite the filter conditions, the two metadata subclasses are doing nearly the same thing:

  • ContentTypeMetadata is gating on field type ContentTypeField
  • StatusFieldMetadata is gating on field type StatusSerializerField

I would reckon that with a little debugging we could probably adapt StatusFieldMetadata to do both in this case.

@FragmentedPacket
Copy link
Contributor Author

I did try and get that working, but wasn't successful as I think that makes the most sense at this point. I will give it another go and hit you up if I have issues with it.

…peField, add new dynamic test, add example test for Cables.
@FragmentedPacket
Copy link
Contributor Author

I'm about to push up working changes as well as new tests, but wanted to get y'alls feedback on testing.

I think I also discovered another bug that the statuses that are returned are not properly filtered for the endpoint. Cables should only have three status choices and virtual machines should have 6. Numbers are arbitrary, but this is with a fresh install of Nautobot.

>>> nb.dcim.cables.choices()['status']
[{'value': 'active', 'display': 'Active'}, {'value': 'available', 'display': 'Available'}, {'value': 'connected', 'display': 'Connected'}, {'value': 'container', 'display': 'Container'}, {'value': 'decommissioned', 'display': 'Decommissioned'}, {'value': 'decommissioning', 'display': 'Decommissioning'}, {'value': 'deprecated', 'display': 'Deprecated'}, {'value': 'deprovisioning', 'display': 'Deprovisioning'}, {'value': 'dhcp', 'display': 'DHCP'}, {'value': 'failed', 'display': 'Failed'}, {'value': 'inventory', 'display': 'Inventory'}, {'value': 'offline', 'display': 'Offline'}, {'value': 'planned', 'display': 'Planned'}, {'value': 'provisioning', 'display': 'Provisioning'}, {'value': 'reserved', 'display': 'Reserved'}, {'value': 'retired', 'display': 'Retired'}, {'value': 'slaac', 'display': 'SLAAC'}, {'value': 'staged', 'display': 'Staged'}, {'value': 'staging', 'display': 'Staging'}]
>>> nb.virtualization.virtual_machines.choices()['status']
[{'value': 'active', 'display': 'Active'}, {'value': 'available', 'display': 'Available'}, {'value': 'connected', 'display': 'Connected'}, {'value': 'container', 'display': 'Container'}, {'value': 'decommissioned', 'display': 'Decommissioned'}, {'value': 'decommissioning', 'display': 'Decommissioning'}, {'value': 'deprecated', 'display': 'Deprecated'}, {'value': 'deprovisioning', 'display': 'Deprovisioning'}, {'value': 'dhcp', 'display': 'DHCP'}, {'value': 'failed', 'display': 'Failed'}, {'value': 'inventory', 'display': 'Inventory'}, {'value': 'offline', 'display': 'Offline'}, {'value': 'planned', 'display': 'Planned'}, {'value': 'provisioning', 'display': 'Provisioning'}, {'value': 'reserved', 'display': 'Reserved'}, {'value': 'retired', 'display': 'Retired'}, {'value': 'slaac', 'display': 'SLAAC'}, {'value': 'staged', 'display': 'Staged'}, {'value': 'staging', 'display': 'Staging'}]

I haven't been able to dig into it too much, but I believe this is the culprit.

https://github.com/nautobot/nautobot/blob/develop/nautobot/extras/api/serializers.py#L621

@glennmatthews
Copy link
Contributor

I think the bug identification and your analysis are correct. It should be possible to do something similar on the serializer field to what the form field does with StatusField.get_limit_choices_to() - looks like creating a custom get_queryset() method on StatusSerializerField would be the equivalent approach.

Copy link
Contributor

@glennmatthews glennmatthews left a comment

Choose a reason for hiding this comment

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

Thank you!

nautobot/dcim/tests/test_api.py Outdated Show resolved Hide resolved
nautobot/utilities/testing/api.py Outdated Show resolved Hide resolved
nautobot/utilities/testing/api.py Outdated Show resolved Hide resolved
nautobot/core/api/metadata.py Outdated Show resolved Hide resolved
@FragmentedPacket
Copy link
Contributor Author

This should be ready for review now with applicable tests and a few minor updates to properly return content_types that are serialized as ContentTypeField.

Copy link
Contributor

@glennmatthews glennmatthews left a comment

Choose a reason for hiding this comment

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

Looks great!

Copy link
Contributor

@jathanism jathanism left a comment

Choose a reason for hiding this comment

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

Elegant decision just making StatusFieldMetadata a subclass of ContentTypeMetadata!

@jathanism jathanism merged commit 17bdad2 into nautobot:develop Jun 9, 2021
@FragmentedPacket FragmentedPacket deleted the 452-cable-choices branch June 15, 2021 04:44
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.

Status not showing up in choices for DCIM/Cables
3 participants