models.py 3.35 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1
2
from django.contrib.auth.models import AbstractUser, Group
from django.db import models
3
4
5
6
from django.conf import settings
from django.dispatch import receiver
from rest_framework.authtoken.models import Token
from django.db.models.signals import post_save
Lukas Burgey's avatar
Lukas Burgey committed
7
from datetime import datetime
Lukas Burgey's avatar
Lukas Burgey committed
8
9
10


class User(AbstractUser):
11
12
13
14
15
16
17
18
19
20
    TYPE_CHOICES = (
            ('apiclient', 'API-Client'),
            ('oidcuser', 'OIDC User'),
            ('admin', 'Admin'),
            )
    user_type = models.CharField(
            max_length=20,
            choices=TYPE_CHOICES,
            default='oidcuser',
            )
Lukas Burgey's avatar
Lukas Burgey committed
21
22
23
24
    sub = models.CharField(max_length=150, blank=True, null=True)
    password = models.CharField(max_length=150, blank=True, null=True)


25
26
27
28
29
30
31
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
    if created:
        if instance.user_type == 'apiclient':
            Token.objects.create(user=instance)


Lukas Burgey's avatar
Lukas Burgey committed
32
33
34
35
36
37
38
39
40
41
42
43
def construct_user(user_info):
    return User(
            sub=user_info['sub'],
            name=user_info['name'],
            first_name=user_info['given_name'],
            last_name=user_info['family_name'],
            email=user_info['email'],
            username=user_info['email'],
            )


class Site(models.Model):
44
45
46
47
48
    client = models.OneToOneField(
            User,
            related_name='site',
            blank=True,
            null=True)
Lukas Burgey's avatar
Lukas Burgey committed
49
50
    name = models.CharField(max_length=150, unique=True)
    description = models.TextField(max_length=300, blank=True)
Lukas Burgey's avatar
Lukas Burgey committed
51
    last_fetch = models.DateTimeField(default=datetime.utcfromtimestamp(0))
Lukas Burgey's avatar
Lukas Burgey committed
52
53
54
55

    def __str__(self):
        return self.name

Lukas Burgey's avatar
Lukas Burgey committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    def clientapi_get_deployments(self):
        services = {}

        for service in self.services.all():
            # deployments with ssh keys
            services[service.name] = (
                    service.deployments
                    .filter(user__user_type='oidcuser')
                    .exclude(ssh_keys=None)
                    )
        self.last_fetch = datetime.now()
        self.save()
        return services

Lukas Burgey's avatar
Lukas Burgey committed
70
71
72
73
74

class Service(models.Model):
    name = models.CharField(max_length=150, unique=True)
    description = models.TextField(max_length=300, blank=True)
    site = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
75
76
            Site,
            related_name='services')
Lukas Burgey's avatar
Lukas Burgey committed
77
    groups = models.ManyToManyField(
Lukas Burgey's avatar
Lukas Burgey committed
78
79
80
            Group,
            related_name='services',
            blank=True)
Lukas Burgey's avatar
Lukas Burgey committed
81
82
83
84
85
86
87
88
89

    def __str__(self):
        return self.name + '@' + self.site.name


class SSHPublicKey(models.Model):
    name = models.CharField(max_length=150, unique=True)
    key = models.TextField(max_length=1000)
    user = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
90
91
            User,
            related_name='ssh_keys')
Lukas Burgey's avatar
Lukas Burgey committed
92
93
94
95
96

    def __str__(self):
        return self.name


Lukas Burgey's avatar
Lukas Burgey committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class Deployment(models.Model):
    user = models.ForeignKey(
            User,
            related_name='deployments',
            on_delete=models.CASCADE,
            )
    service = models.ForeignKey(
            Service,
            related_name='deployments',
            on_delete=models.CASCADE,
            )
    # SET_NULL: we allow credentials to be deleted after deployment
    ssh_keys = models.ManyToManyField(
            SSHPublicKey,
            related_name='deployments',
            blank=True,
            )

    def __str__(self):
        return str(self.user) + '@' + str(self.service)