Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / db / query.py @ 435bb7fb

History | View | Annotate | Download (1.4 kB)

1
from synnefo.db.models import IPAddress
2

    
3

    
4
def get_server_ips(server, for_update=False):
5
    ips = IPAddress.objects.select_related("subnet")
6
    ips = ips.filter(nic__machine=server, deleted=False)
7
    if for_update:
8
        ips = ips.select_for_update()
9
    return ips
10

    
11

    
12
def get_server_active_ips(server, for_update=False):
13
    ips = get_server_ips(server, for_update=for_update)
14
    return ips.filter(nic__state="ACTIVE")
15

    
16

    
17
def get_server_public_ip(server, version=4):
18
    ips = get_server_active_ips(server)
19
    try:
20
        public_ips = ips.filter(network__public=True,
21
                                subnet__ipversion=version)
22
        return public_ips[0].address
23
    except IndexError:
24
        return None
25

    
26

    
27
def get_floating_ips(for_update=False):
28
    ips = IPAddress.objects.select_related("subnet")
29
    ips = ips.filter(floating_ip=True, deleted=False)
30
    if for_update:
31
        ips = ips.select_for_update()
32
    return ips
33

    
34

    
35
def get_server_floating_ips(server, for_update=False):
36
    floating_ips = get_floating_ips(for_update=for_update)
37
    return floating_ips.filter(nic__machine=server)
38

    
39

    
40
def get_server_floating_ip(server, address, for_update=False):
41
    server_fips = get_server_floating_ips(server, for_update=for_update)
42
    return server_fips.get(address=address)
43

    
44

    
45
def get_user_floating_ip(userid, address, for_update=False):
46
    fips = get_floating_ips(for_update=for_update)
47
    return fips.get(userid=userid, address=address)