Revision e221ade2 snf-cyclades-app/synnefo/api/util.py

b/snf-cyclades-app/synnefo/api/util.py
157 157
    return b64encode(enc)
158 158

  
159 159

  
160
def get_vm(server_id, user_id):
161
    """Return a VirtualMachine instance or raise ItemNotFound."""
160
def get_vm(server_id, user_id, non_deleted=False, non_suspended=False):
161
    """Find a VirtualMachine instance based on ID and owner."""
162 162

  
163 163
    try:
164 164
        server_id = int(server_id)
165
        return VirtualMachine.objects.get(id=server_id, userid=user_id)
165
        vm = VirtualMachine.objects.get(id=server_id, userid=user_id)
166
        if non_deleted and vm.deleted:
167
            raise VirtualMachine.DeletedError
168
        if non_suspended and vm.suspended:
169
            raise Unauthorized("Administratively Suspended VM")
166 170
    except ValueError:
167 171
        raise BadRequest('Invalid server ID.')
168 172
    except VirtualMachine.DoesNotExist:
169 173
        raise ItemNotFound('Server not found.')
170 174

  
171

  
172 175
def get_vm_meta(vm, key):
173 176
    """Return a VirtualMachineMetadata instance or raise ItemNotFound."""
174 177

  

Also available in: Unified diff