Revision 4500650c snf-cyclades-app/synnefo/api/management/commands/server-list.py

b/snf-cyclades-app/synnefo/api/management/commands/server-list.py
34 34
from optparse import make_option
35 35

  
36 36
from django.core.management.base import BaseCommand, CommandError
37
from synnefo.management.common import (format_vm_state, get_backend,
37
from synnefo.management.common import (format_vm_state, get_backend, Omit,
38 38
                                       filter_results, pprint_table, UUIDCache)
39 39
from synnefo.api.util import get_image
40 40
from synnefo.db.models import VirtualMachine
......
72 72
        make_option('--backend-id',
73 73
            dest='backend_id',
74 74
            help="List only servers of the specified backend"),
75
        make_option('--user',
76
            dest='user',
77
            help="List only servers of the specified user (uuid or email)"),
75 78
        make_option('--filter-by',
76 79
            dest='filter_by',
77 80
            help="Filter results. Comma seperated list of key `cond` val pairs"
78 81
                 " that displayed entries must satisfy. e.g."
79 82
                 " --filter-by \"operstate=STARTED,id>=22\"."
80 83
                 " Available keys are: %s" % ", ".join(FIELDS)),
81
        make_option(
82
            '--uuids',
84
        make_option('--displayname',
83 85
            action='store_true',
84
            dest='use_uuids',
86
            dest='displayname',
85 87
            default=False,
86
            help="Display UUIDs instead of user emails"),
88
            help="Display both uuid and display name"),
87 89
    )
88 90

  
89 91
    def handle(self, *args, **options):
90 92
        if args:
91 93
            raise CommandError("Command doesn't accept any arguments")
92 94

  
93
        use_uuids = options["use_uuids"]
95
        ucache = UUIDCache()
96

  
94 97
        if options['backend_id']:
95 98
            backend = get_backend(options['backend_id'])
96 99
            servers = backend.virtual_machines
......
108 111
        if options['build']:
109 112
            servers = servers.filter(operstate='BUILD')
110 113

  
114
        user = options['user']
115
        if user:
116
            if '@' in user:
117
                user = ucache.get_user(user)
118
            servers = servers.filter(userid=user)
119

  
111 120
        filter_by = options['filter_by']
112 121
        if filter_by:
113 122
            servers = filter_results(servers, filter_by)
114 123

  
124
        displayname = options['displayname']
125

  
115 126
        cache = ImageCache()
116
        if not use_uuids:
117
            ucache = UUIDCache()
118 127

  
119
        headers = ('id', 'name', 'owner', 'flavor', 'image', 'state',
120
                   'backend')
128
        headers = filter(lambda x: x is not Omit,
129
                        ['id',
130
                         'name',
131
                         'owner_uuid',
132
                         'owner_name' if displayname else Omit,
133
                         'flavor',
134
                         'image',
135
                         'state',
136
                         'backend',
137
                          ])
121 138

  
122 139
        table = []
123 140
        for server in servers.order_by('id'):
......
132 149

  
133 150
            state = format_vm_state(server)
134 151

  
135
            user = server.userid
136
            if not use_uuids:
137
                user = ucache.get_user(server.userid)
138

  
139
            fields = (str(server.id), name, user, flavor, image,
140
                      state, str(server.backend))
152
            uuid = server.userid
153
            if displayname:
154
                dname = ucache.get_user(server.userid)
155

  
156
            fields = filter(lambda x: x is not Omit,
157
                            [str(server.id),
158
                             name,
159
                             uuid,
160
                             dname if displayname else Omit,
161
                             flavor,
162
                             image,
163
                             state,
164
                             str(server.backend),
165
                            ])
141 166
            table.append(fields)
142 167

  
143 168
        separator = " | " if options['csv'] else None

Also available in: Unified diff