Revision 69dadbe4 snf-cyclades-app/synnefo/api/util.py

b/snf-cyclades-app/synnefo/api/util.py
49 49
from synnefo.db.models import (Flavor, VirtualMachine, VirtualMachineMetadata,
50 50
                               Network, NetworkInterface, SecurityGroup,
51 51
                               BridgePoolTable, MacPrefixPoolTable, IPAddress,
52
                               IPPoolTable)
52
                               IPPoolTable, Subnet)
53 53
from synnefo.db import pools
54 54

  
55 55
from synnefo.plankton.utils import image_backend
......
226 226
        raise faults.ItemNotFound('Network %s not found.' % network_id)
227 227

  
228 228

  
229
def get_subnet(subnet_id, user_id, for_update=False, public=True,
230
               non_deleted=False):
231
    """Return a Subnet instance or raise ItemNotFound."""
232

  
233
    try:
234
        subnet_id = int(subnet_id)
235
        objects = Subnet.objects
236
        if for_update:
237
            objects = objects.select_for_update()
238
        if public:
239
            subnet = objects.get(Q(network__userid=user_id) |
240
                                 Q(network__public=True), id=subnet_id)
241
        else:
242
            subnet = objects.get(network__userid=user_id, id=subnet_id)
243
        if non_deleted and subnet.deleted:
244
            raise faults.BadRequest("Subnet has been deleted.")
245
        return subnet
246
    except (ValueError, Subnet.DoesNotExist):
247
        raise faults.ItemNotFound('Subnet %s not found.' % subnet_id)
248

  
249

  
229 250
def get_port(port_id, user_id, for_update=False):
230 251
    """
231 252
    Return a NetworkInteface instance or raise ItemNotFound.
......
247 268
    except (ValueError, NetworkInterface.DoesNotExist):
248 269
        raise faults.ItemNotFound('Port not found.')
249 270

  
271

  
250 272
def get_security_group(sg_id):
251 273
    try:
252 274
        sg = SecurityGroup.objects.get(id=sg_id)
......
254 276
    except (ValueError, SecurityGroup.DoesNotExist):
255 277
        raise faults.ItemNotFound("Not valid security group")
256 278

  
279

  
257 280
def get_floating_ip_by_address(userid, address, for_update=False):
258 281
    try:
259 282
        objects = IPAddress.objects

Also available in: Unified diff