Revision ce55f211 snf-cyclades-app/synnefo/db/models.py

b/snf-cyclades-app/synnefo/db/models.py
38 38
    disk_template = models.CharField('Disk template', max_length=32,
39 39
            default=settings.DEFAULT_GANETI_DISK_TEMPLATE)
40 40
    deleted = models.BooleanField('Deleted', default=False)
41
    
41

  
42 42
    class Meta:
43 43
        verbose_name = u'Virtual machine flavor'
44 44
        unique_together = ('cpu', 'ram', 'disk', 'disk_template')
45
    
45

  
46 46
    @property
47 47
    def name(self):
48 48
        """Returns flavor name (generated)"""
49 49
        return u'C%dR%dD%d' % (self.cpu, self.ram, self.disk)
50
    
50

  
51 51
    def __unicode__(self):
52 52
        return self.name
53 53

  
......
62 62
       ('REBOOT', 'Reboot VM'),
63 63
       ('DESTROY', 'Destroy VM')
64 64
    )
65
    
65

  
66 66
    # The internal operating state of a VM
67 67
    OPER_STATES = (
68 68
        ('BUILD', 'Queued for creation'),
......
71 71
        ('STARTED', 'Started'),
72 72
        ('DESTROYED', 'Destroyed')
73 73
    )
74
    
74

  
75 75
    # The list of possible operations on the backend
76 76
    BACKEND_OPCODES = (
77 77
        ('OP_INSTANCE_CREATE', 'Create Instance'),
......
93 93
        ('OP_INSTANCE_RECREATE_DISKS', 'Recreate Disks'),
94 94
        ('OP_INSTANCE_FAILOVER', 'Failover Instance')
95 95
    )
96
    
96

  
97 97
    # A backend job may be in one of the following possible states
98 98
    BACKEND_STATUSES = (
99 99
        ('queued', 'request queued'),
......
149 149
    suspended = models.BooleanField('Administratively Suspended',
150 150
                                    default=False)
151 151

  
152
    # VM State 
152
    # VM State
153 153
    # The following fields are volatile data, in the sense
154 154
    # that they need not be persistent in the DB, but rather
155 155
    # get generated at runtime by quering Ganeti and applying
156 156
    # updates received from Ganeti.
157
    
157

  
158 158
    # In the future they could be moved to a separate caching layer
159 159
    # and removed from the database.
160 160
    # [vkoukis] after discussion with [faidon].
......
188 188
            self._action = action
189 189
         def __str__(self):
190 190
            return repr(str(self._action))
191
    
191

  
192 192
    class DeletedError(Exception):
193 193
        pass
194
    
194

  
195 195
    class BuildingError(Exception):
196 196
        pass
197
    
197

  
198 198
    def __init__(self, *args, **kw):
199 199
        """Initialize state for just created VM instances."""
200 200
        super(VirtualMachine, self).__init__(*args, **kw)
201 201
        # This gets called BEFORE an instance gets save()d for
202 202
        # the first time.
203
        if not self.pk: 
203
        if not self.pk:
204 204
            self.action = None
205 205
            self.backendjobid = None
206 206
            self.backendjobstatus = None
207 207
            self.backendopcode = None
208 208
            self.backendlogmsg = None
209 209
            self.operstate = 'BUILD'
210
    
210

  
211 211
    @property
212 212
    def backend_id(self):
213 213
        """Returns the backend id for this VM by prepending backend-prefix."""
214 214
        if not self.id:
215 215
            raise VirtualMachine.InvalidBackendIdError("self.id is None")
216 216
        return '%s%s' % (settings.BACKEND_PREFIX_ID, self.id)
217
    
217

  
218 218
    class Meta:
219 219
        verbose_name = u'Virtual machine instance'
220 220
        get_latest_by = 'created'
221
    
221

  
222 222
    def __unicode__(self):
223 223
        return self.name
224 224

  
......
227 227
    meta_key = models.CharField(max_length=50)
228 228
    meta_value = models.CharField(max_length=500)
229 229
    vm = models.ForeignKey(VirtualMachine, related_name='metadata')
230
    
230

  
231 231
    class Meta:
232 232
        unique_together = (('meta_key', 'vm'),)
233 233
        verbose_name = u'Key-value pair of metadata for a VM.'
234
    
234

  
235 235
    def __unicode__(self):
236 236
        return u'%s: %s' % (self.meta_key, self.meta_value)
237 237

  
......
241 241
        ('ACTIVE', 'Active'),
242 242
        ('DELETED', 'Deleted')
243 243
    )
244
    
244

  
245 245
    name = models.CharField(max_length=255)
246 246
    created = models.DateTimeField(auto_now_add=True)
247 247
    updated = models.DateTimeField(auto_now=True)
......
252 252
    link = models.ForeignKey('NetworkLink', related_name='+')
253 253
    machines = models.ManyToManyField(VirtualMachine,
254 254
                                      through='NetworkInterface')
255
    
255

  
256 256
    def __unicode__(self):
257 257
        return self.name
258 258

  
......
263 263
        ('DISABLED', 'Disabled'),
264 264
        ('PROTECTED', 'Protected')
265 265
    )
266
    
266

  
267 267
    machine = models.ForeignKey(VirtualMachine, related_name='nics')
268 268
    network = models.ForeignKey(Network, related_name='nics')
269 269
    created = models.DateTimeField(auto_now_add=True)
......
274 274
    ipv6 = models.CharField(max_length=100, null=True)
275 275
    firewall_profile = models.CharField(choices=FIREWALL_PROFILES,
276 276
                                        max_length=30, null=True)
277
    
277

  
278 278
    def __unicode__(self):
279 279
        return '%s@%s' % (self.machine.name, self.network.name)
280 280

  
......
284 284
    index = models.IntegerField()
285 285
    name = models.CharField(max_length=255)
286 286
    available = models.BooleanField(default=True)
287
    
287

  
288 288
    def __unicode__(self):
289 289
        return self.name
290

  
291
    class NotAvailable(Exception):
292
        pass
293

  

Also available in: Unified diff