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

Expand the user rest interface

Adds patching of Deployment's state_target and DeploymentStates
questionnaire (amongst other fields).
parent d74c445b
......@@ -2,11 +2,9 @@
import logging
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 .. import models
from ..models import serializers, deployments
LOGGER = logging.getLogger(__name__)
......@@ -44,8 +42,8 @@ class SSHPublicKeyView(generics.RetrieveDestroyAPIView):
id=self.request.query_params.get('id'),
)
def perform_destroy(self, key):
self.request.user.remove_key(key)
def perform_destroy(self, instance):
self.request.user.remove_key(instance)
class SSHPublicKeyListView(generics.ListCreateAPIView):
......@@ -66,50 +64,63 @@ class ServiceListView(generics.ListAPIView):
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):
serializer_class = serializers.DeploymentSerializer
def get_queryset(self):
return self.request.user.deployments.all()
def perform_create(self, serializer):
# TODO
pass
# basically obsolete
# class ProvisioningView(views.APIView):
# authentication_classes = (OIDCTokenAuthHTTPBackend,)
#
# def post(self, request):
# for key in ['s']:
# if key not in request.data:
# return _error_response(request, 'Missing key "{}"'.format(key))
#
# service_name = request.data.get('s', '')
# state_target = request.data.get('state_target', deployments.DEPLOYED)
#
# service = None
# try:
# service = request.user.services.get(
# name=service_name,
# )
#
# deployment = deployments.get_deployment(request.user, service=service)
# if deployment is not None:
# if state_target == deployments.DEPLOYED:
# 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),
# )
class DeploymentStateView(generics.RetrieveUpdateAPIView):
serializer_class = serializers.DeploymentStateSerializer
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.states.all(),
id=self.request.query_params.get('id'),
)
def perform_update(self, serializer):
state = serializer.save()
if 'questionnaire' in self.request.data:
LOGGER.debug('Patch changed questionnaire')
state.questionnaire_changed()
class DeploymentStateListView(generics.ListCreateAPIView):
serializer_class = serializers.DeploymentStateSerializer
def get_queryset(self):
return self.request.user.states.all()
......@@ -4,6 +4,12 @@ from . import user_rest as views
URLPATTERNS = [
url(r'^ssh-key$', views.SSHPublicKeyView.as_view()),
url(r'^ssh-keys$', views.SSHPublicKeyListView.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'^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