Revision 911a1bc1 snf-cyclades-app/synnefo/api/subnets.py

b/snf-cyclades-app/synnefo/api/subnets.py
131 131
        networks.validate_network_params(None, None, cidr, gateway)
132 132
        slac = subnet.get('slac', None)
133 133
        if slac is not None:
134
            dhcp = check_dhcp_value(slac)
134
            dhcp = check_boolean_value(slac, "slac")
135 135
        else:
136
            dhcp = check_dhcp_value(subnet.get('enable_dhcp', True))
136
            dhcp = check_boolean_value(subnet.get('enable_dhcp', True), "dhcp")
137 137
    else:
138 138
        networks.validate_network_params(cidr, gateway)
139
        dhcp = check_dhcp_value(subnet.get('enable_dhcp', True))
139
        dhcp = check_boolean_value(subnet.get('enable_dhcp', True), "dhcp")
140 140

  
141 141
    name = check_name_length(subnet.get('name', None))
142 142

  
......
227 227
    if not name:
228 228
        raise api.faults.BadRequest("Only the name of subnet can be updated")
229 229

  
230
    #if subnet.get('ip_version', None):
231
    #    raise api.faults.BadRequest("Malformed request, ip_version cannot be "
232
    #                                "updated")
233
    #if subnet.get('cidr', None):
234
    #    raise api.faults.BadRequest("Malformed request, cidr cannot be "
235
    #                                "updated")
236
    #if subnet.get('allocation_pools', None):
237
    #    raise api.faults.BadRequest("Malformed request, allocation pools "
238
    #                                "cannot be updated")
239
    #
240
    # Check if request contained host/dns information
241
    #check_for_hosts_dns(subnet)
242
    #
243
    #name = subnet.get('name', original_dict['name'])
244 230
    check_name_length(name)
245 231

  
246
    #dhcp = subnet.get('enable_dhcp', original_dict['enable_dhcp'])
247
    #check_dhcp_value(dhcp)
248
    #
249
    #gateway = subnet.get('gateway_ip', original_dict['gateway_ip'])
250
    #FIX ME, check if IP is in use
251
    #if original_dict['ip_version'] == 6:
252
    #    networks.validate_network_params(None, None, original_dict['cidr'],
253
    #                                     gateway)
254
    #else:
255
    #    networks.validate_network_params(original_dict['cidr'], gateway)
256
    #
257 232
    try:
258
        #original_subnet.gateway = gateway
259 233
        original_subnet.name = name
260
        #original_subnet.dhcp = dhcp
261 234
        original_subnet.save()
262 235
    except:
263 236
        #Fix me
......
271 244
#Utility functions
272 245
def subnet_to_dict(subnet):
273 246
    """Returns a dictionary containing the info of a subnet"""
274
    # FIX ME, allocation pools
275
    dictionary = dict({'id': subnet.id, 'network_id': subnet.network.id,
276
                       'name': subnet.name, 'tenant_id': subnet.network.userid,
247
    dns = check_empty_lists(subnet.dns_nameservers)
248
    hosts = check_empty_lists(subnet.host_routes)
249
    #allocation_pools =
250

  
251
    dictionary = dict({'id': str(subnet.id),
252
                       'network_id': str(subnet.network.id),
253
                       'name': subnet.name if subnet.name is not None else "",
254
                       'tenant_id': subnet.network.userid,
255
                       'user_id': subnet.network.userid,
277 256
                       'gateway_ip': subnet.gateway,
278
                       'ip_version': subnet.ipversion, 'cidr': subnet.cidr,
257
                       'ip_version': subnet.ipversion,
258
                       'cidr': subnet.cidr,
279 259
                       'enable_dhcp': subnet.dhcp,
280
                       'dns_nameservers': subnet.dns_nameservers,
281
                       'host_routes': subnet.host_routes,
260
                       'dns_nameservers': dns,
261
                       'host_routes': hosts,
282 262
                       'allocation_pools': []})
283 263

  
284 264
    if subnet.ipversion == 6:
......
287 267
    return dictionary
288 268

  
289 269

  
270
def check_empty_lists(value):
271
    """Check if value is Null/None, in which case we return an empty list"""
272
    if value is None:
273
        return []
274
    return value
275

  
276

  
290 277
def check_number_of_subnets(network, version):
291 278
    """Check if a user can add a subnet in a network"""
292 279
    if network.subnets.filter(ipversion=version):
......
294 281
                                    "network is allowed")
295 282

  
296 283

  
297
def check_dhcp_value(dhcp):
284
def check_boolean_value(value, key):
298 285
    """Check if dhcp value is in acceptable values"""
299
    if dhcp not in [True, False]:
300
        raise api.faults.BadRequest("Malformed request, enable_dhcp/slac must "
301
                                    "be True or False")
302
    return dhcp
286
    if value not in [True, False]:
287
        raise api.faults.BadRequest("Malformed request, %s must "
288
                                    "be True or False" % key)
289
    return value
303 290

  
304 291

  
305 292
def check_name_length(name):
......
333 320
                                                          user_id)
334 321
        return Subnet.objects.get(id=subnet_id, network__userid=user_id)
335 322
    except (ValueError, Subnet.DoesNotExist):
336
        raise api.faults.ItemNotFound('Subnet not found.')
323
        raise api.faults.ItemNotFound('Subnet not found')
337 324

  
338 325

  
339 326
def parse_ip_pools(pools):
......
346 333
    """
347 334
    pool_list = list()
348 335
    for pool in pools:
349
        asd = [pool["start"], pool["end"]]
350
        pool_list.append(asd)
336
        parse = [pool["start"], pool["end"]]
337
        pool_list.append(parse)
351 338
    return pool_list
352 339

  
353 340

  
......
355 342
    """
356 343
    Validate the given IP pools are inside the cidr range
357 344
    Validate there are no overlaps in the given pools
345
    Finally, validate the gateway isn't in the given ip pools
358 346
    Input must be a list containing a sublist with start/end ranges as strings
359 347
    [["192.168.42.1", "192.168.42.15"], ["192.168.42.30", "192.168.42.60"]]
360 348
    """
361
    pool_list = list()
362
    for pool in pools:
363
        pool_list.append(map(lambda a: IPAddress(a), pool))
364
    pool_list = sorted(pool_list)
349
    pool_list = [(map(lambda ip_str: IPAddress(ip_str), pool))
350
                 for pool in pools]
351
    pool_list.sort()
365 352

  
366 353
    if pool_list[0][0] <= cidr.network:
367 354
        raise api.faults.Conflict("IP Pool out of bounds")
......
369 356
        raise api.faults.Conflict("IP Pool out of bounds")
370 357

  
371 358
    for start, end in pool_list:
372
        if start >= end:
359
        if start > end:
373 360
            raise api.faults.Conflict("Invalid IP pool range")
374 361
        # Raise BadRequest if gateway is inside the pool range
375 362
        if not (gateway < start or gateway > end):
......
378 365
    # Check if there is a conflict between the IP Poll ranges
379 366
    end = cidr.network
380 367
    for pool in pool_list:
381
        if end >= pool[1]:
368
        if end >= pool[0]:
382 369
            raise api.faults.Conflict("IP Pool range conflict")
383 370
        end = pool[1]

Also available in: Unified diff