Revision f261896d

b/snf-cyclades-app/synnefo/neutron/models.py
222 222
    ipversion = models.IntegerField('IP Version', default=4)
223 223
    cidr = models.CharField('Subnet', max_length=32, null=True)
224 224
    gateway = models.CharField('Gateway', max_length=32, null=True)
225
    #dns = models.
226
    #pools = models.
227
    #hostroutes = models.
228 225
    dhcp = models.BooleanField('DHCP', default=True)
229 226

  
230 227
    # Synnefo related fields
231 228
    # subnet6 will be null for IPv4 only networks
232 229
    subnet6 = models.CharField('IPv6 Subnet', max_length=64, null=True)
233 230
    gateway6 = models.CharField('IPv6 Gateway', max_length=64, null=True)
234
    #dns_nameservers = models.
235 231
    #pool = models.OneToOneField('IPPoolTable', related_name='network',
236 232
    #                            default=lambda: IPPoolTable.objects.create(
237 233
    #                                                        available_map='',
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
b/snf-cyclades-app/synnefo/neutron/tests/api.py
73 73
        response = self.put(url,params=json.dumps(request),user=test_net.userid)
74 74
        self.assertEqual(response.status_code, 400)
75 75

  
76

  
76 77
class SubnetTest(BaseAPITest):
77 78
    def test_list_subnets(self):
78 79
        '''Test Subnet list'''
......
95 96
        response = self.get(url)
96 97
        self.assertItemNotFound(response)
97 98

  
98
    def test_subnet_delete_not_in_use(self):
99
    def test_subnet_delete_not_found(self):
100
        '''Test delete a subnet that doesn't exist'''
101
        url = join_urls(SUBNETS_URL, '52')
102
        response = self.get(url)
103
        self.assertItemNotFound(response)
104

  
105
    def test_subnet_delete(self):
99 106
        '''Test delete a subnet that's not in use'''
100 107
        url = join_urls(SUBNETS_URL, '42')
101 108
        response = self.get(url)
102 109
        self.assertItemNotFound(response)
110

  
111
    def test_create_subnet_with_malformed_ipversion(self):
112
        '''Create a subnet with a malformed ip_version type'''
113
        # PEP8 for {{}}?
114
        request = {'subnet': {'network_id':
115
                              'ed2e3c10-2e43-4297-9006-2863a2d1abbc',
116
                              'cidr': '10.0.3.0/24',
117
                              'ip_version': 8}}
118

  
119
        response = self.post(SUBNETS_URL, "user9", json.dumps(request), "json")
120
        self.assertBadRequest(response)

Also available in: Unified diff