Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.4 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

    
41
HELP_MSG = """Modify a network.
42

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

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

    
52

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

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

    
110
    )
111

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

    
116
        network = get_network(args[0])
117

    
118
        # Validate subnet
119
        if options.get('subnet'):
120
            validate_network_info(options)
121

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

    
130
        fields = ('name', 'userid', 'subnet', 'gateway', 'subnet6', 'gateway6',
131
                  'dhcp', 'state', 'link', 'mac_prefix')
132
        for field in fields:
133
            value = options.get(field, None)
134
            if value:
135
                network.__setattr__(field, value)
136

    
137
        add_reserved_ips = options.get('add_reserved_ips')
138
        remove_reserved_ips = options.get('remove_reserved_ips')
139
        if add_reserved_ips or remove_reserved_ips:
140
            if add_reserved_ips:
141
                add_reserved_ips = add_reserved_ips.split(",")
142
            if remove_reserved_ips:
143
                remove_reserved_ips = remove_reserved_ips.split(",")
144

    
145
        for bnetwork in network.backend_networks.all():
146
            with pooled_rapi_client(bnetwork.backend) as c:
147
                c.ModifyNetwork(network=network.backend_id,
148
                                add_reserved_ips=add_reserved_ips,
149
                                remove_reserved_ips=remove_reserved_ips)
150

    
151
        network.save()