-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] REST API for main controller features #379
Closes #379
- Loading branch information
1 parent
a627b61
commit fab61ba
Showing
7 changed files
with
238 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from rest_framework import serializers | ||
from swapper import load_model | ||
|
||
from openwisp_users.api.mixins import FilterSerializerByOrgOwned | ||
from openwisp_utils.api.serializers import ValidatedModelSerializer | ||
|
||
Template = load_model('config', 'Template') | ||
Vpn = load_model('config', 'Vpn') | ||
Device = load_model('config', 'Device') | ||
Config = load_model('config', 'Config') | ||
|
||
|
||
class BaseMeta: | ||
read_only_fields = ['created', 'modified'] | ||
|
||
|
||
class TemplateSerializer(FilterSerializerByOrgOwned, ValidatedModelSerializer): | ||
config = serializers.JSONField() | ||
default_values = serializers.JSONField() | ||
|
||
class Meta(BaseMeta): | ||
model = Template | ||
fields = '__all__' | ||
|
||
|
||
class TemplateForConfigSerializer(serializers.RelatedField): | ||
def get_queryset(self): | ||
return Template.objects.all() | ||
|
||
def to_representation(self, instance): | ||
return {'id': instance.id, 'name': instance.name} | ||
|
||
|
||
class ConfigSerializer(ValidatedModelSerializer): | ||
templates = TemplateForConfigSerializer(many=True, read_only=True) | ||
context = serializers.JSONField() | ||
config = serializers.JSONField() | ||
|
||
class Meta(BaseMeta): | ||
model = Config | ||
# depth = 1 | ||
exclude = ('device',) | ||
|
||
|
||
class VpnSerializer(FilterSerializerByOrgOwned, ValidatedModelSerializer): | ||
config = serializers.JSONField() | ||
|
||
class Meta(BaseMeta): | ||
model = Vpn | ||
fields = '__all__' | ||
|
||
|
||
class DeviceListSerializer(ValidatedModelSerializer): | ||
class Meta(BaseMeta): | ||
model = Device | ||
fields = [ | ||
'id', | ||
'name', | ||
'organization', | ||
'mac_address', | ||
'key', | ||
'last_ip', | ||
'management_ip', | ||
'model', | ||
'os', | ||
'system', | ||
'notes', | ||
] | ||
|
||
|
||
class DeviceDetailSerializer(ValidatedModelSerializer): | ||
configuration = ConfigSerializer(read_only=False, source='config') | ||
|
||
class Meta(BaseMeta): | ||
model = Device | ||
fields = DeviceListSerializer.Meta.fields + [ | ||
'created', | ||
'modified', | ||
'configuration', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from django.urls import include, path | ||
|
||
from . import views as api_views | ||
|
||
urlpatterns = [ | ||
path( | ||
'controller/', | ||
include( | ||
[ | ||
path('template/', api_views.template_list, name='template_list',), | ||
path( | ||
'template/<str:pk>/', | ||
api_views.template_detail, | ||
name='template_detail', | ||
), | ||
path( | ||
'template/<str:pk>/configuration/', | ||
api_views.download_template_config, | ||
name='download_template_config', | ||
), | ||
path('vpn/', api_views.vpn_list, name='vpn_list',), | ||
path('vpn/<str:pk>/', api_views.vpn_detail, name='vpn_detail',), | ||
path( | ||
'vpn/<str:pk>/configuration/', | ||
api_views.download_vpn_config, | ||
name='download_vpn_config', | ||
), | ||
path('device/', api_views.device_list, name='device_list',), | ||
path( | ||
'device/<str:pk>/', api_views.device_detail, name='device_detail', | ||
), | ||
path( | ||
'device/<str:pk>/configuration/', | ||
api_views.download_device_config, | ||
name='download_device_config', | ||
), | ||
] | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
from rest_framework import pagination | ||
from rest_framework.authentication import SessionAuthentication | ||
from rest_framework.generics import ( | ||
ListCreateAPIView, | ||
RetrieveAPIView, | ||
RetrieveUpdateDestroyAPIView, | ||
) | ||
from rest_framework.permissions import DjangoModelPermissions, IsAuthenticated | ||
from swapper import load_model | ||
|
||
from openwisp_users.api.authentication import BearerAuthentication | ||
from openwisp_users.api.mixins import FilterByOrganizationManaged | ||
from openwisp_users.api.permissions import IsOrganizationManager | ||
|
||
from ..config.admin import BaseConfigAdmin | ||
from .serializers import ( | ||
DeviceDetailSerializer, | ||
DeviceListSerializer, | ||
TemplateSerializer, | ||
VpnSerializer, | ||
) | ||
|
||
Template = load_model('config', 'Template') | ||
Vpn = load_model('config', 'Vpn') | ||
Device = load_model('config', 'Device') | ||
|
||
|
||
class ListViewPagination(pagination.PageNumberPagination): | ||
page_size = 10 | ||
page_size_query_param = 'page_size' | ||
max_page_size = 100 | ||
|
||
|
||
class ProtectedAPIMixin(FilterByOrganizationManaged): | ||
authentication_classes = [BearerAuthentication, SessionAuthentication] | ||
permission_classes = [ | ||
IsAuthenticated, | ||
IsOrganizationManager, | ||
DjangoModelPermissions, | ||
] | ||
|
||
|
||
class TemplateListCreateView(ProtectedAPIMixin, ListCreateAPIView): | ||
serializer_class = TemplateSerializer | ||
queryset = Template.objects.all() | ||
pagination_class = ListViewPagination | ||
|
||
|
||
class TemplateDetailView(ProtectedAPIMixin, RetrieveUpdateDestroyAPIView): | ||
serializer_class = TemplateSerializer | ||
queryset = Template.objects.all() | ||
|
||
|
||
class DownloadTemplateconfiguration(ProtectedAPIMixin, RetrieveAPIView): | ||
serializer_class = TemplateSerializer | ||
queryset = Template.objects.none() | ||
model = Template | ||
|
||
def retrieve(self, request, *args, **kwargs): | ||
return BaseConfigAdmin.download_view(self, request, pk=kwargs['pk']) | ||
|
||
|
||
class VpnListCreateView(ProtectedAPIMixin, ListCreateAPIView): | ||
serializer_class = VpnSerializer | ||
queryset = Vpn.objects.all() | ||
pagination_class = ListViewPagination | ||
|
||
|
||
class VpnDetailView(ProtectedAPIMixin, RetrieveUpdateDestroyAPIView): | ||
serializer_class = VpnSerializer | ||
queryset = Vpn.objects.all() | ||
|
||
|
||
class DownloadVpnView(ProtectedAPIMixin, RetrieveAPIView): | ||
serializer_class = VpnSerializer | ||
queryset = Vpn.objects.none() | ||
model = Vpn | ||
|
||
def retrieve(self, request, *args, **kwargs): | ||
return BaseConfigAdmin.download_view(self, request, pk=kwargs['pk']) | ||
|
||
|
||
class DeviceListCreateView(ProtectedAPIMixin, ListCreateAPIView): | ||
serializer_class = DeviceListSerializer | ||
queryset = Device.objects.all() | ||
pagination_class = ListViewPagination | ||
|
||
|
||
class DeviceDetailView(ProtectedAPIMixin, RetrieveUpdateDestroyAPIView): | ||
serializer_class = DeviceDetailSerializer | ||
queryset = Device.objects.all() | ||
|
||
|
||
class DownloadDeviceView(ProtectedAPIMixin, RetrieveAPIView): | ||
serializer_class = DeviceListSerializer | ||
queryset = Device.objects.none() | ||
model = Device | ||
|
||
def retrieve(self, request, *args, **kwargs): | ||
return BaseConfigAdmin.download_view(self, request, pk=kwargs['pk']) | ||
|
||
|
||
template_list = TemplateListCreateView.as_view() | ||
template_detail = TemplateDetailView.as_view() | ||
download_template_config = DownloadTemplateconfiguration.as_view() | ||
vpn_list = VpnListCreateView.as_view() | ||
vpn_detail = VpnDetailView.as_view() | ||
download_vpn_config = DownloadVpnView.as_view() | ||
device_list = DeviceListCreateView.as_view() | ||
device_detail = DeviceDetailView.as_view() | ||
download_device_config = DownloadDeviceView().as_view() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters