Revision efabd2d4 snf-cyclades-app/synnefo/neutron/subnet_views.py

b/snf-cyclades-app/synnefo/neutron/subnet_views.py
107 107
    if ipversion not in [4, 6]:
108 108
        raise api.faults.BadRequest("Malformed IP version type")
109 109

  
110
    dhcp = subnet.get('enable_dhcp', True)
111
    if dhcp not in [True, False]:
112
        raise api.faults.BadRequest("Malformed request, enable_dhcp must be "
113
                                    "True or False")
114
    name = subnet.get('name', None)
115
    if len(str(name)) > Subnet.SUBNET_NAME_LENGTH:
116
        raise api.faults.BadRequest("Subnet name too long")
110
    dhcp = check_dhcp_value(subnet.get('enable_dhcp', True))
111
    name = check_name_length(subnet.get('name', None))
117 112

  
118 113
    # Returns the first available IP in the subnet
119 114
    if ipversion == 6:
120
        potential_gateway = IPv6Network(cidr).network + 1
115
        potential_gateway = str(IPv6Network(cidr).network + 1)
121 116
        check_number_of_subnets(network, 6)
122 117
    else:
123
        potential_gateway = IPv4Network(cidr).network + 1
118
        potential_gateway = str(IPv4Network(cidr).network + 1)
124 119
        check_number_of_subnets(network, 4)
125 120

  
126 121
    gateway = subnet.get('gateway_ip', potential_gateway)
127
    networks.validate_network_params(cidr, gateway)
122

  
123
    if ipversion == 6:
124
        networks.validate_network_params(None, None, cidr, gateway)
125
    else:
126
        networks.validate_network_params(cidr, gateway)
127

  
128
    check_for_hosts_dns(subnet)
128 129

  
129 130
    # FIX ME
130 131
    try:
......
134 135
    except:
135 136
        return "Error"
136 137

  
137
    return HttpResponse(sub, status=200)
138
    subnet_dict = subnet_to_dict(sub)
139
    data = json.dumps({'subnet': subnet_dict})
140
    return HttpResponse(data, status=200)
138 141

  
139 142

  
140 143
@api.api_method(http_method='GET', user_required=True, logger=log)
......
165 168
@api.api_method(http_method='PUT', user_required=True, logger=log)
166 169
def update_subnet(request, sub_id):
167 170
    '''Update info of a subnet'''
171
    dictionary = utils.get_request_dict(request)
172
    log.info('Update subnet %s', dictionary)
173
    user_id = request.user_uniq
168 174

  
175
    try:
176
        subnet = dictionary['subnet']
177
    except KeyError:
178
        raise api.faults.BadRequest("Malformed request")
169 179

  
170
# util functions
180
    original_subnet = get_subnet_fromdb(sub_id, user_id)
181
    original_dict = subnet_to_dict(original_subnet)
182

  
183
    if subnet.get('ip_version', None):
184
        raise api.faults.BadRequest("Malformed request, ip_version cannot be "
185
                                    "updated")
186
    if subnet.get('cidr', None):
187
        raise api.faults.BadRequest("Malformed request, cidr cannot be "
188
                                    "updated")
189
    if subnet.get('allocation_pools', None):
190
        raise api.faults.BadRequest("Malformed request, allocation pools "
191
                                    "cannot be updated")
192

  
193
    check_for_hosts_dns(subnet)
194
    name = subnet.get('name', original_dict['name'])
195
    if name:
196
        check_name_length(name)
197

  
198
    dhcp = subnet.get('enable_dhcp', original_dict['enable_dhcp'])
199
    check_dhcp_value(dhcp)
200

  
201
    gateway = subnet.get('gateway_ip', original_dict['gateway_ip'])
202
    #FIX ME, check if IP is in use
203
    if original_dict['ip_version'] == 6:
204
        networks.validate_network_params(None, None, original_dict['cidr'],
205
                                         gateway)
206
    else:
207
        networks.validate_network_params(original_dict['cidr'], gateway)
171 208

  
209
    try:
210
        original_subnet.gateway = gateway
211
        original_subnet.name = name
212
        original_subnet.dhcp = dhcp
213
        original_subnet.save()
214
    except:
215
        #Fix me
216
        return "Unknown Error"
217

  
218
    subnet_dict = subnet_to_dict(original_subnet)
219
    data = json.dumps({'subnet': subnet_dict})
220
    return HttpResponse(data, status=200)
172 221

  
222

  
223
#Utility functions
173 224
def subnet_to_dict(subnet):
174 225
    '''Returns a dictionary containing the info of a subnet'''
175 226
    # FIX ME, allocation pools
......
183 234

  
184 235

  
185 236
def check_number_of_subnets(network, version):
186
    '''Checks if a user can add a subnet in a network'''
237
    '''Check if a user can add a subnet in a network'''
187 238
    if network.subnet_set.filter(ipversion=version):
188 239
        raise api.faults.BadRequest("Only one subnet of IPv4/IPv6 per "
189 240
                                    "network is allowed")
190 241

  
191 242

  
243
def check_dhcp_value(dhcp):
244
    '''Check if dhcp value is in acceptable values'''
245
    if dhcp not in [True, False]:
246
        raise api.faults.BadRequest("Malformed request, enable_dhcp must be "
247
                                    "True or False")
248
    return dhcp
249

  
250

  
251
def check_name_length(name):
252
    '''Check if the length of a name is within acceptable value'''
253
    if len(str(name)) > Subnet.SUBNET_NAME_LENGTH:
254
        raise api.faults.BadRequest("Subnet name too long")
255
    return name
256

  
257

  
258
def check_for_hosts_dns(subnet):
259
    '''
260
    Check if a request contains host_routes or dns_nameservers options
261
    Expects the request in a dictionary format
262
    '''
263
    if subnet.get('host_routes', None):
264
        raise api.faults.BadRequest("Setting host routes isn't supported")
265
    if subnet.get('dns_nameservers', None):
266
        raise api.faults.BadRequest("Setting dns nameservers isn't supported")
267

  
268

  
192 269
def get_subnet_fromdb(subnet_id, user_id, for_update=False):
193
    """
270
    '''
194 271
    Return a Subnet instance or raise ItemNotFound.
195 272
    This is the same as util.get_network
196
    """
273
    '''
197 274
    try:
198 275
        subnet_id = int(subnet_id)
199
        objects = Subnet.objects
200 276
        if for_update:
201
            objects = objects.select_for_update()
202
        return objects.get(id=subnet_id, network__userid=user_id)
277
            return Subnet.objects.select_for_update().get(id=subnet_id,
278
                                                          network__userid=
279
                                                          user_id)
280
        return Subnet.objects.get(id=subnet_id, network__userid=user_id)
203 281
    except (ValueError, Subnet.DoesNotExist):
204 282
        raise api.faults.ItemNotFound('Subnet not found.')

Also available in: Unified diff