Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / neutron / subnet_views.py @ f261896d

History | View | Annotate | Download (6 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('synnefo.neutron')
48

    
49

    
50
def demux(request):
51
    if request.method == 'GET':
52
        return list_subnets(request)
53
    elif request.method == 'POST':
54
        #return create_network(request)
55
        return HttpResponse("in subnet POST")
56
    else:
57
        return api.api_method_not_allowed(request)
58

    
59

    
60
def subnet_demux(request, offset):
61
    if request.method == 'GET':
62
        return get_subnet(request, offset)
63
    elif request.method == 'DELETE':
64
        return delete_network(request, offset)
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
    '''List all subnets of a user'''
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
    '''Create a subnet'''
88
    user_id = request.user_uniq
89

    
90
    try:
91
        dic = utils.get_request_dict(request)
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.BadRequests("Malformed IP version type")
101

    
102
    dhcp = subnet.get('enable_dhcp', True)
103
    name = subnet.get('name', None)
104

    
105
    # FIX ME, SNF:gateway6 vs gateway6
106
    gateway6 = subnet.get('SNF:gateway6', None)
107
    subnet6 = subnet.get('SNF:subnet6', None)
108

    
109
    # Returns the first available ip in the subnet
110
    potential_gateway = ipaddr.IPv4Network(cidr).network + 1
111
    gateway = subnet.get('gateway_ip', potential_gateway)
112

    
113
    networks.validate_network_params(subnet, gateway, subnet6, gateway6)
114

    
115
    try:
116
        sub = Subnet.objects.create(name=name, network_id=network_id,
117
                                    cidr=cidr, ipversion=ipversion,
118
                                    gateway=gateway, gateway6=gateway6,
119
                                    subnet6=subnet6)
120
    # FIX ME
121
    except:
122
        print "Error"
123

    
124
    return HttpResponse("test")
125

    
126

    
127
@api.api_method(http_method='GET', user_required=True, logger=log)
128
def get_subnet(request, offset):
129
    '''Show info of a specific subnet'''
130
    try:
131
        subnet = Subnet.objects.get(subnet_id=offset)
132
    except Subnet.DoesNotExist:
133
        raise api.faults.ItemNotFound("Subnet not found")
134

    
135
    subnet_dic = subnet_to_dict(subnet)
136
    data = json.dumps({'subnet': subnet_dic})
137
    return HttpResponse(data, status=200)
138

    
139

    
140
@api.api_method(http_method='DELETE', user_required=True, logger=log)
141
def delete_subnet(request, offset):
142
    '''Delete a subnet'''
143
    try:
144
        subnet = Subnet.objects.get(subnet_id=offset)
145
    except Subnet.DoesNotExist:
146
        raise api.faults.ItemNotFound("Subnet not found")
147
        # Add support for 409 error, subnet in use
148

    
149
    subnet.delete()
150
    return HttpResponse(status=204)
151

    
152

    
153
def subnet_to_dict(subnet):
154
    '''Returns a dictionary containing the info of a subnet'''
155
    # FIX ME, allocation pools
156
    dic = dict({'id': subnet.sunbet_id, 'network_id': subnet.network.id,
157
                'name': subnet.name, 'tenant_id': subnet.network.userid,
158
                'gateway_ip': subnet.gateway, 'ip_version': subnet.ipversion,
159
                'cidr': subnet.cidr, 'enable_dhcp': subnet.dhcp,
160
                'dns_nameservers': [], 'host_routes': [],
161
                'allocation_pools': [], 'SNF:gateway6': subnet.gateway6,
162
                'SNF:subnet6': subnet.subnet6})
163

    
164
#    dic = dict(id=subnet.subnet_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=[], allocation_pools=[], host_routes=[],
169
#               snf-gateway6=subnet.gateway6, snf-subnet6=subnet.subnet6)
170

    
171
    return dic