Skip to content

Commit

Permalink
Fix URLs, add web applications to API
Browse files Browse the repository at this point in the history
  • Loading branch information
troeger committed Aug 18, 2020
1 parent 42c16d5 commit 98c9c10
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
5 changes: 5 additions & 0 deletions kubeportal/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from django.contrib.auth import get_user_model
from rest_framework import serializers
from kubeportal.models import WebApplication


class UserSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
fields = ('id', 'username', 'first_name', 'last_name', 'email')


class WebApplicationSerializer(serializers.ModelSerializer):
class Meta:
model = WebApplication
47 changes: 27 additions & 20 deletions kubeportal/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


from django.contrib.auth import get_user_model
from kubeportal.api.serializers import UserSerializer
from kubeportal.models import UserState
from kubeportal.api.serializers import UserSerializer, WebApplicationSerializer
from kubeportal.models import UserState, WebApplication
from kubeportal import kubernetes
from django.conf import settings

Expand All @@ -18,7 +18,17 @@ class UserView(viewsets.ReadOnlyModelViewSet):
serializer_class = UserSerializer


class KubeportalStatisticsView(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
class WebApplicationView(viewsets.ReadOnlyModelViewSet):
'''
API endpoint that allows for web applications to queried
@todo: Implement web application filtering
'''
queryset = WebApplication.objects.all()
serializer_class = WebApplicationSerializer


class KubeportalStatisticsView(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
'''
API endpoint that returns statistics for the Kubeportal installation.
'''
Expand All @@ -43,28 +53,25 @@ class ClusterStatisticsView(mixins.RetrieveModelMixin, mixins.ListModelMixin, vi
'''
API endpoint that returns statistics for the whole cluster.
'''
def retrieve(request, *args, **kwargs):

stats = {'kubernetes_version': kubernetes.get_kubernetes_version,
'apiserver_url': kubernetes.get_apiserver,
'node_count': kubernetes.get_number_of_nodes,
'cpu_count': kubernetes.get_number_of_cpus,
'mainmemory_sum': kubernetes.get_memory_sum,
'pod_count': kubernetes.get_number_of_pods,
'volume_count': kubernetes.get_number_of_volumes}

def retrieve(self, request, *args, **kwargs):
# Production tests have shown that some of these Kubernetes calls may take a moment.
# Given that, we offer individual API endpoints per single statistic and let the frontend
# fetch the stuff async.

key = kwargs['pk']
if key == 'kubernetes_version':
return Response(kubernetes.get_kubernetes_version())
if key == 'apiserver_url':
return Response(kubernetes.get_apiserver())
if key == 'node_count':
return Response(kubernetes.get_number_of_nodes())
if key == 'cpu_count':
return Response(kubernetes.get_number_of_cpus())
if key == 'mainmemory_sum':
return Response(kubernetes.get_memory_sum())
if key == 'pod_count':
return Response(kubernetes.get_number_of_pods())
if key == 'volume_count':
return Response(kubernetes.get_number_of_volumes())

raise NotFound
if key in self.stats.keys():
return Response(self.stats[key]())
else:
raise NotFound

def list(request, *args, **kwargs):
return Response(['kubernetes_version', 'apiserver_url', 'node_count', 'cpu_count', 'mainmemory_sum', 'pod_count', 'volume_count'])
Expand Down
2 changes: 2 additions & 0 deletions kubeportal/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class Common(Configuration):
]

REST_FRAMEWORK = {
'DEFAULT_VERSIONING_CLASS':
'rest_framework.versioning.NamespaceVersioning',
'DEFAULT_AUTHENTICATION_CLASSES': [
'dj_rest_auth.jwt_auth.JWTCookieAuthentication',
],
Expand Down
5 changes: 3 additions & 2 deletions kubeportal/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
from rest_framework import routers
router = routers.SimpleRouter()
router.register('users', api_views.UserView)
router.register('statistics/kubeportal', api_views.KubeportalStatisticsView, basename='kubeportal_statistics')
router.register('statistics/cluster', api_views.ClusterStatisticsView, basename='cluster_statistics')
router.register('kubeportal/statistics', api_views.KubeportalStatisticsView, basename='kubeportal_statistics')
router.register('kubeportal/webapplications', api_views.WebApplicationView, basename='kubeportal_webapplications')
router.register('cluster/statistics', api_views.ClusterStatisticsView, basename='cluster_statistics')

urlpatterns = [
# frontend web views
Expand Down

0 comments on commit 98c9c10

Please sign in to comment.