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

from optparse import make_option

from django.core.management.base import BaseCommand, CommandError

from astakos.im.models import Service


class Command(BaseCommand):
    args = "<service ID>"
    help = "Modify service attributes"

    option_list = BaseCommand.option_list + (
46
47
48
49
        make_option('--url',
                    dest='url',
                    default=None,
                    help="Set service url"),
50
51
        make_option('--api-url',
                    dest='api_url',
52
                    default=None,
53
                    help="Set service API url"),
54
55
56
57
        make_option('--auth-token',
                    dest='auth_token',
                    default=None,
                    help="Set a custom service auth token"),
58
        make_option('--renew-token',
59
60
61
62
                    action='store_true',
                    dest='renew_token',
                    default=False,
                    help="Renew service auth token"),
63
64
65
66
        make_option('--type',
                    dest='type',
                    default=None,
                    help="Modify service type"),
67
68
69
70
71
72
73
74
75
    )

    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError("Please provide a service ID")

        try:
            service = Service.objects.get(id=int(args[0]))
        except Service.DoesNotExist:
76
77
78
            raise CommandError(
                "Service does not exist. You may run snf-manage "
                "service-list for available service IDs.")
79

80
        api_url = options.get('api_url')
81
        url = options.get('url')
82
83
        auth_token = options.get('auth_token')
        renew_token = options.get('renew_token')
84
        type = options.get('type')
85

86
87
        if api_url:
            service.api_url = api_url
88

89
90
91
        if url:
            service.url = url

92
93
94
95
96
97
        if auth_token:
            service.auth_token = auth_token

        if renew_token and not auth_token:
            service.renew_token()

98
99
100
        if type:
            service.type = type

101
102
        service.save()

103
104
105
106
        if renew_token:
            self.stdout.write(
                'Service\'s new token: %s\n' % service.auth_token
            )