Commit fed72fb7 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Refactor the client response handling

parent 1c64600c
...@@ -4,6 +4,5 @@ from . import views ...@@ -4,6 +4,5 @@ from . import views
url(r'^deployments', views.DeploymentsView.as_view()), url(r'^deployments', views.DeploymentsView.as_view()),
url(r'^config', views.ConfigurationView.as_view()), url(r'^config', views.ConfigurationView.as_view()),
url(r'^ack/(?P<state_id>\d+)', views.AckView.as_view()),
url(r'^response', views.ResponseView.as_view()), url(r'^response', views.ResponseView.as_view()),
] ]
...@@ -40,64 +40,37 @@ class ConfigurationView(views.APIView): ...@@ -40,64 +40,37 @@ class ConfigurationView(views.APIView):
return Response(response) return Response(response)
class AckView(views.APIView): class ResponseView(views.APIView):
authentication_classes = AUTHENTICATION_CLASSES authentication_classes = AUTHENTICATION_CLASSES
def delete(self, request, state_id=None): def post(self, request):
# find the corresponding state for this item output ='output', {})
for item in state_id ='id', None)
if == int(state_id):
if item.parent.state_target == 'deployed':
return Response({})'%s executed the obsolete state#%s', request.user, state_id) if state_id is None:
err = 'no state id'
LOGGER.error('Error parsing response from %s: %s', request.user, err)
return Response( return Response(
data={'error': 'obsolete_state'}, data={'error': err},
status=500, status=500,
) )
class ResponseView(views.APIView):
authentication_classes = AUTHENTICATION_CLASSES
def post(self, request):
output =['output']
status = output['status']
state_id =['id']
# LOGGER.debug('%s responded to state %s:\n%s', request.user, state_id,
# find the corresponding state for this item # find the corresponding state for this item
state_item = None state_item = None
for item in for item in
if == int(state_id): if == int(state_id):
state_item = item state_item = item
if state_item is not None: if state_item is not None:
if status == 'deployed': err = state_item.client_response(output)
state_item.client_deployed( if err is not None:
credentials=output.get('credentials', {}), LOGGER.error('Error parsing response from %s: %s', request.user, err)
return Response({})
elif status == 'removed':
return Response({})
elif status == 'questionnaire':
return Response({})
LOGGER.error("Unrecognized response status from client: %s", status)
return Response( return Response(
data={'error': 'unknown status'}, data={'error': err},
status=500, status=500,
) )
return Response({})'%s executed the obsolete state#%s', request.user, state_id)'%s executed the obsolete state#%s', request.user, state_id)
return Response( return Response(
...@@ -26,6 +26,8 @@ STATE_CHOICES = ( ...@@ -26,6 +26,8 @@ STATE_CHOICES = (
('deployed', 'Deployed'), ('deployed', 'Deployed'),
('not_deployed', 'Not Deployed'), ('not_deployed', 'Not Deployed'),
('questionnaire', 'Questionnaire'), ('questionnaire', 'Questionnaire'),
('failed', 'Failed'),
('rejected', 'Rejected'),
) )
...@@ -877,31 +879,39 @@ class DeploymentStateItem(models.Model): ...@@ -877,31 +879,39 @@ class DeploymentStateItem(models.Model):
self._set_state('deployment_pending') self._set_state('deployment_pending')
self.parent.publish_to_client() self.parent.publish_to_client()
# client: deployed # returns None on success, or a string describing an error
def client_deployed(self, credentials=None): def client_response(self, output):
self.credentials = credentials status = output.get('status', 'undefined')
self._set_state('deployed') if status != 'undefined':
# update values
# client: removed if status == 'deployed':
def client_removed(self): self.credentials = output.get('credentials', {})
# TODO check if all values are reset correctly elif status == 'removed':
self.credentials = credential_default() self.credentials = credential_default()
self.questionnaire = questionnaire_default() self.questionnaire = questionnaire_default()
self._set_state('not_deployed') elif status == 'questionnaire':
self.questionnaire = output.get('questionnaire', {})
# this removal maybe was the last of out ssh key elif status == 'rejected':
self.key.try_final_deletion() pass
elif status == 'failed':
return 'unknown state '+status
return None
return 'missing status in output'
# client: questionnaire
def client_questionnaire(self, questionnaire=None):
self.questionnaire = questionnaire
def msg(self, msg): def msg(self, msg):
return '[DSItem:{}] {}'.format(self, msg) return '[DSItem:{}] {}'.format(self, msg)
def _set_state(self, state): def _set_state(self, state):
if self.state == 'rejected':'refusing to change state of rejected state_item')
self.state = state self.state = state
LOGGER.debug(self.msg('state: '+self.state)) LOGGER.debug(self.msg('state: '+self.state))
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