Commit e7a3b0f4 authored by Lukas Burgey's avatar Lukas Burgey

Change some properties

parent 036b2b6c
......@@ -9,7 +9,9 @@ from .. import models, serializers as backend_serializers
class ServiceSerializer(serializers.ModelSerializer):
class Meta:
model = models.Service
fields = ['name']
fields = [
'name',
]
class UserSerializer(serializers.ModelSerializer):
......@@ -49,7 +51,6 @@ class DeploymentStateSerializer(serializers.Serializer):
id = serializers.IntegerField()
state_target = serializers.CharField()
user = UserSerializer()
service = ServiceSerializer()
key = backend_serializers.SSHPublicKeySerializer()
......
......@@ -22,15 +22,18 @@ class ServiceSerializer(serializers.ModelSerializer):
'name',
'site',
'groups',
'description',
]
class DeploymentStateItemSerializer(serializers.ModelSerializer):
service = ServiceSerializer()
services = ServiceSerializer(many=True)
key = backend_serializers.SSHPublicKeyRefSerializer()
site = SiteSerializer()
questionnaire = serializers.JSONField()
credentials = serializers.JSONField()
group = backend_serializers.GroupSerializer()
class Meta:
model = models.DeploymentStateItem
......@@ -42,6 +45,8 @@ class DeploymentStateItemSerializer(serializers.ModelSerializer):
'credentials',
'key',
'service',
'services',
'group',
]
......@@ -62,6 +67,7 @@ class DeploymentStateSerializer(serializers.ModelSerializer):
class DeploymentSerializer(serializers.ModelSerializer):
sites = SiteSerializer(many=True)
service = ServiceSerializer()
services = ServiceSerializer(many=True)
ssh_keys = backend_serializers.SSHPublicKeyRefSerializer(many=True)
......@@ -71,6 +77,7 @@ class DeploymentSerializer(serializers.ModelSerializer):
class Meta:
model = models.Deployment
fields = [
'sites',
'service',
'services',
'group',
......@@ -89,7 +96,7 @@ class UserSerializer(serializers.ModelSerializer):
class Meta:
model = models.User
fields = [
'email',
'profile_name',
'groups',
'id',
'ssh_keys',
......
......@@ -271,6 +271,15 @@ class User(AbstractUser):
editable=False,
)
@property
def profile_name(self):
if 'email' in self.userinfo:
return self.userinfo['email']
elif 'name':
return self.userinfo['name']
else:
return self.id
@property
def deployment_states(self):
states = []
......@@ -317,13 +326,11 @@ class User(AbstractUser):
if 'sub' not in userinfo:
raise Exception('Missing attribute in userinfo: sub')
sub = userinfo['sub']
username = sub
email = ''
if 'email' not in userinfo:
username = sub
email = ''
else:
if 'email' in userinfo:
username = userinfo['email']
email = userinfo['email']
user = cls(
user_type='oidcuser',
......@@ -421,6 +428,11 @@ class User(AbstractUser):
def update_userinfo(self, userinfo):
self.userinfo = userinfo
self.save()
if 'sub' not in userinfo:
raise Exception('Missing attribute in userinfo: sub')
sub = userinfo['sub']
groups = userinfo.get('groups', [])
# FIXME probably inefficient
self.groups.clear()
......@@ -459,7 +471,6 @@ class User(AbstractUser):
key.save()
class Site(models.Model):
client = models.OneToOneField(
User,
......@@ -581,13 +592,21 @@ class SSHPublicKey(models.Model):
return '[SSHKey:{}] {}'.format(self, msg)
# Deployment describes the credential state per user (and site) as it is supposed to be
# Deployment describes the supposed state of the users ssh keys at either:
# - a group (and and the services associated with the group)
# - a single service
#
# (exception: if is_active=False the ssh_keys contain the keys to be deployed
# if the deployment is reactivated)
# DeploymentState track the state of a single ssh key at either:
# - a group (and and the services associated with the group)
# - a single service
# DeploymentStateItem track the acknowledgements from the clients for either :
# - the sites that handle the associated group (i.e provide a service for members of the group)
# - the sites that provide the associated service
#
# DeploymentState is what is sent to the clients via rabbitmq
# The DeploymentStateItem track the acknowledgements from the clients
# Note: two possible kinds of Deployment:
# (group is None and service is not None) or
# (group is not None and service is None)
class Deployment(models.Model):
user = models.ForeignKey(
User,
......@@ -602,12 +621,6 @@ class Deployment(models.Model):
null=True,
blank=True,
)
# only used when group is set and service is not set
@property
def services(self):
if self.group is not None:
return self.group.services.all()
return None
service = models.ForeignKey(
Service,
......@@ -625,6 +638,18 @@ class Deployment(models.Model):
default=True,
)
# only used when group is not None and service is None
@property
def services(self):
if self.group is not None:
return self.group.services.all()
return None
# only used when group is not None and service is None
@property
def sites(self):
return Site.objects.filter(services__groups=self.group).distinct()
# get a deployment for a user/service.
# if it does not exist it is created
@classmethod
......@@ -774,6 +799,10 @@ class DeploymentState(models.Model):
def service(self):
return self.deployment.service
@property
def services(self):
return self.deployment.services
@property
def group(self):
return self.deployment.group
......@@ -811,13 +840,13 @@ class DeploymentState(models.Model):
)
deploy.save()
elif deployment.group is not None:
for service in deployment.group.services.all():
for site in service.site.all():
deploy = DeploymentStateItem(
parent=state,
site=site,
)
deploy.save()
# every site which provides a service for group
for site in deployment.sites:
deploy = DeploymentStateItem(
parent=state,
site=site,
)
deploy.save()
return state
......@@ -914,13 +943,21 @@ class DeploymentStateItem(models.Model):
def user(self):
return self.parent.user
@property
def key(self):
return self.parent.key
@property
def service(self):
return self.parent.service
@property
def key(self):
return self.parent.key
def group(self):
return self.parent.group
@property
def services(self):
return self.parent.services.filter(groups=self.group)
# STATE transitions
# user: deployment requested
......
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