root / snf-cyclades-app / synnefo / neutron / subnet_views.py @ 882b662f
History | View | Annotate | Download (6.4 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 |
from models import Subnet |
43 |
from synnefo.logic import networks |
44 |
|
45 |
import ipaddr |
46 |
|
47 |
log = getLogger(__name__) |
48 |
|
49 |
|
50 |
def demux(request): |
51 |
if request.method == 'GET': |
52 |
return list_subnets(request)
|
53 |
elif request.method == 'POST': |
54 |
return create_subnet(request)
|
55 |
else:
|
56 |
return api.api_method_not_allowed(request)
|
57 |
|
58 |
|
59 |
def subnet_demux(request, offset): |
60 |
if request.method == 'GET': |
61 |
return get_subnet(request, offset)
|
62 |
elif request.method == 'DELETE': |
63 |
return delete_subnet(request, offset)
|
64 |
elif request.method == 'PUT': |
65 |
return update_subnet(request, offset)
|
66 |
else:
|
67 |
return api.api_method_not_allowed(request)
|
68 |
|
69 |
|
70 |
@api.api_method(http_method='GET', user_required=True, logger=log) |
71 |
def list_subnets(request): |
72 |
'''List all subnets of a user'''
|
73 |
log.debug('list_subnets')
|
74 |
|
75 |
user_subnets = Subnet.objects.filter(network__userid=request.user_uniq) |
76 |
subnets_dict = [subnet_to_dict(user_subnets) |
77 |
for net in user_subnets.order_by('name')] |
78 |
data = json.dumps({'subnets': subnets_dict})
|
79 |
|
80 |
return HttpResponse(data, status=200) |
81 |
|
82 |
|
83 |
@api.api_method(http_method='POST', user_required=True, logger=log) |
84 |
def create_subnet(request): |
85 |
'''Create a subnet'''
|
86 |
|
87 |
dic = utils.get_request_dict(request) |
88 |
log.info('create subnet %s', dic)
|
89 |
user_id = request.user_uniq |
90 |
|
91 |
try:
|
92 |
subnet = dic['subnet']
|
93 |
network_id = subnet['network_id']
|
94 |
cidr = subnet['cidr']
|
95 |
except KeyError: |
96 |
raise api.faults.BadRequest("Malformed request") |
97 |
|
98 |
ipversion = subnet.get('ip_version', 4) |
99 |
if ipversion not in [4, 6]: |
100 |
raise api.faults.BadRequest("Malformed IP version type") |
101 |
|
102 |
dhcp = subnet.get('enable_dhcp', True) |
103 |
if dhcp not in [True, False]: |
104 |
raise api.faults.BadRequest("Malformed request, enable_dhcp must be" |
105 |
" True or False")
|
106 |
name = subnet.get('name', None) |
107 |
if len(str(name)) > Subnet.SUBNET_NAME_LENGTH: |
108 |
raise api.faults.BadRequest("Subnet name too long") |
109 |
|
110 |
# FIX ME, SNF:gateway6 vs gateway6
|
111 |
gateway6 = subnet.get('SNF:gateway6', None) |
112 |
subnet6 = subnet.get('SNF:subnet6', None) |
113 |
|
114 |
# Returns the first available IP in the subnet
|
115 |
potential_gateway = ipaddr.IPv4Network(cidr).network + 1
|
116 |
gateway = subnet.get('gateway_ip', potential_gateway)
|
117 |
|
118 |
networks.validate_network_params(cidr, gateway, subnet6, gateway6) |
119 |
|
120 |
# FIX ME
|
121 |
try:
|
122 |
sub = Subnet.objects.create(name=name, network_id=network_id, |
123 |
cidr=cidr, ipversion=ipversion, |
124 |
gateway=gateway, gateway6=gateway6, |
125 |
subnet6=subnet6) |
126 |
except:
|
127 |
print "Error" |
128 |
|
129 |
return HttpResponse("test") |
130 |
|
131 |
|
132 |
@api.api_method(http_method='GET', user_required=True, logger=log) |
133 |
def get_subnet(request, offset): |
134 |
'''Show info of a specific subnet'''
|
135 |
try:
|
136 |
subnet = Subnet.objects.get(subnet_id=offset) |
137 |
except Subnet.DoesNotExist:
|
138 |
raise api.faults.ItemNotFound("Subnet not found") |
139 |
|
140 |
subnet_dic = subnet_to_dict(subnet) |
141 |
data = json.dumps({'subnet': subnet_dic})
|
142 |
return HttpResponse(data, status=200) |
143 |
|
144 |
|
145 |
@api.api_method(http_method='DELETE', user_required=True, logger=log) |
146 |
def delete_subnet(request, offset): |
147 |
'''Delete a subnet'''
|
148 |
# Commented until we have a design document
|
149 |
#log.info('delete_subnet %s', offset)
|
150 |
#try:
|
151 |
# subnet = Subnet.objects.get(subnet_id=offset)
|
152 |
#except Subnet.DoesNotExist:
|
153 |
# raise api.faults.ItemNotFound("Subnet not found")
|
154 |
# Add support for 409 error, subnet in use
|
155 |
|
156 |
#subnets.delete()
|
157 |
#return HttpResponse(status=204)
|
158 |
raise api.faults.BadRequest("Deletion of a subnet is not supported") |
159 |
|
160 |
|
161 |
def subnet_to_dict(subnet): |
162 |
'''Returns a dictionary containing the info of a subnet'''
|
163 |
# FIX ME, allocation pools
|
164 |
dic = dict({'id': subnet.sunbet_id, 'network_id': subnet.network.id, |
165 |
'name': subnet.name, 'tenant_id': subnet.network.userid, |
166 |
'gateway_ip': subnet.gateway, 'ip_version': subnet.ipversion, |
167 |
'cidr': subnet.cidr, 'enable_dhcp': subnet.dhcp, |
168 |
'dns_nameservers': [], 'host_routes': [], |
169 |
'allocation_pools': [], 'SNF:gateway6': subnet.gateway6, |
170 |
'SNF:subnet6': subnet.subnet6})
|
171 |
|
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=[], allocation_pools=[], host_routes=[],
|
177 |
# snf-gateway6=subnet.gateway6, snf-subnet6=subnet.subnet6)
|
178 |
|
179 |
return dic
|