Revision ce55f211 snf-cyclades-app/synnefo/api/networks.py

b/snf-cyclades-app/synnefo/api/networks.py
1 1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2
# 
2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
5 5
# conditions are met:
6
# 
6
#
7 7
#   1. Redistributions of source code must retain the above
8 8
#      copyright notice, this list of conditions and the following
9 9
#      disclaimer.
10
# 
10
#
11 11
#   2. Redistributions in binary form must reproduce the above
12 12
#      copyright notice, this list of conditions and the following
13 13
#      disclaimer in the documentation and/or other materials
14 14
#      provided with the distribution.
15
# 
15
#
16 16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......
25 25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 27
# POSSIBILITY OF SUCH DAMAGE.
28
# 
28
#
29 29
# The views and conclusions contained in the software and
30 30
# documentation are those of the authors and should not be
31 31
# interpreted as representing official policies, either expressed
......
34 34
from logging import getLogger
35 35

  
36 36
from django.conf.urls.defaults import patterns
37
from django.conf import settings
37 38
from django.db.models import Q
38 39
from django.http import HttpResponse
39 40
from django.template.loader import render_to_string
40 41
from django.utils import simplejson as json
41 42

  
42
from synnefo.api import util
43
from synnefo.api import faults, util
43 44
from synnefo.api.actions import network_actions
44 45
from synnefo.api.common import method_not_allowed
45 46
from synnefo.api.faults import BadRequest, OverLimit, Unauthorized
46
from synnefo.db.models import Network
47
from synnefo.db.models import Network, NetworkLink
47 48
from synnefo.logic import backend
48 49

  
49 50

  
......
106 107
    #                       unauthorized (401),
107 108
    #                       badRequest (400),
108 109
    #                       overLimit (413)
109
    
110

  
110 111
    log.debug('list_networks detail=%s', detail)
111 112
    since = util.isoparse(request.GET.get('changes-since'))
112 113
    user_networks = Network.objects.filter(
113 114
                                Q(userid=request.user_uniq) | Q(public=True))
114
    
115

  
115 116
    if since:
116 117
        user_networks = user_networks.filter(updated__gte=since)
117 118
        if not user_networks:
118 119
            return HttpResponse(status=304)
119 120
    else:
120 121
        user_networks = user_networks.filter(state='ACTIVE')
121
    
122

  
122 123
    networks = [network_to_dict(network, request.user_uniq, detail)
123 124
                for network in user_networks]
124
    
125

  
125 126
    if request.serialization == 'xml':
126 127
        data = render_to_string('list_networks.xml', {
127 128
            'networks': networks,
......
144 145

  
145 146
    req = util.get_request_dict(request)
146 147
    log.debug('create_network %s', req)
147
    
148

  
148 149
    try:
149 150
        d = req['network']
150 151
        name = d['name']
151 152
    except (KeyError, ValueError):
152 153
        raise BadRequest('Malformed request.')
153
    
154
    network = backend.create_network(name, request.user_uniq)
155
    if not network:
156
        raise OverLimit('Network count limit exceeded for your account.')
157
    
154

  
155
    count = Network.objects.filter(userid=request.user_uniq,
156
                                          state='ACTIVE').count()
157

  
158
    # get user limit
159
    networks_limit_for_user = \
160
        settings.NETWORKS_USER_QUOTA.get(request.user_uniq,
161
                settings.MAX_NETWORKS_PER_USER)
162

  
163
    if count >= networks_limit_for_user:
164
        raise faults.OverLimit("Network count limit exceeded for your account.")
165

  
166
    try:
167
        network = backend.create_network(name, request.user_uniq)
168
    except NetworkLink.NotAvailable:
169
        raise faults.OverLimit('No networks available.')
170

  
158 171
    networkdict = network_to_dict(network, request.user_uniq)
159 172
    return render_network(request, networkdict, status=202)
160 173

  
......
168 181
    #                       badRequest (400),
169 182
    #                       itemNotFound (404),
170 183
    #                       overLimit (413)
171
    
184

  
172 185
    log.debug('get_network_details %s', network_id)
173 186
    net = util.get_network(network_id, request.user_uniq)
174 187
    netdict = network_to_dict(net, request.user_uniq)
......
188 201

  
189 202
    req = util.get_request_dict(request)
190 203
    log.debug('update_network_name %s', network_id)
191
    
204

  
192 205
    try:
193 206
        name = req['network']['name']
194 207
    except (TypeError, KeyError):
......
211 224
    #                       itemNotFound (404),
212 225
    #                       unauthorized (401),
213 226
    #                       overLimit (413)
214
    
227

  
215 228
    log.debug('delete_network %s', network_id)
216 229
    net = util.get_network(network_id, request.user_uniq)
217 230
    if net.public:
......
226 239
    log.debug('network_action %s %s', network_id, req)
227 240
    if len(req) != 1:
228 241
        raise BadRequest('Malformed request.')
229
    
242

  
230 243
    net = util.get_network(network_id, request.user_uniq)
231 244
    if net.public:
232 245
        raise Unauthorized('Can not modify the public network.')
233
    
246

  
234 247
    key = req.keys()[0]
235 248
    val = req[key]
236 249

  

Also available in: Unified diff