Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.2 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 = \
42
"""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('--name',
61
            dest='name',
62
            metavar='NAME',
63
            help="Set network's name"),
64
        make_option('--userid',
65
            dest='userid',
66
            help="Set the userid of the network owner"),
67
        make_option('--subnet',
68
            dest='subnet',
69
            help="Set network's subnet"),
70
        make_option('--gateway',
71
            dest='gateway',
72
            help="Set network's gateway"),
73
        make_option('--subnet6',
74
            dest='subnet6',
75
            help="Set network's IPv6 subnet"),
76
        make_option('--gateway6',
77
            dest='gateway6',
78
            help="Set network's IPv6 gateway"),
79
        make_option('--dhcp',
80
            dest='dhcp',
81
            help="Set if network will use nfdhcp"),
82
        make_option('--state',
83
            dest='state',
84
            metavar='STATE',
85
            help="Set network's state"),
86
        make_option('--link',
87
            dest='link',
88
            help="Set the connectivity link"),
89
        make_option('--mac-prefix',
90
            dest="mac_prefix",
91
            help="Set the MAC prefix"),
92
        make_option('--add-reserved-ips',
93
            dest="add_reserved_ips",
94
            help="Comma seperated list of IPs to externally reserve."),
95
        make_option('--remove-reserved-ips',
96
            dest="remove_reserved_ips",
97
            help="Comma seperated list of IPs to externally release."),
98

    
99
    )
100

    
101
    def handle(self, *args, **options):
102
        if len(args) != 1:
103
            raise CommandError("Please provide a network ID")
104

    
105
        network = get_network(args[0])
106

    
107
        # Validate subnet
108
        if options.get('subnet'):
109
            validate_network_info(options)
110

    
111
        # Validate state
112
        state = options.get('state')
113
        if state:
114
            allowed = [x[0] for x in Network.OPER_STATES]
115
            if state not in allowed:
116
                msg = "Invalid state, must be one of %s" % ', '.join(allowed)
117
                raise CommandError(msg)
118

    
119
        fields = ('name', 'userid', 'subnet', 'gateway', 'subnet6', 'gateway6',
120
                  'dhcp', 'state', 'link', 'mac_prefix')
121
        for field in fields:
122
            value = options.get(field, None)
123
            if value:
124
                network.__setattr__(field, value)
125

    
126
        add_reserved_ips = options.get('add_reserved_ips')
127
        remove_reserved_ips = options.get('remove_reserved_ips')
128
        if add_reserved_ips or remove_reserved_ips:
129
            if add_reserved_ips:
130
                add_reserved_ips = add_reserved_ips.split(",")
131
            if remove_reserved_ips:
132
                remove_reserved_ips = remove_reserved_ips.split(",")
133

    
134
        for bnetwork in network.backend_networks.all():
135
            with pooled_rapi_client(bnetwork.backend) as c:
136
                c.ModifyNetwork(network=network.backend_id,
137
                                add_reserved_ips=add_reserved_ips,
138
                                remove_reserved_ips=remove_reserved_ips)
139

    
140
        network.save()