root / snf-cyclades-app / synnefo / api / management / commands / network-modify.py @ 53b9ba10
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 _common import validate_network_info |
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 |
try:
|
106 |
network_id = int(args[0]) |
107 |
network = Network.objects.get(id=network_id) |
108 |
except ValueError: |
109 |
raise CommandError("Invalid network ID") |
110 |
except (ValueError, Network.DoesNotExist): |
111 |
raise CommandError("Network not found in DB") |
112 |
|
113 |
# Validate subnet
|
114 |
if options.get('subnet'): |
115 |
validate_network_info(options) |
116 |
|
117 |
# Validate state
|
118 |
state = options.get('state')
|
119 |
if state:
|
120 |
allowed = [x[0] for x in Network.OPER_STATES] |
121 |
if state not in allowed: |
122 |
msg = "Invalid state, must be one of %s" % ', '.join(allowed) |
123 |
raise CommandError(msg)
|
124 |
|
125 |
fields = ('name', 'userid', 'subnet', 'gateway', 'subnet6', 'gateway6', |
126 |
'dhcp', 'state', 'link', 'mac_prefix') |
127 |
for field in fields: |
128 |
value = options.get(field, None)
|
129 |
if value:
|
130 |
network.__setattr__(field, value) |
131 |
|
132 |
add_reserved_ips = options.get('add_reserved_ips')
|
133 |
remove_reserved_ips = options.get('remove_reserved_ips')
|
134 |
if add_reserved_ips or remove_reserved_ips: |
135 |
if add_reserved_ips:
|
136 |
add_reserved_ips = add_reserved_ips.split(",")
|
137 |
if remove_reserved_ips:
|
138 |
remove_reserved_ips = remove_reserved_ips.split(",")
|
139 |
|
140 |
for bnetwork in network.backend_networks.all(): |
141 |
with pooled_rapi_client(bnetwork.backend) as c: |
142 |
c.ModifyNetwork(network=network.backend_id, |
143 |
add_reserved_ips=add_reserved_ips, |
144 |
remove_reserved_ips=remove_reserved_ips) |
145 |
|
146 |
network.save() |