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