root / snf-cyclades-app / synnefo / neutron / subnet_views.py @ 7eae8fce
History | View | Annotate | Download (6.1 kB)
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from logging import getLogger |
35 |
from snf_django.lib import api |
36 |
from snf_django.lib.api import faults |
37 |
|
38 |
from django.http import HttpResponse |
39 |
from django.utils import simplejson as json |
40 |
|
41 |
from snf_django.lib.api import utils |
42 |
|
43 |
from models import Subnet |
44 |
|
45 |
log = getLogger('synnefo.neutron')
|
46 |
|
47 |
|
48 |
def demux(request): |
49 |
if request.method == 'GET': |
50 |
return list_subnets(request)
|
51 |
elif request.method == 'POST': |
52 |
#return create_network(request)
|
53 |
return HttpResponse("in subnet POST") |
54 |
else:
|
55 |
return api.api_method_not_allowed(request)
|
56 |
|
57 |
|
58 |
def subnet_demux(request, offset): |
59 |
if request.method == 'GET': |
60 |
#return get_network(request,offset)
|
61 |
return HttpResponse("in subnet det get") |
62 |
elif request.method == 'DELETE': |
63 |
#return delete_network(request,offset)
|
64 |
return HttpResponse("in subnet det delete") |
65 |
elif request.method == 'PUT': |
66 |
#return update_network(request,offset)
|
67 |
return HttpResponse("in subnet det put") |
68 |
else:
|
69 |
return api.api_method_not_allowed(request)
|
70 |
|
71 |
|
72 |
@api.api_method(http_method='GET', user_required=True, logger=log) |
73 |
def list_subnets(request): |
74 |
'''Placeholder'''
|
75 |
log.debug('list_subnets')
|
76 |
|
77 |
user_subnets = Subnet.objects.filter(network__userid=request.user_uniq) |
78 |
subnets_dict = [subnet_to_dict(user_subnets) |
79 |
for net in user_subnets.order_by('name')] |
80 |
data = json.dumps({'subnets': subnets_dict})
|
81 |
|
82 |
return HttpResponse(data, status=200) |
83 |
|
84 |
|
85 |
@api.api_method(http_method='POST', user_required=True, logger=log) |
86 |
def create_subnet(request): |
87 |
'''Placeholder'''
|
88 |
dic = utils.get_request_dict(request) |
89 |
|
90 |
try:
|
91 |
subnet = dic['subnet']
|
92 |
network_id = subnet['network_id']
|
93 |
cidr = subnet['cidr']
|
94 |
except KeyError: |
95 |
raise api.faults.BadRequest("Malformed request") |
96 |
|
97 |
ipversion = subnet.get('ip_version', '4') |
98 |
if ipversion != 4 or ipversion != 6: |
99 |
raise api.faults.BadRequests("Malformed IP version type") |
100 |
dhcp = subnet.get('enable_dhcp', True) |
101 |
name = subnet.get('name', None) |
102 |
|
103 |
# Returns the first available ip in the subnet
|
104 |
potential_gateway = ipaddr.IPv4Network(cidr).network + 1
|
105 |
gateway = subnet.get('gateway_ip', potential_gateway)
|
106 |
|
107 |
ddnew_net = Network(name=name, user_id=user_id) |
108 |
new_net.save() |
109 |
net_dic = network_to_dict(new_net) |
110 |
data = json.dumps({'network': net_dic})
|
111 |
|
112 |
#try:
|
113 |
# sub = Subnet.objects.create(name=name, network_id=network_id,
|
114 |
# cidr=cidr, ipversion=ipversion,
|
115 |
# gateway=gateway)
|
116 |
return HttpResponse("test world") |
117 |
#return 1
|
118 |
|
119 |
|
120 |
@api.api_method(http_method='GET', user_required=True, logger=log) |
121 |
def get_subnet(request, offset): |
122 |
'''Placeholder'''
|
123 |
try:
|
124 |
subnet = Subnet.objects.get(id=offset) |
125 |
except Subnet.DoesNotExist:
|
126 |
raise api.faults.ItemNotFound("Subnet not found") |
127 |
|
128 |
subnet_dic = subnet_to_dict(subnet) |
129 |
data = json.dumps({'subnet': subnet_dic})
|
130 |
return HttpResponse(data, status=200) |
131 |
|
132 |
|
133 |
@api.api_method(http_method='DELETE', user_required=True, logger=log) |
134 |
def delete_subnet(request, offset): |
135 |
'''Placeholder'''
|
136 |
try:
|
137 |
subnet = Subnet.objects.get(id=offset) |
138 |
except Subnet.DoesNotExist:
|
139 |
raise api.faults.ItemNotFound("Subnet not found") |
140 |
# Add support for 409 error, subnet in use
|
141 |
|
142 |
subnet.delete() |
143 |
return HttpResponse(status=204) |
144 |
|
145 |
|
146 |
@api.api_method(http_method='PUT', user_required=True, logger=log) |
147 |
def update_network(request, offset): |
148 |
try:
|
149 |
net = Network.objects.get(id=offset) |
150 |
except Network.DoesNotExist:
|
151 |
raise api.faults.ItemNotFound("Network not found") |
152 |
info = utils.get_request_dict(request) |
153 |
updatable = set(["name", "driver"]) |
154 |
try:
|
155 |
new_vals = info["network"]
|
156 |
except Keyerror:
|
157 |
raise api.faults.BadRequest()
|
158 |
|
159 |
for key, val in new_vals.iteritems(): |
160 |
if key in updatable: |
161 |
setattr(net, key, val)
|
162 |
else:
|
163 |
raise api.faults.BadRequest()
|
164 |
net.save() |
165 |
net_dic = network_to_dict(net) |
166 |
data = json.dumps({"network": net_dic})
|
167 |
return HttpResponse(data)
|
168 |
|
169 |
|
170 |
def subnet_to_dict(subnet): |
171 |
'''Returns a dictionary containing the info of a subnet'''
|
172 |
dic = dict(id=subnet.subnet_id, network_id=subnet.network.id,
|
173 |
name=subnet.name, tenant_id=subnet.network.userid, |
174 |
gateway_ip=subnet.gateway, ip_version=subnet.ipversion, |
175 |
cidr=subnet.cidr, enable_dhcp=subnet.dhcp, |
176 |
dns_nameservers=subnet.dns) |
177 |
# , allocation_pools=subnet.pools,
|
178 |
# host_routes=subneth.ostroutes, snf-gateway6=subnet.gateway6,
|
179 |
# snf-subnet6=subnet.subnet6)
|
180 |
return dic
|