Commit 68673d49 authored by Lukas Burgey's avatar Lukas Burgey

Make some properties not nullable

parent 147c1088
# Generated by Django 2.1.3 on 2018-11-20 17:58
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('backend', '0023_rename'),
]
operations = [
migrations.AlterField(
model_name='deployment',
name='user',
field=models.ForeignKey(default=3, on_delete=django.db.models.deletion.CASCADE, related_name='deployments', to=settings.AUTH_USER_MODEL),
preserve_default=False,
),
migrations.AlterField(
model_name='deployment',
name='vo',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='deployments', to='backend.VO'),
preserve_default=False,
),
migrations.AlterField(
model_name='deploymentstate',
name='parent',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='state_items', to='backend.Deployment'),
),
]
......@@ -148,16 +148,13 @@ class Deployment(models.Model):
user = models.ForeignKey(
User,
related_name='deployments',
on_delete=models.SET_NULL,
null=True,
on_delete=models.CASCADE,
)
vo = models.ForeignKey(
VO,
related_name='deployments',
on_delete=models.CASCADE,
null=True,
blank=True,
)
# which state do we currently want to reach?
......@@ -174,9 +171,7 @@ class Deployment(models.Model):
# credentials provided by the backend to the clients
@property
def credentials(self):
if self.user is not None:
return self.user.credentials
return {}
return self.user.credentials
@property
def state(self):
......@@ -199,10 +194,7 @@ class Deployment(models.Model):
@property
def services(self):
if self.vo is not None:
return self.vo.services.all()
else:
return Service.objects.none()
return self.vo.services.all()
@property
def sites(self):
......@@ -211,37 +203,24 @@ class Deployment(models.Model):
for service in self.services
]
def create_state_items(self, site=None):
def create_state_items(self):
for service in self.services:
LOGGER.debug('create_state_items: creating DeploymentState for service %s at sites %s', service, service.site.all())
if site is not None:
# if not service.site.exists():
# raise ValueError('Cannot create state item for service without site')
for service_site in service.site.all():
# LOGGER.debug('create_state_items: creating DeploymentStates for service %s at site %s', service, service_site)
DeploymentState.get_state_item(
parent=self,
user=self.user,
site=site,
site=service_site,
service=service,
).save()
return
else:
# if not service.site.exists():
# raise ValueError('Cannot create state item for service without site')
for service_site in service.site.all():
# LOGGER.debug('create_state_items: creating DeploymentStates for service %s at site %s', service, service_site)
DeploymentState.get_state_item(
parent=self,
user=self.user,
site=service_site,
service=service,
).save()
@classmethod
def get_deployment(cls, user, vo=None, site=None):
if vo is None:
raise ValueError('get_deployment needs a service or a vo')
def get_deployment(cls, user, vo):
try:
return cls.objects.get(
user=user,
......@@ -255,7 +234,7 @@ class Deployment(models.Model):
)
deployment.save()
deployment.create_state_items(site=site)
deployment.create_state_items()
LOGGER.debug(deployment.msg('created'))
return deployment
......@@ -306,11 +285,10 @@ class Deployment(models.Model):
data = DeploymentSerializer(self).data
msg = dumps(data)
if self.vo is not None:
RabbitMQInstance.load().publish_by_vo(
self.vo,
msg,
)
RabbitMQInstance.load().publish_by_vo(
self.vo,
msg,
)
# sends a state update via RabbitMQ / STOMP to the users webpage instance
def publish_to_user(self):
......@@ -340,24 +318,21 @@ class Deployment(models.Model):
self.save()
def __str__(self):
if self.vo is not None:
return 'VO-Dep: ({}:{})#{}'.format(
self.vo,
self.user,
self.id,
)
else:
return 'VO-Dep: (!NO-VO!:{})#{}'.format(
self.user,
self.id,
)
return 'VO-Dep: ({}:{})#{}'.format(
self.vo,
self.user,
self.id,
)
class DeploymentState(models.Model):
parentless = ValueError('Tried to access parent of parentless deployment state')
parent = models.ForeignKey(
Deployment,
related_name='state_items',
on_delete=models.CASCADE,
on_delete=models.SET_NULL,
null=True
)
user = models.ForeignKey(
......@@ -409,13 +384,14 @@ class DeploymentState(models.Model):
@property
def user_credentials(self):
if self.user is not None:
return self.user.credentials
return {}
return self.user.credentials
@property
def vo(self):
return self.parent.vo
if self.parent is not None:
return self.parent.vo
else:
raise self.parentless
@classmethod
def get_state_item(cls, parent=None, user=None, site=None, service=None):
......@@ -484,6 +460,10 @@ class DeploymentState(models.Model):
# user: deprovisioning requested
def user_remove(self):
if self.parent is None:
LOGGER.error('user_remove: parentless')
return
if (
self.parent.state_target == DEPLOYED
and (self.state == FAILED or self.state == REJECTED)
......@@ -514,6 +494,10 @@ class DeploymentState(models.Model):
# user: questionnaire answered
def user_answers(self, answers=None):
if self.parent is None:
LOGGER.error('user_answers: parentless')
return
self.questionnaire = answers
self._set_state(DEPLOYMENT_PENDING, publish=False)
self.parent.publish_to_client()
......@@ -553,7 +537,7 @@ class DeploymentState(models.Model):
self.save()
# the client completed a deployment after the user wished to remove the deployment
if self.parent.state_target == NOT_DEPLOYED:
if self.parent is not None and self.parent.state_target == NOT_DEPLOYED:
self.user_remove()
elif state == NOT_DEPLOYED:
......@@ -562,7 +546,7 @@ class DeploymentState(models.Model):
self.save()
# the client completed a removal after the user wished to deploy the deployment
if self.parent.state_target == DEPLOYED:
if self.parent is not None and self.parent.state_target == DEPLOYED:
self.user_deploy()
elif state == QUESTIONNAIRE:
......@@ -586,8 +570,8 @@ class DeploymentState(models.Model):
def _set_state(self, state, publish=True):
# assure all user credentials have a state
if self.parent.user is not None:
for key in self.parent.user.ssh_keys.all():
if self.user is not None:
for key in self.user.ssh_keys.all():
try:
CredentialState.get_credential_state(
credential=key,
......@@ -599,15 +583,22 @@ class DeploymentState(models.Model):
self.state = state
self.save()
LOGGER.debug(self.msg('State changed to '+self.state))
if publish:
if publish and self.parent is not None:
self.parent.publish_to_user()
def msg(self, msg):
return '{} - {}'.format(self, msg)
def __str__(self):
return 'DSItem: ({}:{}:{})#{}'.format(
self.parent.id,
if self.parent is not None:
return 'DState: ({}:{}:{})#{}'.format(
self.parent.id,
self.service,
self.site,
self.id,
)
return 'DState: (ORPHANED:{}:{})#{}'.format(
self.service,
self.site,
self.id,
......
......@@ -123,15 +123,13 @@ class DeploymentView(views.APIView):
deployment = None
if 'vo' in request.data:
deployment = models.Deployment.get_deployment(
request.user,
vo=VO.objects.get(id=request.data['vo']),
)
# elif 'service' in request.data:
# deployment = models.Deployment.get_deployment(
# request.user,
# service=models.Service.objects.get(id=request.data['service']),
# )
try:
deployment = models.Deployment.get_deployment(
request.user,
VO.objects.get(id=request.data['vo']),
)
except VO.DoesNotExist:
return _api_error_response("invalid value of field 'vo': does not exist")
if request_type == 'add':
deployment.user_deploy()
......
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