Commit 99fba34f authored by Lukas Burgey's avatar Lukas Burgey

Implement the questionnaire logic

parent d45e4e76
......@@ -5,3 +5,4 @@ db.cnf
static
deployment
deploy
runtest
......@@ -7,6 +7,7 @@ from django.contrib.sessions.models import Session
from ...models import User, RabbitMQInstance
LOGGER = logging.getLogger(__name__)
CLIENT_DEBUGGING = False
ALLOW = HttpResponse('allow')
DENY = HttpResponse('deny')
......@@ -144,24 +145,26 @@ def resource_endpoint(request):
_webpage_client_userid(request)
and _resource_authorized_webpage_client(request)
):
LOGGER.debug(
'Granted %s access to resource %s %s to client',
permission,
resource,
name,
)
if CLIENT_DEBUGGING:
LOGGER.debug(
'Granted %s access to resource %s %s to client',
permission,
resource,
name,
)
return ALLOW
if (
_apiclient_valid(request)
and _resource_authorized_apiclient(request)
):
LOGGER.debug(
'Granted %s access to resource %s %s to client',
permission,
resource,
name,
)
if CLIENT_DEBUGGING:
LOGGER.debug(
'Granted %s access to resource %s %s to client',
permission,
resource,
name,
)
return ALLOW
LOGGER.error(
......@@ -187,12 +190,13 @@ def topic_endpoint(request):
routing_key == webpage_client_userid
and not 'write' in permission
):
LOGGER.debug(
'Granted %s access to %s %s to client',
permission,
resource,
routing_key,
)
if CLIENT_DEBUGGING:
LOGGER.debug(
'Granted %s access to %s %s to client',
permission,
resource,
routing_key,
)
return ALLOW
LOGGER.error(
......
......@@ -3,7 +3,7 @@ import logging
from rest_framework import generics, views
from rest_framework.authentication import BasicAuthentication
from rest_framework.response import Response
from .serializers import SiteSerializer, ServiceSerializer, RabbitMQInstanceSerializer
from .serializers import DeploymentTaskSerializer, ServiceSerializer, RabbitMQInstanceSerializer
from ..models import RabbitMQInstance
LOGGER = logging.getLogger(__name__)
......@@ -13,12 +13,12 @@ LOGGER = logging.getLogger(__name__)
AUTHENTICATION_CLASSES = (BasicAuthentication, )
class DeploymentsView(generics.RetrieveAPIView):
class DeploymentsView(generics.ListAPIView):
authentication_classes = AUTHENTICATION_CLASSES
serializer_class = SiteSerializer
serializer_class = DeploymentTaskSerializer
def get_object(self):
return self.request.user.site
def get_queryset(self):
return self.request.user.site.tasks
# the client has to fetch the configuration (like services etc.) here
......@@ -45,8 +45,7 @@ class AckView(views.APIView):
# find the corresponding task for this item
for item in request.user.site.task_items.all():
if item.task.id == int(task_id):
item.task.item_finished(request.user.site)
LOGGER.debug('Got acknowledgement for task %s', task_id)
item.success()
return Response({'ok': True})
# this is no critical
......@@ -71,15 +70,15 @@ class ResponseView(views.APIView):
if task_item is not None:
if status == 'success':
task_item.task.item_finished(request.user.site)
task_item.done()
return Response({'ok': True})
elif status == 'fail':
task_item.task.item_failed(request.user.site)
task_item.failed()
return Response({'ok': True})
elif status == 'reject':
task_item.task.item_rejected(request.user.site)
task_item.rejected(request.data['output']['questionnaire'])
return Response({'ok': True})
LOGGER.info('%s executed the obsolete task#%s', request.user, task_id)
......
......@@ -24,6 +24,7 @@ class DeploymentTaskItemSerializer(serializers.ModelSerializer):
service = ServiceSerializer()
key = backend_serializers.SSHPublicKeySerializerB()
site = SiteSerializer()
questionnaire = serializers.JSONField()
class Meta:
model = models.DeploymentTaskItem
......@@ -33,6 +34,8 @@ class DeploymentTaskItemSerializer(serializers.ModelSerializer):
'service',
'site',
'state',
'questionnaire',
'id',
]
......@@ -45,6 +48,7 @@ class DeploymentTaskSerializer(serializers.ModelSerializer):
'action',
'key',
'service',
'id',
]
......
......@@ -6,4 +6,5 @@ URLPATTERNS = [
url(r'^sshkey', views.SSHPublicKeyView.as_view()),
url(r'^deployments', views.DeploymentView.as_view()),
url(r'^delete_user', views.UserDeletionView.as_view()),
url(r'^questionnaire', views.QuestionnaireView.as_view()),
]
......@@ -129,6 +129,21 @@ class DeploymentView(views.APIView):
return _api_state_response(request)
class QuestionnaireView(views.APIView):
def post(self, request):
task_item_id = request.query_params.get('id', '')
if task_item_id != '':
item = models.DeploymentTaskItem.objects.filter(id=int(task_item_id))
if item.exists():
item.first().questionnaire_answered(
answers=request.data,
)
else:
LOGGER.error('Received questionnaire answer for non existing item')
return _api_state_response(request)
class UserDeletionView(views.APIView):
def delete(self, request):
# this also logs out the user
......
This diff is collapsed.
......@@ -15,6 +15,8 @@ TEST_USERINFO = {
'name': TEST_NAME,
'sub': TEST_SUB,
}
TEST_SERVICE = 'test_service'
TEST_SITE = 'test_site'
def setup_fixture():
......@@ -41,14 +43,14 @@ def setup_fixture():
user.save()
site = models.Site(
name=TEST_NAME,
description=TEST_NAME,
name=TEST_SITE,
description=TEST_SITE,
)
site.save()
service = models.Service(
name=TEST_NAME,
description=TEST_NAME,
name=TEST_SERVICE,
description=TEST_SERVICE,
)
service.save()
service.site.add(site)
......@@ -107,11 +109,12 @@ class DeploymentTest(TestCase):
def test_deployment(self):
user = models.User.objects.get(username=TEST_NAME)
key = models.SSHPublicKey.objects.get(name=TEST_NAME)
service = models.Service.objects.get(name=TEST_NAME)
site = models.Site.objects.get(name=TEST_NAME)
service = models.Service.objects.get(name=TEST_SERVICE)
deployment = models.Deployment.get_deployment(user, service)
self.assertIsNotNone(deployment)
# no tasks exist yet
self.assertFalse(deployment.deploys.exists())
self.assertFalse(deployment.withdrawals.exists())
......@@ -119,16 +122,23 @@ class DeploymentTest(TestCase):
deployment=deployment,
key=key,
)
# one deploy should exist
self.assertTrue(deployment.deploys.exists())
self.assertFalse(deployment.withdrawals.exists())
withdrawal_task = models.DeploymentTask.construct_withdrawal_task(
models.DeploymentTask.construct_withdrawal_task(
deployment=deployment,
key=key,
)
# one withdraw should exist
self.assertFalse(deployment.deploys.exists())
self.assertTrue(deployment.withdrawals.exists())
withdrawal_task.item_finished(site)
# "execute" the withdrawals
for task in deployment.withdrawals.all():
for item in task.task_items.all():
item.success()
self.assertFalse(deployment.deploys.exists())
self.assertFalse(deployment.withdrawals.exists())
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