Commit 264736a0 authored by Lukas Burgey's avatar Lukas Burgey

Restructure the user rest view

parent 8dacca2d
......@@ -10,7 +10,7 @@ from rest_framework.response import Response
from .. import models
from ..models import serializers
from ..models import serializers, deployments
LOGGER = logging.getLogger(__name__)
......@@ -39,37 +39,42 @@ class ProvisioningView(views.APIView):
permission_classes = (AllowAny,)
def post(self, request):
if 'at' not in request.data:
return _error_response(request, "Need access token field 'at'")
if 'iss' not in request.data:
return _error_response(request, "Need issuer uri field 'iss'")
for key in ['at', 'iss', 'key_name', 's']:
if key not in request.data:
return _error_response(request, 'Missing key "{}"'.format(key))
access_token = request.data.get('at', '')
issuer_uri = request.data.get('iss', '')
key_name = request.data.get('key_name', '')
service_name = request.data.get('s', '')
user = authenticate(
request,
token=request.data['at'],
issuer_uri=request.data['iss'],
token=access_token,
issuer_uri=issuer_uri,
)
if user is None:
return _error_response(request, "Unable to authenticate user")
return _error_response(
request,
'Unable to authenticate user with token "{}" at issuer "{}"'.format(
access_token,
issuer_uri,
),
)
if user is None:
return _error_response("Unable to authenticate user")
LOGGER.debug("USER-RESTAPI: authenticated user %s using access token", user)
if 'key_name' not in request.data:
return _error_response(request, "Need ssh key field 'key_name'")
ssh_key = None
try:
ssh_key = user.ssh_keys.get(
name=request.data['key_name']
name=request.data['key_name'],
)
except models.SSHPublicKey.DoesNotExist:
if 'key_value' not in request.data:
return _error_response(request, "Need ssh key field 'key_value'")
return _error_response(request, 'Need ssh key field "key_value" to create the key "{}"'.format(
key_name,
))
ssh_key = models.SSHPublicKey(
name=request.data['key_name'],
......@@ -78,24 +83,22 @@ class ProvisioningView(views.APIView):
)
ssh_key.save()
if 's' not in request.data:
return _error_response(request, "Need service identifier field 's'")
service = None
try:
service = user.services.get(
name=request.data['s'],
name=service_name,
)
# the problem is that without VO name the state items may be ambiguous
state_items = user.state_items.filter(service=service)
return Response({
'state_items': serializers.DeploymentStateSerializer(state_items, many=True).data,
'service': serializers.ServiceSerializer(service).data,
'ssh_key': serializers.SSHPublicKeySerializer(ssh_key).data,
})
deployment = deployments.get_deployment(user, service=service)
if deployment is not None:
deployment.user_deploy()
return Response(
serializers.DeploymentSerializer(deployment).data,
)
except models.Service.DoesNotExist:
return _error_response(
request,
"Service '%s' does not exist / you are not authorised to use it".format(request.data['s']),
'Service "{}" does not exist or you are not authorised to use it'.format(service_name),
)
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