Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / modifyflavor.py @ af6a3bc5

History | View | Annotate | Download (2.6 kB)

1 76f5441f Giorgos Verigakis
# Copyright 2012 GRNET S.A. All rights reserved.
2 76f5441f Giorgos Verigakis
#
3 76f5441f Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 76f5441f Giorgos Verigakis
# without modification, are permitted provided that the following
5 76f5441f Giorgos Verigakis
# conditions are met:
6 76f5441f Giorgos Verigakis
#
7 76f5441f Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 76f5441f Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 76f5441f Giorgos Verigakis
#      disclaimer.
10 76f5441f Giorgos Verigakis
#
11 76f5441f Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 76f5441f Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 76f5441f Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 76f5441f Giorgos Verigakis
#      provided with the distribution.
15 76f5441f Giorgos Verigakis
#
16 76f5441f Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 76f5441f Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 76f5441f Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 76f5441f Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 76f5441f Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 76f5441f Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 76f5441f Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 76f5441f Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 76f5441f Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 76f5441f Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 76f5441f Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 76f5441f Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 76f5441f Giorgos Verigakis
#
29 76f5441f Giorgos Verigakis
# The views and conclusions contained in the software and
30 76f5441f Giorgos Verigakis
# documentation are those of the authors and should not be
31 76f5441f Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 76f5441f Giorgos Verigakis
# or implied, of GRNET S.A.
33 76f5441f Giorgos Verigakis
34 76f5441f Giorgos Verigakis
from optparse import make_option
35 76f5441f Giorgos Verigakis
36 76f5441f Giorgos Verigakis
from django.core.management.base import BaseCommand, CommandError
37 76f5441f Giorgos Verigakis
38 76f5441f Giorgos Verigakis
from synnefo.db.models import Flavor
39 76f5441f Giorgos Verigakis
40 76f5441f Giorgos Verigakis
41 76f5441f Giorgos Verigakis
class Command(BaseCommand):
42 76f5441f Giorgos Verigakis
    args = "<flavor id>"
43 76f5441f Giorgos Verigakis
    help = "Modify a flavor"
44 76f5441f Giorgos Verigakis
    
45 76f5441f Giorgos Verigakis
    option_list = BaseCommand.option_list + (
46 76f5441f Giorgos Verigakis
        make_option('--set-deleted',
47 76f5441f Giorgos Verigakis
            action='store_true',
48 76f5441f Giorgos Verigakis
            dest='deleted',
49 76f5441f Giorgos Verigakis
            help="Mark a server as deleted"),
50 76f5441f Giorgos Verigakis
        make_option('--set-undeleted',
51 76f5441f Giorgos Verigakis
            action='store_true',
52 76f5441f Giorgos Verigakis
            dest='undeleted',
53 76f5441f Giorgos Verigakis
            help="Mark a server as not deleted"),
54 76f5441f Giorgos Verigakis
        )
55 76f5441f Giorgos Verigakis
    
56 76f5441f Giorgos Verigakis
    def handle(self, *args, **options):
57 76f5441f Giorgos Verigakis
        if len(args) != 1:
58 76f5441f Giorgos Verigakis
            raise CommandError("Please provide a flavor ID")
59 76f5441f Giorgos Verigakis
        
60 76f5441f Giorgos Verigakis
        try:
61 76f5441f Giorgos Verigakis
            flavor_id = int(args[0])
62 76f5441f Giorgos Verigakis
            flavor = Flavor.objects.get(id=flavor_id)
63 76f5441f Giorgos Verigakis
        except (ValueError, Flavor.DoesNotExist):
64 76f5441f Giorgos Verigakis
            raise CommandError("Invalid flavor ID")
65 76f5441f Giorgos Verigakis
        
66 76f5441f Giorgos Verigakis
        if options.get('deleted'):
67 76f5441f Giorgos Verigakis
            flavor.deleted = True
68 76f5441f Giorgos Verigakis
        elif options.get('undeleted'):
69 76f5441f Giorgos Verigakis
            flavor.deleted = False
70 76f5441f Giorgos Verigakis
        
71 76f5441f Giorgos Verigakis
        flavor.save()