Revision 0dae1b9f snf-cyclades-app/synnefo/neutron/subnet_views.py
b/snf-cyclades-app/synnefo/neutron/subnet_views.py | ||
---|---|---|
39 | 39 |
from django.utils import simplejson as json |
40 | 40 |
|
41 | 41 |
from snf_django.lib.api import utils |
42 |
from models import Subnet |
|
42 |
from models import Subnet, Network
|
|
43 | 43 |
from synnefo.logic import networks |
44 | 44 |
|
45 |
import ipaddr
|
|
45 |
from ipaddr import IPv4Network, IPv6Network
|
|
46 | 46 |
|
47 | 47 |
log = getLogger(__name__) |
48 | 48 |
|
... | ... | |
56 | 56 |
return api.api_method_not_allowed(request) |
57 | 57 |
|
58 | 58 |
|
59 |
def subnet_demux(request, offset):
|
|
59 |
def subnet_demux(request, sub_id):
|
|
60 | 60 |
if request.method == 'GET': |
61 |
return get_subnet(request, offset)
|
|
61 |
return get_subnet(request, sub_id)
|
|
62 | 62 |
elif request.method == 'DELETE': |
63 |
return delete_subnet(request, offset)
|
|
63 |
return delete_subnet(request, sub_id)
|
|
64 | 64 |
elif request.method == 'PUT': |
65 |
return update_subnet(request, offset)
|
|
65 |
return update_subnet(request, sub_id)
|
|
66 | 66 |
else: |
67 | 67 |
return api.api_method_not_allowed(request) |
68 | 68 |
|
... | ... | |
73 | 73 |
log.debug('list_subnets') |
74 | 74 |
|
75 | 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')]
|
|
76 |
subnets_dict = [subnet_to_dict(sub)
|
|
77 |
for sub in user_subnets.order_by('id')]
|
|
78 | 78 |
data = json.dumps({'subnets': subnets_dict}) |
79 | 79 |
|
80 | 80 |
return HttpResponse(data, status=200) |
... | ... | |
84 | 84 |
def create_subnet(request): |
85 | 85 |
'''Create a subnet''' |
86 | 86 |
|
87 |
dic = utils.get_request_dict(request) |
|
88 |
log.info('create subnet %s', dic) |
|
87 |
dictionary = utils.get_request_dict(request)
|
|
88 |
log.info('create subnet %s', dictionary)
|
|
89 | 89 |
user_id = request.user_uniq |
90 | 90 |
|
91 | 91 |
try: |
92 |
subnet = dic['subnet'] |
|
92 |
subnet = dictionary['subnet']
|
|
93 | 93 |
network_id = subnet['network_id'] |
94 | 94 |
cidr = subnet['cidr'] |
95 | 95 |
except KeyError: |
96 | 96 |
raise api.faults.BadRequest("Malformed request") |
97 | 97 |
|
98 |
try: |
|
99 |
network = Network.objects.get(id=network_id) |
|
100 |
except Network.DoesNotExist: |
|
101 |
raise api.faults.ItemNotFound("No networks found with that id") |
|
102 |
|
|
103 |
if user_id != network.userid: |
|
104 |
raise api.faults.Unauthorized("Unauthorized operation") |
|
105 |
|
|
98 | 106 |
ipversion = subnet.get('ip_version', 4) |
99 | 107 |
if ipversion not in [4, 6]: |
100 | 108 |
raise api.faults.BadRequest("Malformed IP version type") |
101 | 109 |
|
102 | 110 |
dhcp = subnet.get('enable_dhcp', True) |
103 | 111 |
if dhcp not in [True, False]: |
104 |
raise api.faults.BadRequest("Malformed request, enable_dhcp must be" |
|
105 |
" True or False")
|
|
112 |
raise api.faults.BadRequest("Malformed request, enable_dhcp must be "
|
|
113 |
"True or False") |
|
106 | 114 |
name = subnet.get('name', None) |
107 | 115 |
if len(str(name)) > Subnet.SUBNET_NAME_LENGTH: |
108 | 116 |
raise api.faults.BadRequest("Subnet name too long") |
109 | 117 |
|
110 |
# FIX ME, SNF:gateway6 vs gateway6 |
|
111 |
gateway6 = subnet.get('SNF:gateway6', None) |
|
112 |
subnet6 = subnet.get('SNF:subnet6', None) |
|
113 |
|
|
114 | 118 |
# Returns the first available IP in the subnet |
115 |
potential_gateway = ipaddr.IPv4Network(cidr).network + 1 |
|
116 |
gateway = subnet.get('gateway_ip', potential_gateway) |
|
119 |
if ipversion == 6: |
|
120 |
potential_gateway = IPv6Network(cidr).network + 1 |
|
121 |
check_number_of_subnets(network, 6) |
|
122 |
else: |
|
123 |
potential_gateway = IPv4Network(cidr).network + 1 |
|
124 |
check_number_of_subnets(network, 4) |
|
117 | 125 |
|
118 |
networks.validate_network_params(cidr, gateway, subnet6, gateway6) |
|
126 |
gateway = subnet.get('gateway_ip', potential_gateway) |
|
127 |
networks.validate_network_params(cidr, gateway) |
|
119 | 128 |
|
120 | 129 |
# FIX ME |
121 | 130 |
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) |
|
131 |
sub = Subnet.objects.create(name=name, network=network, cidr=cidr, |
|
132 |
ipversion=ipversion, gateway=gateway, |
|
133 |
dhcp=dhcp) |
|
126 | 134 |
except: |
127 |
print "Error"
|
|
135 |
return "Error"
|
|
128 | 136 |
|
129 |
return HttpResponse("test")
|
|
137 |
return HttpResponse(sub, status=200)
|
|
130 | 138 |
|
131 | 139 |
|
132 | 140 |
@api.api_method(http_method='GET', user_required=True, logger=log) |
133 |
def get_subnet(request, offset):
|
|
141 |
def get_subnet(request, sub_id):
|
|
134 | 142 |
'''Show info of a specific subnet''' |
143 |
log.debug('get_subnet %s', sub_id) |
|
144 |
user_id = request.user_uniq |
|
145 |
|
|
135 | 146 |
try: |
136 |
subnet = Subnet.objects.get(subnet_id=offset)
|
|
147 |
subnet = Subnet.objects.get(id=sub_id)
|
|
137 | 148 |
except Subnet.DoesNotExist: |
138 | 149 |
raise api.faults.ItemNotFound("Subnet not found") |
139 | 150 |
|
140 |
subnet_dic = subnet_to_dict(subnet) |
|
141 |
data = json.dumps({'subnet': subnet_dic}) |
|
151 |
if subnet.network.userid != user_id: |
|
152 |
raise api.failts.Unauthorized("You're not allowed to view this subnet") |
|
153 |
|
|
154 |
subnet_dict = subnet_to_dict(subnet) |
|
155 |
data = json.dumps({'subnet': subnet_dict}) |
|
142 | 156 |
return HttpResponse(data, status=200) |
143 | 157 |
|
144 | 158 |
|
145 | 159 |
@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) |
|
160 |
def delete_subnet(request, sub_id): |
|
161 |
'''Delete a subnet -- Operation not allowed''' |
|
158 | 162 |
raise api.faults.BadRequest("Deletion of a subnet is not supported") |
159 | 163 |
|
160 | 164 |
|
165 |
@api.api_method(http_method='PUT', user_required=True, logger=log) |
|
166 |
def update_subnet(request, sub_id): |
|
167 |
'''Update info of a subnet''' |
|
168 |
|
|
169 |
|
|
161 | 170 |
def subnet_to_dict(subnet): |
162 | 171 |
'''Returns a dictionary containing the info of a subnet''' |
163 | 172 |
# 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 |
|
173 |
dictionary = dict({'id': subnet.id, 'network_id': subnet.network.id, |
|
174 |
'name': subnet.name, 'tenant_id': subnet.network.userid, |
|
175 |
'gateway_ip': subnet.gateway, |
|
176 |
'ip_version': subnet.ipversion, 'cidr': subnet.cidr, |
|
177 |
'enable_dhcp': subnet.dhcp, 'dns_nameservers': [], |
|
178 |
'host_routes': [], 'allocation_pools': []}) |
|
179 |
return dictionary |
|
180 |
|
|
181 |
|
|
182 |
def check_number_of_subnets(network, version): |
|
183 |
'''Checks if a user can add a subnet in a network''' |
|
184 |
if network.subnet_set.filter(ipversion=version): |
|
185 |
raise api.faults.BadRequest("Only one subnet of IPv4/IPv6 per " |
|
186 |
"network is allowed") |
Also available in: Unified diff