Skip to content

Commit

Permalink
Closes netbox-community#13808: render-config API endpoint for virtual…
Browse files Browse the repository at this point in the history
… machines
  • Loading branch information
LuPo committed Nov 16, 2023
1 parent d52a6d3 commit 2ea3a51
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions netbox/virtualization/api/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from rest_framework.decorators import action
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.routers import APIRootView

from dcim.models import Device
from extras.api.mixins import ConfigContextQuerySetMixin
from extras.api.mixins import ConfigContextQuerySetMixin, ConfigTemplateRenderMixin
from netbox.api.viewsets import NetBoxModelViewSet
from netbox.api.renderers import TextRenderer
from utilities.utils import count_related
from virtualization import filtersets
from virtualization.models import Cluster, ClusterGroup, ClusterType, VirtualMachine, VMInterface
Expand Down Expand Up @@ -52,7 +56,7 @@ class ClusterViewSet(NetBoxModelViewSet):
# Virtual machines
#

class VirtualMachineViewSet(ConfigContextQuerySetMixin, NetBoxModelViewSet):
class VirtualMachineViewSet(ConfigContextQuerySetMixin, NetBoxModelViewSet, ConfigTemplateRenderMixin):
queryset = VirtualMachine.objects.prefetch_related(
'site', 'cluster', 'device', 'role', 'tenant', 'platform', 'primary_ip4', 'primary_ip6', 'tags'
)
Expand All @@ -78,6 +82,23 @@ def get_serializer_class(self):

return serializers.VirtualMachineWithConfigContextSerializer

@action(detail=True, methods=['post'], url_path='render-config', renderer_classes=[JSONRenderer, TextRenderer])
def render_config(self, request, pk):
"""
Resolve and render the preferred ConfigTemplate for this virtual machine.
"""
vm = self.get_object()
configtemplate = vm.get_config_template()
if not configtemplate:
return Response({'error': 'No config template found for this virtualmachine.'}, status=HTTP_400_BAD_REQUEST)

# Compile context data
context_data = vm.get_config_context()
context_data.update(request.data)
context_data.update({'virtualmachine': vm})

return self.render_configtemplate(request, configtemplate, context_data)


class VMInterfaceViewSet(NetBoxModelViewSet):
queryset = VMInterface.objects.prefetch_related(
Expand Down

0 comments on commit 2ea3a51

Please sign in to comment.