Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / networks.py @ e57893cd

History | View | Annotate | Download (12.4 kB)

1 6ef51e9f Giorgos Verigakis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 ce55f211 Kostas Papadimitriou
#
3 adee02b8 Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 adee02b8 Giorgos Verigakis
# without modification, are permitted provided that the following
5 adee02b8 Giorgos Verigakis
# conditions are met:
6 ce55f211 Kostas Papadimitriou
#
7 adee02b8 Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 adee02b8 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 adee02b8 Giorgos Verigakis
#      disclaimer.
10 ce55f211 Kostas Papadimitriou
#
11 adee02b8 Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 adee02b8 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 adee02b8 Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 adee02b8 Giorgos Verigakis
#      provided with the distribution.
15 ce55f211 Kostas Papadimitriou
#
16 adee02b8 Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 adee02b8 Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 adee02b8 Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 adee02b8 Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 adee02b8 Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 adee02b8 Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 adee02b8 Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 adee02b8 Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 adee02b8 Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 adee02b8 Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 adee02b8 Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 adee02b8 Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 ce55f211 Kostas Papadimitriou
#
29 adee02b8 Giorgos Verigakis
# The views and conclusions contained in the software and
30 adee02b8 Giorgos Verigakis
# documentation are those of the authors and should not be
31 adee02b8 Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 adee02b8 Giorgos Verigakis
# or implied, of GRNET S.A.
33 adee02b8 Giorgos Verigakis
34 2035039b Giorgos Verigakis
35 ce55f211 Kostas Papadimitriou
from django.conf import settings
36 b3fd98ae Christos Stavrakakis
from django.conf.urls.defaults import patterns
37 4dba0480 Christos Stavrakakis
from django.db import transaction
38 b3fd98ae Christos Stavrakakis
from django.db.models import Q
39 0269afd6 Giorgos Verigakis
from django.http import HttpResponse
40 69d6d7dd Giorgos Verigakis
from django.template.loader import render_to_string
41 0269afd6 Giorgos Verigakis
from django.utils import simplejson as json
42 0269afd6 Giorgos Verigakis
43 b3fd98ae Christos Stavrakakis
from snf_django.lib import api
44 b3fd98ae Christos Stavrakakis
from snf_django.lib.api import faults, utils
45 5061546f Christos Stavrakakis
from synnefo.api import util
46 41a7fae7 Christos Stavrakakis
from synnefo.api.servers import network_actions
47 77f45438 Christos Stavrakakis
from synnefo import quotas
48 03992c72 Christos Stavrakakis
from synnefo.db.models import Network
49 99af08a4 Christos Stavrakakis
from synnefo.db.utils import validate_mac
50 03992c72 Christos Stavrakakis
from synnefo.db.pools import EmptyPool
51 b36f78fa Giorgos Verigakis
from synnefo.logic import backend
52 9e98ba3c Giorgos Verigakis
53 9e98ba3c Giorgos Verigakis
54 b3fd98ae Christos Stavrakakis
from logging import getLogger
55 b3fd98ae Christos Stavrakakis
log = getLogger(__name__)
56 b36f78fa Giorgos Verigakis
57 e440e835 Christos Stavrakakis
urlpatterns = patterns(
58 e440e835 Christos Stavrakakis
    'synnefo.api.networks',
59 0269afd6 Giorgos Verigakis
    (r'^(?:/|.json|.xml)?$', 'demux'),
60 0269afd6 Giorgos Verigakis
    (r'^/detail(?:.json|.xml)?$', 'list_networks', {'detail': True}),
61 f4fe8796 Giorgos Verigakis
    (r'^/(\w+)(?:.json|.xml)?$', 'network_demux'),
62 f4fe8796 Giorgos Verigakis
    (r'^/(\w+)/action(?:.json|.xml)?$', 'network_action'),
63 0269afd6 Giorgos Verigakis
)
64 0269afd6 Giorgos Verigakis
65 0269afd6 Giorgos Verigakis
66 0269afd6 Giorgos Verigakis
def demux(request):
67 0269afd6 Giorgos Verigakis
    if request.method == 'GET':
68 0269afd6 Giorgos Verigakis
        return list_networks(request)
69 0269afd6 Giorgos Verigakis
    elif request.method == 'POST':
70 0269afd6 Giorgos Verigakis
        return create_network(request)
71 0269afd6 Giorgos Verigakis
    else:
72 c0720b78 Kostas Papadimitriou
        return api.api_method_not_allowed(request)
73 0269afd6 Giorgos Verigakis
74 6ef51e9f Giorgos Verigakis
75 b19653d1 Giorgos Verigakis
def network_demux(request, network_id):
76 0269afd6 Giorgos Verigakis
    if request.method == 'GET':
77 b19653d1 Giorgos Verigakis
        return get_network_details(request, network_id)
78 0269afd6 Giorgos Verigakis
    elif request.method == 'PUT':
79 b19653d1 Giorgos Verigakis
        return update_network_name(request, network_id)
80 0269afd6 Giorgos Verigakis
    elif request.method == 'DELETE':
81 b19653d1 Giorgos Verigakis
        return delete_network(request, network_id)
82 0269afd6 Giorgos Verigakis
    else:
83 c0720b78 Kostas Papadimitriou
        return api.api_method_not_allowed(request)
84 0269afd6 Giorgos Verigakis
85 0269afd6 Giorgos Verigakis
86 6ef51e9f Giorgos Verigakis
def network_to_dict(network, user_id, detail=True):
87 7fede91e Christos Stavrakakis
    d = {'id': str(network.id), 'name': network.name}
88 1b696c26 Christos Stavrakakis
    d['links'] = util.network_to_links(network.id)
89 0269afd6 Giorgos Verigakis
    if detail:
90 936e0be3 Christos Stavrakakis
        d['user_id'] = network.userid
91 936e0be3 Christos Stavrakakis
        d['tenant_id'] = network.userid
92 08b079e2 Stavros Sachtouris
        d['cidr'] = network.subnet
93 cc3f266e Christos Stavrakakis
        d['cidr6'] = network.subnet6
94 08b079e2 Stavros Sachtouris
        d['gateway'] = network.gateway
95 cc3f266e Christos Stavrakakis
        d['gateway6'] = network.gateway6
96 08b079e2 Stavros Sachtouris
        d['dhcp'] = network.dhcp
97 b7d38981 Dimitris Aragiorgis
        d['type'] = network.flavor
98 b3fd98ae Christos Stavrakakis
        d['updated'] = utils.isoformat(network.updated)
99 b3fd98ae Christos Stavrakakis
        d['created'] = utils.isoformat(network.created)
100 d44c236b Giorgos Verigakis
        d['status'] = network.state
101 7fede91e Christos Stavrakakis
        d['public'] = network.public
102 08b079e2 Stavros Sachtouris
103 7fede91e Christos Stavrakakis
        attachments = [util.construct_nic_id(nic)
104 e440e835 Christos Stavrakakis
                       for nic in network.nics.filter(machine__userid=user_id)
105 939d71dd Christos Stavrakakis
                                              .filter(state="ACTIVE")
106 cd2b0bf5 Christos Stavrakakis
                                              .order_by('machine')]
107 5029ff36 Christos Stavrakakis
        d['attachments'] = attachments
108 0269afd6 Giorgos Verigakis
    return d
109 0269afd6 Giorgos Verigakis
110 6ef51e9f Giorgos Verigakis
111 0269afd6 Giorgos Verigakis
def render_network(request, networkdict, status=200):
112 0269afd6 Giorgos Verigakis
    if request.serialization == 'xml':
113 0269afd6 Giorgos Verigakis
        data = render_to_string('network.xml', {'network': networkdict})
114 0269afd6 Giorgos Verigakis
    else:
115 0269afd6 Giorgos Verigakis
        data = json.dumps({'network': networkdict})
116 0269afd6 Giorgos Verigakis
    return HttpResponse(data, status=status)
117 0269afd6 Giorgos Verigakis
118 0269afd6 Giorgos Verigakis
119 b3fd98ae Christos Stavrakakis
@api.api_method(http_method='GET', user_required=True, logger=log)
120 0269afd6 Giorgos Verigakis
def list_networks(request, detail=False):
121 0269afd6 Giorgos Verigakis
    # Normal Response Codes: 200, 203
122 0269afd6 Giorgos Verigakis
    # Error Response Codes: computeFault (400, 500),
123 0269afd6 Giorgos Verigakis
    #                       serviceUnavailable (503),
124 0269afd6 Giorgos Verigakis
    #                       unauthorized (401),
125 0269afd6 Giorgos Verigakis
    #                       badRequest (400),
126 0269afd6 Giorgos Verigakis
    #                       overLimit (413)
127 ce55f211 Kostas Papadimitriou
128 0c37a721 Christos Stavrakakis
    log.debug('list_networks detail=%s', detail)
129 b3fd98ae Christos Stavrakakis
    since = utils.isoparse(request.GET.get('changes-since'))
130 8b2515a9 Christos Stavrakakis
    user_networks = Network.objects.filter(Q(userid=request.user_uniq) |
131 3878553d Kostas Papadimitriou
                                           Q(public=True))
132 ce55f211 Kostas Papadimitriou
133 0269afd6 Giorgos Verigakis
    if since:
134 d44c236b Giorgos Verigakis
        user_networks = user_networks.filter(updated__gte=since)
135 0269afd6 Giorgos Verigakis
        if not user_networks:
136 0269afd6 Giorgos Verigakis
            return HttpResponse(status=304)
137 3878553d Kostas Papadimitriou
    else:
138 3878553d Kostas Papadimitriou
        user_networks = user_networks.filter(deleted=False)
139 ce55f211 Kostas Papadimitriou
140 4b3b8688 Giorgos Verigakis
    networks = [network_to_dict(network, request.user_uniq, detail)
141 cd2b0bf5 Christos Stavrakakis
                for network in user_networks.order_by('id')]
142 ce55f211 Kostas Papadimitriou
143 0269afd6 Giorgos Verigakis
    if request.serialization == 'xml':
144 b36f78fa Giorgos Verigakis
        data = render_to_string('list_networks.xml', {
145 b36f78fa Giorgos Verigakis
            'networks': networks,
146 b36f78fa Giorgos Verigakis
            'detail': detail})
147 0269afd6 Giorgos Verigakis
    else:
148 5029ff36 Christos Stavrakakis
        data = json.dumps({'networks': networks})
149 aa197ee4 Vangelis Koukis
150 0269afd6 Giorgos Verigakis
    return HttpResponse(data, status=200)
151 0269afd6 Giorgos Verigakis
152 6ef51e9f Giorgos Verigakis
153 b3fd98ae Christos Stavrakakis
@api.api_method(http_method='POST', user_required=True, logger=log)
154 d4993f77 Christos Stavrakakis
@transaction.commit_manually
155 2509ce17 Christos Stavrakakis
def create_network(request):
156 0269afd6 Giorgos Verigakis
    # Normal Response Code: 202
157 0269afd6 Giorgos Verigakis
    # Error Response Codes: computeFault (400, 500),
158 0269afd6 Giorgos Verigakis
    #                       serviceUnavailable (503),
159 0269afd6 Giorgos Verigakis
    #                       unauthorized (401),
160 13b954b0 Giorgos Verigakis
    #                       badMediaType(415),
161 0269afd6 Giorgos Verigakis
    #                       badRequest (400),
162 17c2ed57 Christos Stavrakakis
    #                       forbidden (403)
163 0269afd6 Giorgos Verigakis
    #                       overLimit (413)
164 aa197ee4 Vangelis Koukis
165 3165f027 Christos Stavrakakis
    try:
166 b3fd98ae Christos Stavrakakis
        req = utils.get_request_dict(request)
167 d4993f77 Christos Stavrakakis
        log.info('create_network %s', req)
168 d4993f77 Christos Stavrakakis
169 4e1133ab Christos Stavrakakis
        user_id = request.user_uniq
170 d4993f77 Christos Stavrakakis
        try:
171 d4993f77 Christos Stavrakakis
            d = req['network']
172 d4993f77 Christos Stavrakakis
            name = d['name']
173 4e1133ab Christos Stavrakakis
        except KeyError:
174 bd40abfa Christos Stavrakakis
            raise faults.BadRequest("Malformed request")
175 4e1133ab Christos Stavrakakis
176 4e1133ab Christos Stavrakakis
        # Get and validate flavor. Flavors are still exposed as 'type' in the
177 4e1133ab Christos Stavrakakis
        # API.
178 4e1133ab Christos Stavrakakis
        flavor = d.get("type", None)
179 4e1133ab Christos Stavrakakis
        if flavor is None:
180 bd40abfa Christos Stavrakakis
            raise faults.BadRequest("Missing request parameter 'type'")
181 4e1133ab Christos Stavrakakis
        elif flavor not in Network.FLAVORS.keys():
182 bd40abfa Christos Stavrakakis
            raise faults.BadRequest("Invalid network type '%s'" % flavor)
183 4e1133ab Christos Stavrakakis
        elif flavor not in settings.API_ENABLED_NETWORK_FLAVORS:
184 99af08a4 Christos Stavrakakis
            raise faults.Forbidden("Can not create network of type '%s'" %
185 99af08a4 Christos Stavrakakis
                                   flavor)
186 4e1133ab Christos Stavrakakis
187 4e1133ab Christos Stavrakakis
        public = d.get("public", False)
188 d4993f77 Christos Stavrakakis
        if public:
189 bd40abfa Christos Stavrakakis
            raise faults.Forbidden("Can not create a public network.")
190 d4993f77 Christos Stavrakakis
191 4e1133ab Christos Stavrakakis
        dhcp = d.get('dhcp', True)
192 d4993f77 Christos Stavrakakis
193 4e1133ab Christos Stavrakakis
        # Get and validate network parameters
194 4e1133ab Christos Stavrakakis
        subnet = d.get('cidr', '192.168.1.0/24')
195 4e1133ab Christos Stavrakakis
        subnet6 = d.get('cidr6', None)
196 4e1133ab Christos Stavrakakis
        gateway = d.get('gateway', None)
197 4e1133ab Christos Stavrakakis
        gateway6 = d.get('gateway6', None)
198 d368ec0c Christos Stavrakakis
        # Check that user provided a valid subnet
199 9ae613af Christos Stavrakakis
        util.validate_network_params(subnet, gateway, subnet6, gateway6)
200 d4993f77 Christos Stavrakakis
201 d4993f77 Christos Stavrakakis
        try:
202 d4993f77 Christos Stavrakakis
            mode, link, mac_prefix, tags = util.values_from_flavor(flavor)
203 99af08a4 Christos Stavrakakis
            validate_mac(mac_prefix + "0:00:00:00")
204 d4993f77 Christos Stavrakakis
            network = Network.objects.create(
205 e440e835 Christos Stavrakakis
                name=name,
206 e440e835 Christos Stavrakakis
                userid=user_id,
207 e440e835 Christos Stavrakakis
                subnet=subnet,
208 e440e835 Christos Stavrakakis
                subnet6=subnet6,
209 e440e835 Christos Stavrakakis
                gateway=gateway,
210 e440e835 Christos Stavrakakis
                gateway6=gateway6,
211 e440e835 Christos Stavrakakis
                dhcp=dhcp,
212 e440e835 Christos Stavrakakis
                flavor=flavor,
213 e440e835 Christos Stavrakakis
                mode=mode,
214 e440e835 Christos Stavrakakis
                link=link,
215 e440e835 Christos Stavrakakis
                mac_prefix=mac_prefix,
216 e440e835 Christos Stavrakakis
                tags=tags,
217 e440e835 Christos Stavrakakis
                action='CREATE',
218 99af08a4 Christos Stavrakakis
                state='ACTIVE')
219 d4993f77 Christos Stavrakakis
        except EmptyPool:
220 d4993f77 Christos Stavrakakis
            log.error("Failed to allocate resources for network of type: %s",
221 d4993f77 Christos Stavrakakis
                      flavor)
222 99af08a4 Christos Stavrakakis
            raise faults.ServiceUnavailable("Failed to allocate network"
223 99af08a4 Christos Stavrakakis
                                            " resources")
224 d4993f77 Christos Stavrakakis
225 2509ce17 Christos Stavrakakis
        # Issue commission to Quotaholder and accept it since at the end of
226 2509ce17 Christos Stavrakakis
        # this transaction the Network object will be created in the DB.
227 2509ce17 Christos Stavrakakis
        # Note: the following call does a commit!
228 2509ce17 Christos Stavrakakis
        quotas.issue_and_accept_commission(network)
229 d4993f77 Christos Stavrakakis
    except:
230 d4993f77 Christos Stavrakakis
        transaction.rollback()
231 e7a8d451 Christos Stavrakakis
        raise
232 d4993f77 Christos Stavrakakis
    else:
233 d4993f77 Christos Stavrakakis
        transaction.commit()
234 7cfbbf32 Christos Stavrakakis
235 4b3b8688 Giorgos Verigakis
    networkdict = network_to_dict(network, request.user_uniq)
236 77f45438 Christos Stavrakakis
    response = render_network(request, networkdict, status=202)
237 77f45438 Christos Stavrakakis
238 77f45438 Christos Stavrakakis
    return response
239 0269afd6 Giorgos Verigakis
240 6ef51e9f Giorgos Verigakis
241 b3fd98ae Christos Stavrakakis
@api.api_method(http_method='GET', user_required=True, logger=log)
242 b19653d1 Giorgos Verigakis
def get_network_details(request, network_id):
243 0269afd6 Giorgos Verigakis
    # Normal Response Codes: 200, 203
244 13b954b0 Giorgos Verigakis
    # Error Response Codes: computeFault (400, 500),
245 13b954b0 Giorgos Verigakis
    #                       serviceUnavailable (503),
246 13b954b0 Giorgos Verigakis
    #                       unauthorized (401),
247 13b954b0 Giorgos Verigakis
    #                       badRequest (400),
248 13b954b0 Giorgos Verigakis
    #                       itemNotFound (404),
249 13b954b0 Giorgos Verigakis
    #                       overLimit (413)
250 ce55f211 Kostas Papadimitriou
251 0c37a721 Christos Stavrakakis
    log.debug('get_network_details %s', network_id)
252 4b3b8688 Giorgos Verigakis
    net = util.get_network(network_id, request.user_uniq)
253 4b3b8688 Giorgos Verigakis
    netdict = network_to_dict(net, request.user_uniq)
254 0269afd6 Giorgos Verigakis
    return render_network(request, netdict)
255 0269afd6 Giorgos Verigakis
256 6ef51e9f Giorgos Verigakis
257 b3fd98ae Christos Stavrakakis
@api.api_method(http_method='PUT', user_required=True, logger=log)
258 b19653d1 Giorgos Verigakis
def update_network_name(request, network_id):
259 0269afd6 Giorgos Verigakis
    # Normal Response Code: 204
260 13b954b0 Giorgos Verigakis
    # Error Response Codes: computeFault (400, 500),
261 13b954b0 Giorgos Verigakis
    #                       serviceUnavailable (503),
262 13b954b0 Giorgos Verigakis
    #                       unauthorized (401),
263 13b954b0 Giorgos Verigakis
    #                       badRequest (400),
264 17c2ed57 Christos Stavrakakis
    #                       forbidden (403)
265 13b954b0 Giorgos Verigakis
    #                       badMediaType(415),
266 13b954b0 Giorgos Verigakis
    #                       itemNotFound (404),
267 13b954b0 Giorgos Verigakis
    #                       overLimit (413)
268 aa197ee4 Vangelis Koukis
269 b3fd98ae Christos Stavrakakis
    req = utils.get_request_dict(request)
270 bf5c82dc Christos Stavrakakis
    log.info('update_network_name %s', network_id)
271 ce55f211 Kostas Papadimitriou
272 0269afd6 Giorgos Verigakis
    try:
273 0269afd6 Giorgos Verigakis
        name = req['network']['name']
274 0269afd6 Giorgos Verigakis
    except (TypeError, KeyError):
275 bd40abfa Christos Stavrakakis
        raise faults.BadRequest('Malformed request.')
276 0269afd6 Giorgos Verigakis
277 4b3b8688 Giorgos Verigakis
    net = util.get_network(network_id, request.user_uniq)
278 207b70d5 Giorgos Verigakis
    if net.public:
279 bd40abfa Christos Stavrakakis
        raise faults.Forbidden('Can not rename the public network.')
280 22a9959d Christos Stavrakakis
    if net.deleted:
281 05d401cf Christos Stavrakakis
        raise faults.BadRequest("Network has been deleted.")
282 0269afd6 Giorgos Verigakis
    net.name = name
283 0269afd6 Giorgos Verigakis
    net.save()
284 0269afd6 Giorgos Verigakis
    return HttpResponse(status=204)
285 0269afd6 Giorgos Verigakis
286 6ef51e9f Giorgos Verigakis
287 b3fd98ae Christos Stavrakakis
@api.api_method(http_method='DELETE', user_required=True, logger=log)
288 4dba0480 Christos Stavrakakis
@transaction.commit_on_success
289 b19653d1 Giorgos Verigakis
def delete_network(request, network_id):
290 0269afd6 Giorgos Verigakis
    # Normal Response Code: 204
291 13b954b0 Giorgos Verigakis
    # Error Response Codes: computeFault (400, 500),
292 13b954b0 Giorgos Verigakis
    #                       serviceUnavailable (503),
293 13b954b0 Giorgos Verigakis
    #                       unauthorized (401),
294 17c2ed57 Christos Stavrakakis
    #                       forbidden (403)
295 13b954b0 Giorgos Verigakis
    #                       itemNotFound (404),
296 13b954b0 Giorgos Verigakis
    #                       overLimit (413)
297 ce55f211 Kostas Papadimitriou
298 bf5c82dc Christos Stavrakakis
    log.info('delete_network %s', network_id)
299 d2e73c0c Christos Stavrakakis
    net = util.get_network(network_id, request.user_uniq, for_update=True)
300 207b70d5 Giorgos Verigakis
    if net.public:
301 bd40abfa Christos Stavrakakis
        raise faults.Forbidden('Can not delete the public network.')
302 5061546f Christos Stavrakakis
303 22a9959d Christos Stavrakakis
    if net.deleted:
304 05d401cf Christos Stavrakakis
        raise faults.BadRequest("Network has been deleted.")
305 22a9959d Christos Stavrakakis
306 0196d9a3 Christos Stavrakakis
    if net.machines.all():  # Nics attached on network
307 bd40abfa Christos Stavrakakis
        raise faults.NetworkInUse('Machines are connected to network.')
308 0196d9a3 Christos Stavrakakis
309 e57893cd Christos Stavrakakis
    if net.floating_ips.filter(deleted=False).exists():
310 e57893cd Christos Stavrakakis
        raise faults.NetworkInUse("Network has allocated floating IP addresses")
311 e57893cd Christos Stavrakakis
312 5061546f Christos Stavrakakis
    net.action = 'DESTROY'
313 5061546f Christos Stavrakakis
    net.save()
314 5061546f Christos Stavrakakis
315 99af08a4 Christos Stavrakakis
    backend_networks = net.backend_networks.exclude(operstate="DELETED")
316 99af08a4 Christos Stavrakakis
    for bnet in backend_networks:
317 99af08a4 Christos Stavrakakis
        backend.delete_network(net, bnet.backend)
318 27cda06b Christos Stavrakakis
    if not backend_networks:
319 27cda06b Christos Stavrakakis
        backend.update_network_state(net)
320 0269afd6 Giorgos Verigakis
    return HttpResponse(status=204)
321 0269afd6 Giorgos Verigakis
322 6ef51e9f Giorgos Verigakis
323 b3fd98ae Christos Stavrakakis
@api.api_method(http_method='POST', user_required=True, logger=log)
324 b19653d1 Giorgos Verigakis
def network_action(request, network_id):
325 b3fd98ae Christos Stavrakakis
    req = utils.get_request_dict(request)
326 0c37a721 Christos Stavrakakis
    log.debug('network_action %s %s', network_id, req)
327 9e98ba3c Giorgos Verigakis
    if len(req) != 1:
328 bd40abfa Christos Stavrakakis
        raise faults.BadRequest('Malformed request.')
329 ce55f211 Kostas Papadimitriou
330 4b3b8688 Giorgos Verigakis
    net = util.get_network(network_id, request.user_uniq)
331 207b70d5 Giorgos Verigakis
    if net.public:
332 bd40abfa Christos Stavrakakis
        raise faults.Forbidden('Can not modify the public network.')
333 22a9959d Christos Stavrakakis
    if net.deleted:
334 05d401cf Christos Stavrakakis
        raise faults.BadRequest("Network has been deleted.")
335 ce55f211 Kostas Papadimitriou
336 0269afd6 Giorgos Verigakis
    try:
337 e221ade2 Christos Stavrakakis
        key = req.keys()[0]
338 e221ade2 Christos Stavrakakis
        val = req[key]
339 0269afd6 Giorgos Verigakis
        assert isinstance(val, dict)
340 0269afd6 Giorgos Verigakis
        return network_actions[key](request, net, req[key])
341 0269afd6 Giorgos Verigakis
    except KeyError:
342 bd40abfa Christos Stavrakakis
        raise faults.BadRequest('Unknown action.')
343 0269afd6 Giorgos Verigakis
    except AssertionError:
344 bd40abfa Christos Stavrakakis
        raise faults.BadRequest('Invalid argument.')