Commit 016589c9 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

The whole API uses id's for identification now

parent 0ce7a593
......@@ -8,7 +8,7 @@ from .. import models, serializers as backend_serializers
class SiteSerializer(serializers.ModelSerializer):
class Meta:
model = models.Site
fields = ['name']
fields = ['id', 'name']
class ServiceSerializer(serializers.ModelSerializer):
......@@ -17,7 +17,7 @@ class ServiceSerializer(serializers.ModelSerializer):
class Meta:
model = models.Service
exclude = ['id']
exclude = []
class DeploymentSerializer(serializers.Serializer):
......
import logging
from django.shortcuts import get_object_or_404
from django.core.exceptions import ObjectDoesNotExist
from rest_framework import views, viewsets
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
......@@ -61,35 +62,26 @@ class ServiceViewSet(viewsets.ModelViewSet):
class SSHPublicKeyView(views.APIView):
def post(self, request):
if 'type' not in request.data:
LOGGER.error('SSHPublicKeyView: malformed request %s', request)
return _api_error_response()
request_type = request.data['type']
if request_type == 'remove':
if 'name' in request.data:
key = get_object_or_404(
models.SSHPublicKey,
name=request.data['name']
)
# we do not delete ssh keys directly, as we need to keep track
# of them until all clients have also deleted them
# key state: -> (1), if there are no deployments
# key state: -> (5), if there are deployments
key.delete_key()
return _api_state_response(request)
elif request_type == 'add':
if 'key' in request.data:
key = models.SSHPublicKey(
user=request.user,
name=request.data['key']['name'],
key=request.data['key']['key'],
)
# key state: -> (2)
key.save()
return _api_state_response(request)
if 'type' in request.data:
if request.data['type'] == 'remove':
if 'id' in request.data:
key = get_object_or_404(
models.SSHPublicKey,
id=request.data['id']
)
# we do not delete ssh keys directly, as we need to keep track
# of them until all clients have also deleted them
key.delete_key()
return _api_state_response(request)
elif request.data['type'] == 'add':
if 'key' in request.data:
key = models.SSHPublicKey(
user=request.user,
name=request.data['key']['name'],
key=request.data['key']['key'],
)
key.save()
return _api_state_response(request)
LOGGER.error('SSHPublicKeyView: malformed request %s', request)
return _api_error_response()
......@@ -106,19 +98,18 @@ class DeploymentView(views.APIView):
return _api_error_response()
request_type = request.data['type']
request_service = get_object_or_404(
models.Service, name=request.data['service']
request_service = models.Service.objects.get(
id=request.data['service'],
)
request_key = get_object_or_404(
models.SSHPublicKey,
name=request.data['key'],
request_key = models.SSHPublicKey.objects.get(
id=request.data['key'],
)
# check if there is already an deployment
# if not request.user.deployments.filter(service=request_service).exists():
try:
deployment = request.user.deployments.get(service=request_service)
except Exception:
except ObjectDoesNotExist:
if request_type == 'remove':
return _api_error_response()
......
......@@ -428,7 +428,6 @@ class Service(models.Model):
class SSHPublicKey(models.Model):
name = models.CharField(
max_length=150,
unique=True,
)
key = models.TextField(
max_length=1000
......
......@@ -9,9 +9,9 @@ from .models import SSHPublicKey
class GroupSerializer(serializers.ModelSerializer):
class Meta:
model = Group
fields = ['name']
fields = ['id', 'name']
class SSHPublicKeySerializer(serializers.ModelSerializer):
class Meta:
model = SSHPublicKey
fields = ['name', 'key']
fields = ['id', 'name', 'key']
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