srikanth | 116e6e8 | 2014-08-19 07:22:37 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright (c) 2013 Big Switch Networks, Inc. |
| 3 | # |
| 4 | # Licensed under the Eclipse Public License, Version 1.0 (the |
| 5 | # "License"); you may not use this file except in compliance with the |
| 6 | # License. You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.eclipse.org/legal/epl-v10.html |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | # implied. See the License for the specific language governing |
| 14 | # permissions and limitations under the License. |
| 15 | # |
| 16 | |
| 17 | import datetime |
| 18 | from django.db import models |
| 19 | from django.contrib.auth.models import User |
| 20 | |
| 21 | # Create your models here. |
| 22 | class CustomerManager(models.Manager): |
| 23 | |
| 24 | def create_customer(self, name, email): |
| 25 | """ |
| 26 | Creates and saves a customer with the given name and email |
| 27 | """ |
| 28 | now = datetime.datetime.now() |
| 29 | |
| 30 | # Normalize the address by lowercasing the domain part of the email address |
| 31 | try: |
| 32 | email_name, domain_part = email.strip().split('@', 1) |
| 33 | except ValueError: |
| 34 | pass |
| 35 | else: |
| 36 | email = '@'.join([email_name, domain_part.lower()]) |
| 37 | |
| 38 | customer = self.model(customername=name, is_active=True, date_joined=now) |
| 39 | customer.save(using=self._db) |
| 40 | return customer |
| 41 | |
| 42 | class ClusterManager(models.Manager): |
| 43 | |
| 44 | def create_cluster(self, name, customer): |
| 45 | """ |
| 46 | Creates and saves a cluster with the given cluster name and the customer |
| 47 | """ |
| 48 | print "clusterManager create_cluster" |
| 49 | now = datetime.datetime.now() |
| 50 | |
| 51 | id = ":".join([customer.customername, name]) |
| 52 | cluster = self.model(id=id, clustername=name, is_active=True, date_joined=now) |
| 53 | cluster.save(using=self._db) |
| 54 | return cluster |
| 55 | |
| 56 | class CustomerUserManager(models.Manager): |
| 57 | |
| 58 | def create_customerUser(self, user, customer): |
| 59 | """ |
| 60 | Creates and saves a customer user membership |
| 61 | """ |
| 62 | id = ":".join([customer.customername, user.username]) |
| 63 | cu = self.model(id=id, user=user, customer=customer) |
| 64 | cu.save(using=self._db) |
| 65 | return cu |
| 66 | |
| 67 | class Customer(models.Model): |
| 68 | """ |
| 69 | Customer defines a customer in the cloud server. |
| 70 | """ |
| 71 | customername = models.CharField('customer name', |
| 72 | primary_key=True, |
| 73 | max_length=30, |
| 74 | help_text="30 characters or fewer. Letters, numbers and @/./+/-/_ characters") |
| 75 | email = models.EmailField('e-mail address', blank=True) |
| 76 | is_active = models.BooleanField('active', |
| 77 | default=True, |
| 78 | help_text="Designates whether this Customer should be treated as active. Unselect this instead of deleting accounts.") |
| 79 | date_joined = models.DateTimeField('date joined', default=datetime.datetime.now) |
| 80 | objects = CustomerManager() |
| 81 | |
| 82 | def __unicode__(self): |
| 83 | return self.customername |
| 84 | |
| 85 | class Cluster(models.Model): |
| 86 | """ |
| 87 | Cluster defines a cluster of nodes in the cloud server. |
| 88 | """ |
| 89 | id = models.CharField( |
| 90 | primary_key=True, |
| 91 | verbose_name='Cluster ID', |
| 92 | max_length=75, |
| 93 | help_text='Unique identifier for the cluster; format is customername:clustername') |
| 94 | clustername = models.CharField('cluster name', |
| 95 | max_length=30, |
| 96 | help_text="Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters") |
| 97 | is_active = models.BooleanField('active', |
| 98 | default=True, |
| 99 | help_text="Designates whether this cluster should be treated as active. Unselect this instead of deleting the cluster.") |
| 100 | date_joined = models.DateTimeField('date joined', |
| 101 | default=datetime.datetime.now) |
| 102 | |
| 103 | customer = models.ForeignKey(Customer, on_delete=models.CASCADE) |
| 104 | |
| 105 | objects = ClusterManager() |
| 106 | |
| 107 | def __unicode__(self): |
| 108 | return self.id |
| 109 | |
| 110 | |
| 111 | class CustomerUser(models.Model): |
| 112 | """ |
| 113 | This is a temporary model that captures the list of users in a given customer |
| 114 | """ |
| 115 | id = models.CharField( |
| 116 | primary_key=True, |
| 117 | verbose_name='Customer User', |
| 118 | max_length=75, |
| 119 | help_text='Unique relationship that shows the customer which the user belongs; \ |
| 120 | format is customername:username') |
| 121 | user = models.ForeignKey(User, on_delete=models.CASCADE) |
| 122 | customer = models.ForeignKey(Customer, on_delete=models.CASCADE) |
| 123 | |
| 124 | objects = CustomerUserManager() |
| 125 | |
| 126 | def __unicode__(self): |
| 127 | return self.id |
| 128 | |
| 129 | |
| 130 | class AuthToken(models.Model): |
| 131 | """ |
| 132 | Store the authentication token as an ascii string |
| 133 | Associate various credential options: user, cluster, customer. At least |
| 134 | one of these must be populated to be a valid entry. |
| 135 | """ |
| 136 | id = models.CharField( |
| 137 | primary_key=True, |
| 138 | max_length = 64) |
| 139 | |
| 140 | cluster = models.ForeignKey( |
| 141 | Cluster, |
| 142 | blank=True, |
| 143 | null=True) |
| 144 | |
| 145 | user = models.ForeignKey( |
| 146 | User, |
| 147 | blank=True, |
| 148 | null=True) |
| 149 | |
| 150 | customer = models.ForeignKey( |
| 151 | Customer, |
| 152 | blank=True, |
| 153 | null=True) |
| 154 | |
| 155 | expiration_date = models.DateTimeField( |
| 156 | verbose_name='Expiration Date', |
| 157 | help_text='Date when the authentication token expires', |
| 158 | blank=True, |
| 159 | null=True) |
| 160 | |
| 161 | annotation = models.CharField( |
| 162 | verbose_name='Annotation', |
| 163 | help_text='Track creation information such as user', |
| 164 | max_length=512, |
| 165 | blank=True, |
| 166 | null=True) |
| 167 | |
| 168 | def __unicode__(self): |
| 169 | return str(self.id) |
| 170 | |