root / snf-cyclades-app / synnefo / api / management / commands / network-modify.py @ b1fb3aac
History | View | Annotate | Download (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 |
make_option( |
113 |
"--drained",
|
114 |
dest="drained",
|
115 |
metavar="True|False",
|
116 |
choices=["True", "False"], |
117 |
help="Set as drained to exclude for IP allocation."
|
118 |
" Only used for public networks.")
|
119 |
) |
120 |
|
121 |
def handle(self, *args, **options): |
122 |
if len(args) != 1: |
123 |
raise CommandError("Please provide a network ID") |
124 |
|
125 |
network = get_network(args[0])
|
126 |
|
127 |
# Validate subnet
|
128 |
if options.get('subnet'): |
129 |
validate_network_info(options) |
130 |
|
131 |
# Validate state
|
132 |
state = options.get('state')
|
133 |
if state:
|
134 |
allowed = [x[0] for x in Network.OPER_STATES] |
135 |
if state not in allowed: |
136 |
msg = "Invalid state, must be one of %s" % ', '.join(allowed) |
137 |
raise CommandError(msg)
|
138 |
|
139 |
dhcp = options.get("dhcp")
|
140 |
if dhcp:
|
141 |
options["dhcp"] = parse_bool(dhcp)
|
142 |
drained = options.get("drained")
|
143 |
if drained:
|
144 |
options["drained"] = parse_bool(drained)
|
145 |
fields = ('name', 'userid', 'subnet', 'gateway', 'subnet6', 'gateway6', |
146 |
'dhcp', 'state', 'link', 'mac_prefix', 'drained') |
147 |
for field in fields: |
148 |
value = options.get(field, None)
|
149 |
if value is not None: |
150 |
network.__setattr__(field, value) |
151 |
|
152 |
add_reserved_ips = options.get('add_reserved_ips')
|
153 |
remove_reserved_ips = options.get('remove_reserved_ips')
|
154 |
if add_reserved_ips or remove_reserved_ips: |
155 |
if add_reserved_ips:
|
156 |
add_reserved_ips = add_reserved_ips.split(",")
|
157 |
if remove_reserved_ips:
|
158 |
remove_reserved_ips = remove_reserved_ips.split(",")
|
159 |
|
160 |
for bnetwork in network.backend_networks.all(): |
161 |
with pooled_rapi_client(bnetwork.backend) as c: |
162 |
c.ModifyNetwork(network=network.backend_id, |
163 |
add_reserved_ips=add_reserved_ips, |
164 |
remove_reserved_ips=remove_reserved_ips) |
165 |
|
166 |
network.save() |