Revision 27816aae

b/db/models.py
348 348
         def __str__(self):
349 349
            return repr(str(self._action))
350 350

  
351
    class InvalidOperStateError(Exception):
352
        def __init__(self, value):
353
            self.value = value
354
        def __str__(self):
355
            return u'Invalid Operation State: %s (valid: STARTED, STOPPED, BUILD, ERROR, DESTROYED)' % ( self.value, )
356

  
357 351
    @staticmethod
358 352
    def id_from_instance_name(name):
359 353
        """Returns VirtualMachine's Django id, given a ganeti machine name.
......
374 368
        # Before this instance gets save()d
375 369
        if not self.pk: 
376 370
            self._action = None
377

  
378
            # [bkarak] update state change
379
            # self._operstate = "BUILD"
380
            self.update_state('BUILD')
381

  
371
            self._update_state('BUILD')
382 372
            self._backendjobid = None
383 373
            self._backendjobstatus = None
384 374
            self._backendopcode = None
......
403 393

  
404 394
        # Notifications of success change the operating state
405 395
        if status == 'success':
406
            # [bkarak] update state change
407
            #self._operstate = VirtualMachine.OPER_STATE_FROM_OPCODE[opcode]
408
            self.update_state(VirtualMachine.OPER_STATE_FROM_OPCODE[opcode])
396
            self._update_state(VirtualMachine.OPER_STATE_FROM_OPCODE[opcode])
409 397
        # Special cases OP_INSTANCE_CREATE fails --> ERROR
410 398
        if status in ('canceled', 'error') and opcode == 'OP_INSTANCE_CREATE':
411
            # [bkarak] update state change
412
            #self._operstate = 'ERROR'
413
            self.update_state('ERROR')
399
            self._update_state('ERROR')
414 400
        # Any other notification of failure leaves the operating state unchanged
415 401

  
416 402
        # [bkarak] update_state saves the object
......
460 446
    def __unicode__(self):
461 447
        return self.name
462 448

  
463
    def update_state(self, new_operstate):
464
        """Update the VM current state (never update the _operstate manually)"""
449
    def _update_state(self, new_operstate):
450
        """Wrapper around updates of the _operstate field
451

  
452
        Currently calls the charge() method when necessary.
453

  
454
        """
465 455

  
466 456
        # Check if the state is valid, need to do this more elegant
467 457
        valid = False
......
478 468
            self._operstate = new_operstate
479 469
            # FIXME: this should be save (?)
480 470
            self.save()
481
        else:
482
            raise VirtualMachine.InvalidOperStateError(new_operstate)
483 471

  
484 472
    def charge(self):
485 473
        """Charges the VM owner
......
492 480

  
493 481
        if self._operstate == 'STARTED':
494 482
            cost_list = self.flavor.get_cost_active(self.charged, cur_datetime)
495
        else:
483
        elif self._operstate == 'STOPPED':
496 484
            cost_list = self.flavor.get_cost_inactive(self.charged, cur_datetime)
485
        else:
486
            return
497 487

  
498 488
        total_cost = 0
499 489

  

Also available in: Unified diff