Revision 1a894bfe snf-cyclades-app/synnefo/api/management/commands/listservers.py

b/snf-cyclades-app/synnefo/api/management/commands/listservers.py
36 36
from django.core.management.base import BaseCommand, CommandError
37 37

  
38 38
from synnefo.api.util import get_image
39
from synnefo.db.models import VirtualMachine
39
from synnefo.db.models import VirtualMachine, Backend
40 40

  
41 41

  
42 42
class Command(BaseCommand):
......
53 53
            dest='build',
54 54
            default=False,
55 55
            help="List only servers in the building state"),
56
        make_option('--non-deleted', action='store_true', dest='non_deleted',
57
                    default=False,
58
                    help="List only non-deleted servers"),
59
        make_option('--backend_id', dest='backend_id',
60
                    help="List only servers of the specified backend")
56 61
        )
57 62
    
58 63
    def handle(self, *args, **options):
59 64
        if args:
60 65
            raise CommandError("Command doesn't accept any arguments")
61
        
62
        servers = VirtualMachine.objects.all()
66

  
67
        if options['backend_id']:
68
            servers = \
69
            Backend.objects.get(id=options['backend_id']).virtual_machines
70
        else:
71
            servers = VirtualMachine.objects
72

  
73
        if options['non_deleted']:
74
            servers = servers.filter(deleted=False)
75
        else:
76
            servers = servers.all()
77

  
63 78
        if options['build']:
64 79
            servers = servers.filter(operstate='BUILD')
65 80
        
66
        labels = ('id', 'name', 'owner', 'flavor', 'image', 'state')
67
        columns = (3, 12, 20, 11, 12, 9)
81
        labels = ('id', 'name', 'owner', 'flavor', 'image', 'state',
82
                  'backend')
83
        columns = (3, 12, 20, 11, 12, 9, 40)
68 84
        
69 85
        if not options['csv']:
70 86
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
......
83 99
                image = get_image(server.imageid, server.userid)['name']
84 100
            except:
85 101
                image = server.imageid
86
            fields = (id, name, server.userid, flavor, image, server.operstate)
102
            fields = (id, name, server.userid, flavor, image, server.operstate,
103
                      str(server.backend))
87 104
            
88 105
            if options['csv']:
89 106
                line = '|'.join(fields)

Also available in: Unified diff