Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / network-modify.py @ 052ef3b9

History | View | Annotate | Download (5.6 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 Network, pooled_rapi_client
39
from synnefo.management.common import validate_network_info, get_network
40
from synnefo.webproject.management.utils import parse_bool
41

    
42
HELP_MSG = """Modify a network.
43

44
This management command will only modify the state of the network in Cyclades
45
DB. The state of the network in the Ganeti backends will remain unchanged. You
46
should manually modify the network in all the backends, to synchronize the
47
state of DB and Ganeti.
48

49
The only exception is add_reserved_ips and remove_reserved_ips options, which
50
modify the IP pool in the Ganeti backends.
51
"""
52

    
53

    
54
class Command(BaseCommand):
55
    args = "<network id>"
56
    help = HELP_MSG
57
    output_transaction = True
58

    
59
    option_list = BaseCommand.option_list + (
60
        make_option(
61
            '--name',
62
            dest='name',
63
            metavar='NAME',
64
            help="Set network's name"),
65
        make_option(
66
            '--userid',
67
            dest='userid',
68
            help="Set the userid of the network owner"),
69
        make_option(
70
            '--subnet',
71
            dest='subnet',
72
            help="Set network's subnet"),
73
        make_option(
74
            '--gateway',
75
            dest='gateway',
76
            help="Set network's gateway"),
77
        make_option(
78
            '--subnet6',
79
            dest='subnet6',
80
            help="Set network's IPv6 subnet"),
81
        make_option(
82
            '--gateway6',
83
            dest='gateway6',
84
            help="Set network's IPv6 gateway"),
85
        make_option(
86
            '--dhcp',
87
            dest='dhcp',
88
            metavar="True|False",
89
            choices=["True", "False"],
90
            help="Set if network will use nfdhcp"),
91
        make_option(
92
            '--state',
93
            dest='state',
94
            metavar='STATE',
95
            help="Set network's state"),
96
        make_option(
97
            '--link',
98
            dest='link',
99
            help="Set the connectivity link"),
100
        make_option(
101
            '--mac-prefix',
102
            dest="mac_prefix",
103
            help="Set the MAC prefix"),
104
        make_option(
105
            '--add-reserved-ips',
106
            dest="add_reserved_ips",
107
            help="Comma seperated list of IPs to externally reserve."),
108
        make_option(
109
            '--remove-reserved-ips',
110
            dest="remove_reserved_ips",
111
            help="Comma seperated list of IPs to externally release."),
112

    
113
    )
114

    
115
    def handle(self, *args, **options):
116
        if len(args) != 1:
117
            raise CommandError("Please provide a network ID")
118

    
119
        network = get_network(args[0])
120

    
121
        # Validate subnet
122
        if options.get('subnet'):
123
            validate_network_info(options)
124

    
125
        # Validate state
126
        state = options.get('state')
127
        if state:
128
            allowed = [x[0] for x in Network.OPER_STATES]
129
            if state not in allowed:
130
                msg = "Invalid state, must be one of %s" % ', '.join(allowed)
131
                raise CommandError(msg)
132

    
133
        dhcp = options.get("dhcp")
134
        if dhcp:
135
            options["dhcp"] = parse_bool(dhcp)
136
        fields = ('name', 'userid', 'subnet', 'gateway', 'subnet6', 'gateway6',
137
                  'dhcp', 'state', 'link', 'mac_prefix')
138
        for field in fields:
139
            value = options.get(field, None)
140
            if value is not None:
141
                network.__setattr__(field, value)
142

    
143
        add_reserved_ips = options.get('add_reserved_ips')
144
        remove_reserved_ips = options.get('remove_reserved_ips')
145
        if add_reserved_ips or remove_reserved_ips:
146
            if add_reserved_ips:
147
                add_reserved_ips = add_reserved_ips.split(",")
148
            if remove_reserved_ips:
149
                remove_reserved_ips = remove_reserved_ips.split(",")
150

    
151
        for bnetwork in network.backend_networks.all():
152
            with pooled_rapi_client(bnetwork.backend) as c:
153
                c.ModifyNetwork(network=network.backend_id,
154
                                add_reserved_ips=add_reserved_ips,
155
                                remove_reserved_ips=remove_reserved_ips)
156

    
157
        network.save()