Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / component-remove.py @ 09166ef0

History | View | Annotate | Download (2.4 kB)

1 a7f3f2a4 Giorgos Korfiatis
# Copyright 2013 GRNET S.A. All rights reserved.
2 a7f3f2a4 Giorgos Korfiatis
#
3 a7f3f2a4 Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 a7f3f2a4 Giorgos Korfiatis
# without modification, are permitted provided that the following
5 a7f3f2a4 Giorgos Korfiatis
# conditions are met:
6 a7f3f2a4 Giorgos Korfiatis
#
7 a7f3f2a4 Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 a7f3f2a4 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 a7f3f2a4 Giorgos Korfiatis
#      disclaimer.
10 a7f3f2a4 Giorgos Korfiatis
#
11 a7f3f2a4 Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 a7f3f2a4 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 a7f3f2a4 Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 a7f3f2a4 Giorgos Korfiatis
#      provided with the distribution.
15 a7f3f2a4 Giorgos Korfiatis
#
16 a7f3f2a4 Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 a7f3f2a4 Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 a7f3f2a4 Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 a7f3f2a4 Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 a7f3f2a4 Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 a7f3f2a4 Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 a7f3f2a4 Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 a7f3f2a4 Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 a7f3f2a4 Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 a7f3f2a4 Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 a7f3f2a4 Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 a7f3f2a4 Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 a7f3f2a4 Giorgos Korfiatis
#
29 a7f3f2a4 Giorgos Korfiatis
# The views and conclusions contained in the software and
30 a7f3f2a4 Giorgos Korfiatis
# documentation are those of the authors and should not be
31 a7f3f2a4 Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 a7f3f2a4 Giorgos Korfiatis
# or implied, of GRNET S.A.
33 a7f3f2a4 Giorgos Korfiatis
34 a6d167c1 Giorgos Korfiatis
from snf_django.management.commands import SynnefoCommand, CommandError
35 a3e3917f Giorgos Korfiatis
from django.db import transaction
36 a7f3f2a4 Giorgos Korfiatis
from astakos.im.models import Component
37 a7f3f2a4 Giorgos Korfiatis
38 a7f3f2a4 Giorgos Korfiatis
39 a6d167c1 Giorgos Korfiatis
class Command(SynnefoCommand):
40 a7f3f2a4 Giorgos Korfiatis
    args = "<component ID or name>"
41 a7f3f2a4 Giorgos Korfiatis
    help = "Remove a component along with its registered services"
42 a7f3f2a4 Giorgos Korfiatis
43 a3e3917f Giorgos Korfiatis
    @transaction.commit_on_success
44 a7f3f2a4 Giorgos Korfiatis
    def handle(self, *args, **options):
45 a7f3f2a4 Giorgos Korfiatis
        if len(args) != 1:
46 a7f3f2a4 Giorgos Korfiatis
            raise CommandError("Please provide a component ID or name")
47 a7f3f2a4 Giorgos Korfiatis
48 a7f3f2a4 Giorgos Korfiatis
        ident = args[0]
49 a7f3f2a4 Giorgos Korfiatis
        try:
50 a7f3f2a4 Giorgos Korfiatis
            try:
51 a7f3f2a4 Giorgos Korfiatis
                ident = int(ident)
52 a7f3f2a4 Giorgos Korfiatis
                component = Component.objects.get(id=ident)
53 a7f3f2a4 Giorgos Korfiatis
            except ValueError:
54 a7f3f2a4 Giorgos Korfiatis
                component = Component.objects.get(name=ident)
55 a7f3f2a4 Giorgos Korfiatis
        except Component.DoesNotExist:
56 a7f3f2a4 Giorgos Korfiatis
            raise CommandError(
57 a7f3f2a4 Giorgos Korfiatis
                "Component does not exist. You may run snf-manage "
58 a7f3f2a4 Giorgos Korfiatis
                "component-list for available component IDs.")
59 a7f3f2a4 Giorgos Korfiatis
60 a7f3f2a4 Giorgos Korfiatis
        component.delete()