Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / db / query.py @ 91884d63

History | View | Annotate | Download (2.9 kB)

1
# Copyright 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29

    
30
from synnefo.db.models import IPAddress
31

    
32

    
33
def get_server_ips(server, for_update=False):
34
    ips = IPAddress.objects.select_related("subnet")
35
    ips = ips.filter(nic__machine=server, deleted=False)
36
    if for_update:
37
        ips = ips.select_for_update()
38
    return ips
39

    
40

    
41
def get_server_active_ips(server, for_update=False):
42
    ips = get_server_ips(server, for_update=for_update)
43
    return ips.filter(nic__state="ACTIVE")
44

    
45

    
46
def get_server_public_ip(server, version=4):
47
    ips = get_server_active_ips(server)
48
    try:
49
        public_ips = ips.filter(network__public=True,
50
                                subnet__ipversion=version)
51
        return public_ips[0].address
52
    except IndexError:
53
        return None
54

    
55

    
56
def get_floating_ips(for_update=False):
57
    ips = IPAddress.objects.select_related("subnet")
58
    ips = ips.filter(floating_ip=True, deleted=False)
59
    if for_update:
60
        ips = ips.select_for_update()
61
    return ips
62

    
63

    
64
def get_server_floating_ips(server, for_update=False):
65
    floating_ips = get_floating_ips(for_update=for_update)
66
    return floating_ips.filter(nic__machine=server)
67

    
68

    
69
def get_server_floating_ip(server, address, for_update=False):
70
    server_fips = get_server_floating_ips(server, for_update=for_update)
71
    return server_fips.get(address=address)
72

    
73

    
74
def get_user_floating_ip(userid, address, for_update=False):
75
    fips = get_floating_ips(for_update=for_update)
76
    return fips.get(userid=userid, address=address)