Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / project-control.py @ 09166ef0

History | View | Annotate | Download (5.3 kB)

1
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from optparse import make_option
35

    
36
from django.db import transaction
37
from snf_django.management.commands import SynnefoCommand, CommandError
38
from snf_django.management import utils
39
from astakos.im.functions import (terminate, suspend, unsuspend,
40
                                  reinstate, check_expiration,
41
                                  approve_application, deny_application)
42

    
43

    
44
class Command(SynnefoCommand):
45
    help = "Manage projects and applications"
46

    
47
    option_list = SynnefoCommand.option_list + (
48
        make_option('--approve',
49
                    dest='approve',
50
                    metavar='<application id>',
51
                    help="Approve a project application"),
52
        make_option('--deny',
53
                    dest='deny',
54
                    metavar='<application id>',
55
                    help="Deny a project application"),
56
        make_option('--terminate',
57
                    dest='terminate',
58
                    metavar='<project id>',
59
                    help="Terminate a project"),
60
        make_option('--suspend',
61
                    dest='suspend',
62
                    metavar='<project id>',
63
                    help="Suspend a project"),
64
        make_option('--unsuspend',
65
                    dest='unsuspend',
66
                    metavar='<project id>',
67
                    help="Resume a suspended project"),
68
        make_option('--reinstate',
69
                    dest='reinstate',
70
                    metavar='<project id>',
71
                    help=("Resume a terminated project; this will fail if its "
72
                          "name has been reserved by another project")),
73
        make_option('--check-expired',
74
                    action='store_true',
75
                    dest='check_expired',
76
                    default=False,
77
                    help="Check projects for expiration"),
78
        make_option('--terminate-expired',
79
                    action='store_true',
80
                    dest='terminate_expired',
81
                    default=False,
82
                    help="Terminate all expired projects"),
83
        make_option('--message', '-m',
84
                    dest='message',
85
                    metavar='<msg>',
86
                    help=("Specify reason of action, "
87
                          "e.g. when denying a project")),
88
    )
89

    
90
    @transaction.commit_on_success
91
    def handle(self, *args, **options):
92

    
93
        self.output_format = options["output_format"]
94
        message = options['message']
95

    
96
        actions = {
97
            'terminate': terminate,
98
            'reinstate': reinstate,
99
            'unsuspend': unsuspend,
100
            'suspend': suspend,
101
            'approve': approve_application,
102
            'deny': lambda a: deny_application(a, reason=message),
103
            'check_expired': lambda _: self.expire(execute=False),
104
            'terminate_expired': lambda _: self.expire(execute=True),
105
        }
106

    
107
        opts = [(key, value)
108
                for (key, value) in options.items()
109
                if key in actions and value]
110

    
111
        if len(opts) != 1:
112
            raise CommandError("Specify exactly one operation.")
113

    
114
        key, value = opts[0]
115
        action = actions[key]
116
        try:
117
            action(value)
118
        except BaseException as e:
119
            raise CommandError(e)
120

    
121
    def print_expired(self, projects, execute):
122
        length = len(projects)
123
        if length == 0:
124
            s = 'No expired projects.\n'
125
            self.stderr.write(s)
126
            return
127
        labels = ('Project', 'Name', 'Status', 'Expiration date')
128
        utils.pprint_table(self.stdout, projects, labels,
129
                           self.output_format, title="Expired projects")
130

    
131
        if execute:
132
            self.stderr.write('%d projects have been terminated.\n' % (
133
                    length,))
134

    
135
    def expire(self, execute=False):
136
        projects = check_expiration(execute=execute)
137
        self.print_expired(projects, execute)