Revision 316787ab snf-cyclades-app/synnefo/logic/subnets.py

b/snf-cyclades-app/synnefo/logic/subnets.py
70 70
@transaction.commit_on_success
71 71
def create_subnet(network_id, cidr, name, ipversion, gateway, dhcp, slac,
72 72
                  dns_nameservers, allocation_pools, host_routes, user_id):
73
    """
74
    Create a subnet
73
    """Create a subnet
75 74
    network_id and the desired cidr are mandatory, everything else is optional
75

  
76 76
    """
77 77
    try:
78 78
        network = Network.objects.get(id=network_id)
......
150 150

  
151 151

  
152 152
def delete_subnet():
153
    """
154
    Delete a subnet, raises BadRequest
153
    """Delete a subnet, raises BadRequest
155 154
    A subnet is deleted ONLY when the network that it belongs to is deleted
155

  
156 156
    """
157 157
    raise api.faults.BadRequest("Deletion of a subnet is not supported")
158 158

  
159 159

  
160 160
@transaction.commit_on_success
161 161
def update_subnet(sub_id, name):
162
    """
163
    Update the fields of a subnet
162
    """Update the fields of a subnet
164 163
    Only the name can be updated
164

  
165 165
    """
166 166
    log.info('Update subnet %s, name %s' % (sub_id, name))
167 167

  
......
179 179

  
180 180

  
181 181
#Utility functions
182
def subnet_to_dict(subnet):
183
    """Returns a dictionary containing the info of a subnet"""
184
    dns = check_empty_lists(subnet.dns_nameservers)
185
    hosts = check_empty_lists(subnet.host_routes)
186
    allocation_pools = subnet.ip_pools.all()
187
    pools = list()
188

  
189
    if allocation_pools:
190
        for pool in allocation_pools:
191
            cidr = IPNetwork(pool.base)
192
            start = str(cidr.network + pool.offset)
193
            end = str(cidr.network + pool.offset + pool.size - 1)
194
            pools.append({"start": start, "end": end})
195

  
196
    dictionary = dict({'id': str(subnet.id),
197
                       'network_id': str(subnet.network.id),
198
                       'name': subnet.name if subnet.name is not None else "",
199
                       'tenant_id': subnet.network.userid,
200
                       'user_id': subnet.network.userid,
201
                       'gateway_ip': subnet.gateway,
202
                       'ip_version': subnet.ipversion,
203
                       'cidr': subnet.cidr,
204
                       'enable_dhcp': subnet.dhcp,
205
                       'dns_nameservers': dns,
206
                       'host_routes': hosts,
207
                       'allocation_pools': pools if pools is not None else []})
208

  
209
    if subnet.ipversion == 6:
210
        dictionary['enable_slac'] = subnet.dhcp
211

  
212
    return dictionary
213

  
214

  
215
def string_to_ipaddr(pools):
216
    """
217
    Convert [["192.168.42.1", "192.168.42.15"],
218
            ["192.168.42.30", "192.168.42.60"]]
219
    to
220
            [[IPv4Address('192.168.42.1'), IPv4Address('192.168.42.15')],
221
            [IPv4Address('192.168.42.30'), IPv4Address('192.168.42.60')]]
222
    and sort the output
223
    """
224
    pool_list = [(map(lambda ip_str: IPAddress(ip_str), pool))
225
                 for pool in pools]
226
    pool_list.sort()
227
    return pool_list
228

  
229

  
230 182
def create_ip_pools(pools, cidr, subnet):
231 183
    """Create IP Pools in the database"""
232 184
    for pool in pools:
......
266 218
    return name
267 219

  
268 220

  
269
def check_for_hosts_dns(subnet):
270
    """
271
    Check if a request contains host_routes or dns_nameservers options
272
    Expects the request in a dictionary format
273
    """
274
    if subnet.get('host_routes', None):
275
        raise api.faults.BadRequest("Setting host routes isn't supported")
276
    if subnet.get('dns_nameservers', None):
277
        raise api.faults.BadRequest("Setting dns nameservers isn't supported")
278

  
279

  
280 221
def get_subnet_fromdb(subnet_id, user_id, for_update=False):
281
    """
282
    Return a Subnet instance or raise ItemNotFound.
222
    """Return a Subnet instance or raise ItemNotFound.
283 223
    This is the same as util.get_network
224

  
284 225
    """
285 226
    try:
286 227
        subnet_id = int(subnet_id)
......
293 234
        raise api.faults.ItemNotFound('Subnet not found')
294 235

  
295 236

  
296
def parse_ip_pools(pools):
297
    """
298
    Convert [{'start': '192.168.42.1', 'end': '192.168.42.15'},
299
             {'start': '192.168.42.30', 'end': '192.168.42.60'}]
300
    to
301
            [["192.168.42.1", "192.168.42.15"],
302
             ["192.168.42.30", "192.168.42.60"]]
303
    """
304
    pool_list = list()
305
    for pool in pools:
306
        parse = [pool["start"], pool["end"]]
307
        pool_list.append(parse)
308
    return pool_list
309

  
310

  
311 237
def validate_subpools(pool_list, cidr, gateway):
312
    """
238
    """Validate IP Pools
239

  
313 240
    Validate the given IP pools are inside the cidr range
314 241
    Validate there are no overlaps in the given pools
315 242
    Finally, validate the gateway isn't in the given ip pools
......
317 244
    ipaddr.IPAddress items eg.,
318 245
    [[IPv4Address('192.168.42.11'), IPv4Address('192.168.42.15')],
319 246
     [IPv4Address('192.168.42.30'), IPv4Address('192.168.42.60')]]
247

  
320 248
    """
321 249
    if pool_list[0][0] <= cidr.network:
322 250
        raise api.faults.Conflict("IP Pool out of bounds")

Also available in: Unified diff