Revision f261896d 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

  
43 42
from models import Subnet
43
from synnefo.logic import networks
44

  
45
import ipaddr
44 46

  
45 47
log = getLogger('synnefo.neutron')
46 48

  
......
59 61
    if request.method == 'GET':
60 62
        return get_subnet(request, offset)
61 63
    elif request.method == 'DELETE':
62
        #return delete_network(request,offset)
63
        return HttpResponse("in subnet det delete")
64
        return delete_network(request, offset)
64 65
    elif request.method == 'PUT':
65 66
        #return update_network(request,offset)
66 67
        return HttpResponse("in subnet det put")
......
83 84

  
84 85
@api.api_method(http_method='POST', user_required=True, logger=log)
85 86
def create_subnet(request):
86
    '''Placeholder'''
87
    dic = utils.get_request_dict(request)
87
    '''Create a subnet'''
88
    user_id = request.user_uniq
88 89

  
89 90
    try:
91
        dic = utils.get_request_dict(request)
90 92
        subnet = dic['subnet']
91 93
        network_id = subnet['network_id']
92 94
        cidr = subnet['cidr']
93 95
    except KeyError:
94 96
        raise api.faults.BadRequest("Malformed request")
95 97

  
96
    ipversion = subnet.get('ip_version', '4')
97
    if ipversion != 4 or ipversion != 6:
98
    ipversion = subnet.get('ip_version', 4)
99
    if ipversion not in [4, 6]:
98 100
        raise api.faults.BadRequests("Malformed IP version type")
101

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

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

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

  
106
    ddnew_net = Network(name=name, user_id=user_id)
107
    new_net.save()
108
    net_dic = network_to_dict(new_net)
109
    data = json.dumps({'network': net_dic})
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"
110 123

  
111
    #try:
112
    #    sub = Subnet.objects.create(name=name, network_id=network_id,
113
    #                                cidr=cidr, ipversion=ipversion,
114
    #                                gateway=gateway)
115
    return HttpResponse("test world")
116
    #return 1
124
    return HttpResponse("test")
117 125

  
118 126

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

  
......
133 141
def delete_subnet(request, offset):
134 142
    '''Delete a subnet'''
135 143
    try:
136
        subnet = Subnet.objects.get(id=offset)
144
        subnet = Subnet.objects.get(subnet_id=offset)
137 145
    except Subnet.DoesNotExist:
138 146
        raise api.faults.ItemNotFound("Subnet not found")
139 147
        # Add support for 409 error, subnet in use
......
142 150
    return HttpResponse(status=204)
143 151

  
144 152

  
145
@api.api_method(http_method='PUT', user_required=True, logger=log)
146
def update_network(request, offset):
147
    try:
148
        net = Network.objects.get(id=offset)
149
    except Network.DoesNotExist:
150
        raise api.faults.ItemNotFound("Network not found")
151
    info = utils.get_request_dict(request)
152
    updatable = set(["name", "driver"])
153
    try:
154
        new_vals = info["network"]
155
    except Keyerror:
156
        raise api.faults.BadRequest()
157

  
158
    for key, val in new_vals.iteritems():
159
        if key in updatable:
160
            setattr(net, key, val)
161
        else:
162
            raise api.faults.BadRequest()
163
        net.save()
164
    net_dic = network_to_dict(net)
165
    data = json.dumps({"network": net_dic})
166
    return HttpResponse(data)
167

  
168

  
169 153
def subnet_to_dict(subnet):
170 154
    '''Returns a dictionary containing the info of a subnet'''
171
    dic = dict(id=subnet.subnet_id, network_id=subnet.network.id,
172
               name=subnet.name, tenant_id=subnet.network.userid,
173
               gateway_ip=subnet.gateway, ip_version=subnet.ipversion,
174
               cidr=subnet.cidr, enable_dhcp=subnet.dhcp,
175
               dns_nameservers=subnet.dns)
176
               # , allocation_pools=subnet.pools,
177
               # host_routes=subneth.ostroutes, snf-gateway6=subnet.gateway6,
178
               # snf-subnet6=subnet.subnet6)
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

  
179 171
    return dic

Also available in: Unified diff