Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / server-modify.py @ 39a6388d

History | View | Annotate | Download (3.9 kB)

1
# Copyright 2012 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.core.management.base import BaseCommand, CommandError
37

    
38
from synnefo.db.models import VirtualMachine
39

    
40

    
41
class Command(BaseCommand):
42
    args = "<server ID>"
43
    help = "Modify a server"
44

    
45
    option_list = BaseCommand.option_list + (
46
        make_option('--name',
47
            dest='name',
48
            metavar='NAME',
49
            help="Set server's name"),
50
        make_option('--owner',
51
            dest='owner',
52
            metavar='USER_ID',
53
            help="Set server's owner"),
54
        make_option('--state',
55
            dest='state',
56
            metavar='STATE',
57
            help="Set server's state"),
58
        make_option('--set-deleted',
59
            action='store_true',
60
            dest='deleted',
61
            help="Mark a server as deleted"),
62
        make_option('--set-undeleted',
63
            action='store_true',
64
            dest='undeleted',
65
            help="Mark a server as not deleted"),
66
        make_option('--set-suspended',
67
            action='store_true',
68
            dest='suspended',
69
            help="Mark a server as suspended"),
70
        make_option('--set-unsuspended',
71
            action='store_true',
72
            dest='unsuspended',
73
            help="Mark a server as not suspended")
74
        )
75

    
76
    def handle(self, *args, **options):
77
        if len(args) != 1:
78
            raise CommandError("Please provide a server ID")
79

    
80
        try:
81
            server_id = int(args[0])
82
            server = VirtualMachine.objects.get(id=server_id)
83
        except (ValueError, VirtualMachine.DoesNotExist):
84
            raise CommandError("Invalid server ID")
85

    
86
        name = options.get('name')
87
        if name is not None:
88
            server.name = name
89

    
90
        owner = options.get('owner')
91
        if owner is not None:
92
            server.userid = owner
93

    
94
        state = options.get('state')
95
        if state is not None:
96
            allowed = [x[0] for x in VirtualMachine.OPER_STATES]
97
            if state not in allowed:
98
                msg = "Invalid state, must be one of %s" % ', '.join(allowed)
99
                raise CommandError(msg)
100
            server.operstate = state
101

    
102
        if options.get('deleted'):
103
            server.deleted = True
104
        elif options.get('undeleted'):
105
            server.deleted = False
106

    
107
        if options.get('suspended'):
108
            server.suspended = True
109
        elif options.get('unsuspended'):
110
            server.suspended = False
111

    
112
        server.save()