Revision 2052c40e snf-cyclades-app/synnefo/logic/management/commands/server-list.py

b/snf-cyclades-app/synnefo/logic/management/commands/server-list.py
32 32
# or implied, of GRNET S.A.
33 33

  
34 34
from optparse import make_option
35
from functools import partial
35 36

  
36 37
from snf_django.management.commands import ListCommand
37 38
from synnefo.db.models import VirtualMachine
......
77 78
    astakos_auth_url = ASTAKOS_AUTH_URL
78 79
    astakos_token = ASTAKOS_TOKEN
79 80

  
80
    def get_ipv4(vm):
81
        return vm.nics.filter(ips__subnet__ipversion=4)\
82
                      .values_list("ips__address", flat=True)
83

  
84
    def get_ipv6(vm):
85
        return vm.nics.filter(ips__subnet__ipversion=6)\
86
                      .values_list("ips__address", flat=True)
81
    def get_ips(version, vm):
82
        ips = []
83
        for nic in vm.nics.all():
84
            for ip in nic.ips.all():
85
                if ip.subnet.ipversion == version:
86
                    ips.append(ip.address)
87
        return ips
87 88

  
88 89
    def format_vm_state(vm):
89 90
        if vm.operstate == "BUILD":
......
100 101
        "image.id": ("imageid", "The ID of the server's image"),
101 102
        "image.name": ("image", "The name of the server's image"),
102 103
        "state": (format_vm_state, "The current state of the server"),
103
        "ipv4": (get_ipv4, "The IPv4 addresses of the server"),
104
        "ipv6": (get_ipv6, "The IPv6 addresses of the server"),
104
        "ipv4": (partial(get_ips, 4),
105
                 "The IPv4 addresses of the server"),
106
        "ipv6": (partial(get_ips, 6),
107
                 "The IPv6 addresses of the server"),
105 108
        "created": ("created", "The date the server was created"),
106 109
        "deleted": ("deleted", "Whether the server is deleted or not"),
107 110
        "suspended": ("suspended", "Whether the server is administratively"
......
126 129
            self.fields = ["image.name" if x == "image.id" else x
127 130
                           for x in self.fields]
128 131

  
132
        if "ipv4" in self.fields or "ipv6" in self.fields:
133
            self.prefetch_related.append("nics__ips__subnet")
134

  
129 135
    def handle_db_objects(self, rows, *args, **kwargs):
130 136
        if "image.name" in self.fields:
131 137
            icache = ImageCache()

Also available in: Unified diff