Skip to content

Commit

Permalink
Merge pull request #8176 from netbox-community/7846-inventoryitem-com…
Browse files Browse the repository at this point in the history
…ponent

Closes #7846: Associate inventory items with device components
  • Loading branch information
jeremystretch authored Dec 28, 2021
2 parents a58f1c6 + 4c5a5c7 commit e9910d1
Show file tree
Hide file tree
Showing 31 changed files with 545 additions and 902 deletions.
4 changes: 3 additions & 1 deletion docs/release-notes/version-3.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ FIELD_CHOICES = {
* [#7681](https://github.com/netbox-community/netbox/issues/7681) - Add `service_id` field for provider networks
* [#7759](https://github.com/netbox-community/netbox/issues/7759) - Improved the user preferences form
* [#7784](https://github.com/netbox-community/netbox/issues/7784) - Support cluster type assignment for config contexts
* [#7846](https://github.com/netbox-community/netbox/issues/7846) - Enable associating inventory items with device components
* [#8168](https://github.com/netbox-community/netbox/issues/8168) - Add `min_vid` and `max_vid` fields to VLAN group

### Other Changes
Expand Down Expand Up @@ -76,7 +77,8 @@ FIELD_CHOICES = {
* dcim.Interface
* Added `module` field
* dcim.InventoryItem
* Added `role` field
* Added `component_type`, `component_id`, and `role` fields
* Added read-only `component` field
* dcim.PowerPort
* Added `module` field
* dcim.PowerOutlet
Expand Down
19 changes: 17 additions & 2 deletions netbox/dcim/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,17 +810,32 @@ class InventoryItemSerializer(PrimaryModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='dcim-api:inventoryitem-detail')
device = NestedDeviceSerializer()
parent = serializers.PrimaryKeyRelatedField(queryset=InventoryItem.objects.all(), allow_null=True, default=None)
manufacturer = NestedManufacturerSerializer(required=False, allow_null=True, default=None)
role = NestedInventoryItemRoleSerializer(required=False, allow_null=True)
manufacturer = NestedManufacturerSerializer(required=False, allow_null=True, default=None)
component_type = ContentTypeField(
queryset=ContentType.objects.filter(MODULAR_COMPONENT_MODELS),
required=False,
allow_null=True
)
component = serializers.SerializerMethodField(read_only=True)
_depth = serializers.IntegerField(source='level', read_only=True)

class Meta:
model = InventoryItem
fields = [
'id', 'url', 'display', 'device', 'parent', 'name', 'label', 'role', 'manufacturer', 'part_id', 'serial',
'asset_tag', 'discovered', 'description', 'tags', 'custom_fields', 'created', 'last_updated', '_depth',
'asset_tag', 'discovered', 'description', 'component_type', 'component_id', 'component', 'tags',
'custom_fields', 'created', 'last_updated', '_depth',
]

@swagger_serializer_method(serializer_or_field=serializers.DictField)
def get_component(self, obj):
if obj.component is None:
return None
serializer = get_serializer_for_model(obj.component, prefix='Nested')
context = {'request': self.context['request']}
return serializer(obj.component, context=context).data


#
# Device component roles
Expand Down
21 changes: 18 additions & 3 deletions netbox/dcim/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,31 @@


#
# PowerFeeds
# Power feeds
#

POWERFEED_VOLTAGE_DEFAULT = 120

POWERFEED_AMPERAGE_DEFAULT = 20

POWERFEED_MAX_UTILIZATION_DEFAULT = 80 # Percentage


#
# Device components
#

MODULAR_COMPONENT_MODELS = Q(
app_label='dcim',
model__in=(
'consoleport',
'consoleserverport',
'frontport',
'interface',
'poweroutlet',
'powerport',
'rearport',
))


#
# Cabling and connections
#
Expand Down
2 changes: 2 additions & 0 deletions netbox/dcim/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,8 @@ class InventoryItemFilterSet(PrimaryModelFilterSet, DeviceComponentFilterSet):
to_field_name='slug',
label='Role (slug)',
)
component_type = ContentTypeFilter()
component_id = MultiValueNumberFilter()
serial = django_filters.CharFilter(
lookup_expr='iexact'
)
Expand Down
4 changes: 2 additions & 2 deletions netbox/dcim/forms/bulk_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from extras.forms import CustomFieldsMixin
from extras.models import Tag
from utilities.forms import DynamicModelMultipleChoiceField, form_from_model
from .object_create import ComponentForm
from .object_create import ComponentCreateForm

__all__ = (
'ConsolePortBulkCreateForm',
Expand All @@ -24,7 +24,7 @@
# Device components
#

class DeviceBulkAddComponentForm(CustomFieldsMixin, ComponentForm):
class DeviceBulkAddComponentForm(CustomFieldsMixin, ComponentCreateForm):
pk = forms.ModelMultipleChoiceField(
queryset=Device.objects.all(),
widget=forms.MultipleHiddenInput()
Expand Down
Loading

0 comments on commit e9910d1

Please sign in to comment.