Revision 90b67503 snf-cyclades-app/synnefo/api/management/commands/server-list.py

b/snf-cyclades-app/synnefo/api/management/commands/server-list.py
41 41

  
42 42
class Command(BaseCommand):
43 43
    help = "List servers"
44
    
44

  
45 45
    option_list = BaseCommand.option_list + (
46 46
        make_option('-c',
47 47
            action='store_true',
......
59 59
        make_option('--backend_id', dest='backend_id',
60 60
                    help="List only servers of the specified backend")
61 61
        )
62
    
62

  
63 63
    def handle(self, *args, **options):
64 64
        if args:
65 65
            raise CommandError("Command doesn't accept any arguments")
66 66

  
67 67
        if options['backend_id']:
68
            servers = \
69
            Backend.objects.get(id=options['backend_id']).virtual_machines
68
            try:
69
                servers = Backend.objects.get(id=options['backend_id'])\
70
                                         .virtual_machines
71
            except Backend.DoesNotExist:
72
                raise CommandError("Backend not found in DB")
70 73
        else:
71 74
            servers = VirtualMachine.objects
72 75

  
......
77 80

  
78 81
        if options['build']:
79 82
            servers = servers.filter(operstate='BUILD')
80
        
83

  
81 84
        labels = ('id', 'name', 'owner', 'flavor', 'image', 'state',
82 85
                  'backend')
83 86
        columns = (3, 12, 20, 11, 12, 9, 40)
84
        
87

  
85 88
        if not options['csv']:
86 89
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
87 90
            self.stdout.write(line + '\n')
88 91
            sep = '-' * len(line)
89 92
            self.stdout.write(sep + '\n')
90
        
91
        for server in servers:
93

  
94
        for server in servers.order_by('id'):
92 95
            id = str(server.id)
93 96
            try:
94 97
                name = server.name.decode('utf8')
......
101 104
                image = server.imageid
102 105
            fields = (id, name, server.userid, flavor, image, server.operstate,
103 106
                      str(server.backend))
104
            
107

  
105 108
            if options['csv']:
106 109
                line = '|'.join(fields)
107 110
            else:
108 111
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
109
            
112

  
110 113
            self.stdout.write(line.encode('utf8') + '\n')

Also available in: Unified diff