Revision e2ee7808 api/actions.py

b/api/actions.py
10 10
from django.utils import simplejson as json
11 11

  
12 12
from synnefo.api.faults import BadRequest, ServiceUnavailable
13
from synnefo.api.util import random_password
13
from synnefo.api.util import random_password, get_vm
14 14
from synnefo.util.vapclient import request_forwarding as request_vnc_forwarding
15 15
from synnefo.logic.backend import (reboot_instance, startup_instance, shutdown_instance,
16 16
                                    get_instance_console)
......
18 18

  
19 19

  
20 20
server_actions = {}
21
network_actions = {}
21 22

  
22 23

  
23 24
def server_action(name):
24 25
    '''Decorator for functions implementing server actions.
25
    
26 26
    `name` is the key in the dict passed by the client.
27 27
    '''
28 28
    
......
31 31
        return func
32 32
    return decorator
33 33

  
34
def network_action(name):
35
    '''Decorator for functions implementing network actions.
36
    `name` is the key in the dict passed by the client.
37
    '''
38

  
39
    def decorator(func):
40
        network_actions[name] = func
41
        return func
42
    return decorator
43

  
34 44

  
35 45
@server_action('changePassword')
36 46
def change_password(request, vm, args):
......
224 234
        data = json.dumps({'console': console})
225 235
    
226 236
    return HttpResponse(data, mimetype=mimetype, status=200)
237

  
238

  
239
@network_action('add')
240
def add(request, net, args):
241
    server_id = args.get('server', None)
242
    if not server_id:
243
        raise BadRequest('Malformed Request.')
244
    vm = get_vm(server_id, request.user)
245
    net.machines.add(vm)
246
    net.save()
247
    return HttpResponse(status=202)
248

  
249
@network_action('remove')
250
def remove(request, net, args):
251
    server_id = args.get('server', None)
252
    if not server_id:
253
        raise BadRequest('Malformed Request.')
254
    vm = get_vm(server_id, request.user)
255
    net.machines.remove(vm)
256
    net.save()
257
    return HttpResponse(status=202)

Also available in: Unified diff