Commit ab06774f authored by Lukas Burgey's avatar Lukas Burgey

Fix the generation of DeploymentStateItems

parent 1d0fdc4d
......@@ -40,40 +40,65 @@ class ConfigurationView(views.APIView):
return Response(response)
def response_view_error(err):
return Response(
data={'error': err},
status=500,
)
class ResponseView(views.APIView):
authentication_classes = AUTHENTICATION_CLASSES
def post(self, request):
client_site = request.user.site
output = request.data.get('output', {})
state_id = request.data.get('id', None)
_service = request.data.get('service', {})
service = None
service_query = client_site.services.filter(
name=_service['name'],
)
#LOGGER.debug("[ResponseView]services: %s %s", service_query, len(service_query))
if service_query.exists() and len(service_query) == 1:
service = service_query.first()
else:
LOGGER.error(
'[ResponseView] Unknown service %s from %s',
_service['name'],
request.user,
)
return response_view_error('unable to determine service')
if state_id is None:
err = 'no state id'
LOGGER.error('Error parsing response from %s: %s', request.user, err)
return Response(
data={'error': err},
status=500,
)
return response_view_error(err)
# find the corresponding state for this item
# find the corresponding DeploymentStateItem for this response
state_item = None
for item in request.user.site.state_items.all():
if item.parent.id == int(state_id):
state_item = item
break
query = client_site.state_items.filter(
parent__id=int(state_id),
site=client_site,
service=service,
)
if query.exists():
if len(query) == 1:
state_item = query.first()
else:
LOGGER.error('[ResponseView] ambiguous DStateItem')
return response_view_error('ambiguous DeploymentStateItem')
else:
LOGGER.error('[ResponseView] No matching DStateItem')
return response_view_error('no matching DeploymentStateItem')
if state_item is not None:
err = state_item.client_response(output)
if err is not None:
LOGGER.error('Error parsing response from %s: %s', request.user, err)
return Response(
data={'error': err},
status=500,
)
LOGGER.error('[ResponseView] Error parsing response from %s: %s', request.user, err)
return response_view_error(err)
return Response({})
LOGGER.info('%s executed the obsolete state#%s', request.user, state_id)
return Response(
data={'error': 'obsolete_state'},
status=500,
)
LOGGER.info('[ResponseView] %s executed the obsolete state#%s', request.user, state_id)
return response_view_error('obsolete state')
......@@ -844,11 +844,16 @@ class DeploymentState(models.Model):
elif deployment.group is not None:
# every site which provides a service for group
for site in deployment.sites:
deploy = DeploymentStateItem(
parent=state,
site=site,
)
deploy.save()
for service in Service.objects.filter(
groups=deployment.group,
site=site,
):
deploy = DeploymentStateItem(
parent=state,
site=site,
service=service,
)
deploy.save()
return state
......@@ -906,14 +911,12 @@ class DeploymentState(models.Model):
def __str__(self):
if self.service is not None:
return "{}:{}#{}".format(
return "{}#{}".format(
self.service,
self.key,
self.id,
)
return "{}:{}#{}".format(
return "{}#{}".format(
self.group,
self.key,
self.id,
)
......@@ -931,6 +934,13 @@ class DeploymentStateItem(models.Model):
related_name='state_items',
on_delete=models.CASCADE,
)
service = models.ForeignKey(
Service,
related_name='state_items',
on_delete=models.CASCADE,
null=True,
blank=True,
)
state = models.CharField(
max_length=50,
choices=STATE_CHOICES,
......@@ -947,6 +957,7 @@ class DeploymentStateItem(models.Model):
default=credential_default,
)
@property
def user(self):
return self.parent.user
......@@ -955,14 +966,11 @@ class DeploymentStateItem(models.Model):
def key(self):
return self.parent.key
@property
def service(self):
return self.parent.service
@property
def group(self):
return self.parent.group
# TODO this should not be needed
@property
def services(self):
return self.parent.services.filter(groups=self.group)
......@@ -1050,16 +1058,14 @@ class DeploymentStateItem(models.Model):
def __str__(self):
if self.group is not None:
return "{}:{}:{}@{}#{}".format(
return "{}:{}@{}#{}".format(
self.group,
self.service,
self.key,
self.site,
self.id,
)
return "{}:{}@{}#{}".format(
return "{}:@{}#{}".format(
self.service,
self.key,
self.site,
self.id,
)
......
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