Commit 8d0175e2 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Expand the user rest interface

Adds patching of Deployment's state_target and DeploymentStates
questionnaire (amongst other fields).
parent d74c445b
...@@ -2,11 +2,9 @@ ...@@ -2,11 +2,9 @@
import logging import logging
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from rest_framework import status, views, generics, exceptions from rest_framework import status, generics, exceptions
from rest_framework.response import Response from rest_framework.response import Response
from .. import models
from ..models import serializers, deployments from ..models import serializers, deployments
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
...@@ -44,8 +42,8 @@ class SSHPublicKeyView(generics.RetrieveDestroyAPIView): ...@@ -44,8 +42,8 @@ class SSHPublicKeyView(generics.RetrieveDestroyAPIView):
id=self.request.query_params.get('id'), id=self.request.query_params.get('id'),
) )
def perform_destroy(self, key): def perform_destroy(self, instance):
self.request.user.remove_key(key) self.request.user.remove_key(instance)
class SSHPublicKeyListView(generics.ListCreateAPIView): class SSHPublicKeyListView(generics.ListCreateAPIView):
...@@ -66,50 +64,63 @@ class ServiceListView(generics.ListAPIView): ...@@ -66,50 +64,63 @@ class ServiceListView(generics.ListAPIView):
return self.request.user.services.all() return self.request.user.services.all()
class DeploymentView(generics.RetrieveUpdateAPIView):
serializer_class = serializers.DeploymentSerializer
def get_serializer_context(self):
dep = self.get_object()
if isinstance(dep, deployments.ServiceDeployment):
self.request.data['resourcetype'] = 'ServiceDeployment'
elif isinstance(dep, deployments.VODeployment):
self.request.data['resourcetype'] = 'VODeployment'
return {
'request': self.request,
}
def get_object(self):
if 'id' not in self.request.query_params:
raise exceptions.ValidationError('Need "id" parameter')
return get_object_or_404(
self.request.user.deployments.all(),
id=self.request.query_params.get('id'),
)
def perform_update(self, serializer):
dep = serializer.save()
dep.target_changed()
class DeploymentListView(generics.ListCreateAPIView): class DeploymentListView(generics.ListCreateAPIView):
serializer_class = serializers.DeploymentSerializer serializer_class = serializers.DeploymentSerializer
def get_queryset(self): def get_queryset(self):
return self.request.user.deployments.all() return self.request.user.deployments.all()
def perform_create(self, serializer):
# TODO class DeploymentStateView(generics.RetrieveUpdateAPIView):
pass serializer_class = serializers.DeploymentStateSerializer
def get_object(self):
# basically obsolete if 'id' not in self.request.query_params:
# class ProvisioningView(views.APIView): raise exceptions.ValidationError('Need "id" parameter')
# authentication_classes = (OIDCTokenAuthHTTPBackend,)
# return get_object_or_404(
# def post(self, request): self.request.user.states.all(),
# for key in ['s']: id=self.request.query_params.get('id'),
# if key not in request.data: )
# return _error_response(request, 'Missing key "{}"'.format(key))
# def perform_update(self, serializer):
# service_name = request.data.get('s', '') state = serializer.save()
# state_target = request.data.get('state_target', deployments.DEPLOYED)
# if 'questionnaire' in self.request.data:
# service = None LOGGER.debug('Patch changed questionnaire')
# try: state.questionnaire_changed()
# service = request.user.services.get(
# name=service_name,
# ) class DeploymentStateListView(generics.ListCreateAPIView):
# serializer_class = serializers.DeploymentStateSerializer
# deployment = deployments.get_deployment(request.user, service=service)
# if deployment is not None: def get_queryset(self):
# if state_target == deployments.DEPLOYED: return self.request.user.states.all()
# deployment.user_deploy()
# elif state_target == deployments.NOT_DEPLOYED:
# deployment.user_remove()
# else:
# return _error_response(request, 'Invalid state_target "{}"'.format(state_target))
#
# return Response(
# serializers.DeploymentSerializer(deployment).data,
# )
#
# except models.Service.DoesNotExist:
# return _error_response(
# request,
# 'Service "{}" does not exist or you are not authorised to use it'.format(service_name),
# )
...@@ -4,6 +4,12 @@ from . import user_rest as views ...@@ -4,6 +4,12 @@ from . import user_rest as views
URLPATTERNS = [ URLPATTERNS = [
url(r'^ssh-key$', views.SSHPublicKeyView.as_view()), url(r'^ssh-key$', views.SSHPublicKeyView.as_view()),
url(r'^ssh-keys$', views.SSHPublicKeyListView.as_view()), url(r'^ssh-keys$', views.SSHPublicKeyListView.as_view()),
url(r'^services$', views.ServiceListView.as_view()), url(r'^services$', views.ServiceListView.as_view()),
url(r'^deployment$', views.DeploymentView.as_view()),
url(r'^deployments$', views.DeploymentListView.as_view()), url(r'^deployments$', views.DeploymentListView.as_view()),
url(r'^dep-state$', views.DeploymentStateView.as_view()),
url(r'^dep-states$', views.DeploymentStateListView.as_view()),
] ]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment