Revision f83ebc98 snf-cyclades-app/synnefo/logic/management/commands/backend-modify.py

b/snf-cyclades-app/synnefo/logic/management/commands/backend-modify.py
33 33

  
34 34
from optparse import make_option
35 35
from django.core.management.base import BaseCommand, CommandError
36
from synnefo.management.common import get_backend, check_backend_credentials
36
from synnefo.management.common import (get_backend, check_backend_credentials,
37
                                       parse_bool)
37 38

  
38 39

  
39 40
class Command(BaseCommand):
......
56 57
            help="Set backend's password"),
57 58
        make_option('--drained',
58 59
            dest='drained',
59
            action='store_true',
60
            default=False,
60
            choices=["True", "False"],
61
            metavar="True|False",
61 62
            help="Set the backend as drained to exclude from allocation "\
62 63
                 "operations"),
63
        make_option('--no-drained',
64
            dest='drained',
65
            action='store_false'),
66 64
        make_option('--offline',
67 65
            dest='offline',
68
            action='store_true',
69
            default=False,
66
            choices=["True", "False"],
67
            metavar="True|False",
70 68
            help="Set the backend as offline to not communicate in order "\
71 69
                 "to avoid delays"),
72
        make_option('--no-offline',
73
            dest='offline',
74
            action='store_false')
75 70
        )
76 71

  
77 72
    def handle(self, *args, **options):
......
81 76
        backend = get_backend(args[0])
82 77

  
83 78
        # Ensure fields correspondence with options and Backend model
84
        fields = ('clustername', 'port', 'username', 'password', 'drained',
85
                  'offline')
79
        credentials_changed = False
80
        fields = ('clustername', 'port', 'username', 'password')
86 81
        for field in fields:
87 82
            value = options.get(field)
88 83
            if value is not None:
89 84
                backend.__setattr__(field, value)
85
                credentials_changed = True
90 86

  
91
        credentials = ('clustername', 'port', 'username', 'password')
92
        for field in credentials:
93
            if options.get(field):
87
        if credentials_changed:
94 88
                # check credentials, if any of them changed!
95 89
                check_backend_credentials(backend.clustername, backend.port,
96 90
                                          backend.username, backend.password)
97
                return
91
        if options['drained']:
92
            backend.drained = parse_bool(options['drained'])
93
        if options['offline']:
94
            backend.offile = parse_bool(options['offline'])
98 95

  
99 96
        backend.save()

Also available in: Unified diff