Revision c75ab92e
b/snf-cyclades-app/synnefo/api/management/commands/network-create.py | ||
---|---|---|
34 | 34 |
from optparse import make_option |
35 | 35 |
|
36 | 36 |
from django.core.management.base import BaseCommand, CommandError |
37 |
from synnefo.management.common import validate_network_info, get_backend
|
|
38 |
from synnefo.webproject.management.utils import pprint_table, parse_bool
|
|
37 |
from synnefo.management.common import get_backend, convert_api_faults
|
|
38 |
from synnefo.webproject.management.utils import parse_bool |
|
39 | 39 |
|
40 |
from synnefo import quotas |
|
41 | 40 |
from synnefo.db.models import Network, Backend |
42 |
from synnefo.db.utils import validate_mac, InvalidMacAddress
|
|
41 |
from synnefo.logic import networks
|
|
43 | 42 |
from synnefo.logic.backend import create_network |
44 |
from synnefo.api.util import values_from_flavor |
|
45 | 43 |
|
46 | 44 |
NETWORK_FLAVORS = Network.FLAVORS.keys() |
47 | 45 |
|
... | ... | |
54 | 52 |
|
55 | 53 |
option_list = BaseCommand.option_list + ( |
56 | 54 |
make_option( |
57 |
"-n", |
|
58 |
"--dry-run", |
|
59 |
dest="dry_run", |
|
60 |
default=False, |
|
61 |
action="store_true"), |
|
62 |
make_option( |
|
63 | 55 |
'--name', |
64 | 56 |
dest='name', |
65 | 57 |
help="Name of network"), |
... | ... | |
91 | 83 |
make_option( |
92 | 84 |
'--dhcp', |
93 | 85 |
dest='dhcp', |
94 |
action='store_true', |
|
95 |
default=False, |
|
86 |
default="False", |
|
87 |
choices=["True", "False"], |
|
88 |
metavar="True|False", |
|
96 | 89 |
help='Automatically assign IPs'), |
97 | 90 |
make_option( |
98 | 91 |
'--public', |
... | ... | |
142 | 135 |
' public networks'), |
143 | 136 |
) |
144 | 137 |
|
138 |
@convert_api_faults |
|
145 | 139 |
def handle(self, *args, **options): |
146 | 140 |
if args: |
147 | 141 |
raise CommandError("Command doesn't accept any arguments") |
148 | 142 |
|
149 |
dry_run = options["dry_run"] |
|
150 | 143 |
name = options['name'] |
151 | 144 |
subnet = options['subnet'] |
152 | 145 |
gateway = options['gateway'] |
... | ... | |
161 | 154 |
tags = options['tags'] |
162 | 155 |
userid = options["owner"] |
163 | 156 |
floating_ip_pool = parse_bool(options["floating_ip_pool"]) |
157 |
dhcp = parse_bool(options["dhcp"]) |
|
164 | 158 |
|
165 | 159 |
if not name: |
166 | 160 |
raise CommandError("name is required") |
... | ... | |
179 | 173 |
if not userid and not public: |
180 | 174 |
raise CommandError("'owner' is required for private networks") |
181 | 175 |
|
182 |
if mac_prefix and flavor == "MAC_FILTERED": |
|
183 |
raise CommandError("Can not override MAC_FILTERED mac-prefix") |
|
184 |
if link and flavor == "PHYSICAL_VLAN": |
|
185 |
raise CommandError("Can not override PHYSICAL_VLAN link") |
|
186 |
|
|
187 |
if backend_id: |
|
176 |
if backend_id is not None: |
|
177 |
try: |
|
178 |
backend_id = int(backend_id) |
|
179 |
except ValueError: |
|
180 |
raise CommandError("Invalid backend-id: %s", backend_id) |
|
188 | 181 |
backend = get_backend(backend_id) |
189 | 182 |
|
190 |
fmode, flink, fmac_prefix, ftags = values_from_flavor(flavor) |
|
191 |
mode = mode or fmode |
|
192 |
link = link or flink |
|
193 |
mac_prefix = mac_prefix or fmac_prefix |
|
194 |
tags = tags or ftags |
|
195 |
|
|
196 |
try: |
|
197 |
validate_mac(mac_prefix + "0:00:00:00") |
|
198 |
except InvalidMacAddress: |
|
199 |
raise CommandError("Invalid MAC prefix '%s'" % mac_prefix) |
|
200 |
subnet, gateway, subnet6, gateway6 = validate_network_info(options) |
|
201 |
|
|
202 |
if not link or not mode: |
|
203 |
raise CommandError("Can not create network." |
|
204 |
" No connectivity link or mode") |
|
205 |
netinfo = { |
|
206 |
"name": name, |
|
207 |
"userid": options["owner"], |
|
208 |
"subnet": subnet, |
|
209 |
"gateway": gateway, |
|
210 |
"gateway6": gateway6, |
|
211 |
"subnet6": subnet6, |
|
212 |
"dhcp": options["dhcp"], |
|
213 |
"flavor": flavor, |
|
214 |
"public": public, |
|
215 |
"mode": mode, |
|
216 |
"link": link, |
|
217 |
"mac_prefix": mac_prefix, |
|
218 |
"tags": tags, |
|
219 |
"floating_ip_pool": floating_ip_pool, |
|
220 |
"state": "ACTIVE"} |
|
221 |
|
|
222 |
if dry_run: |
|
223 |
self.stdout.write("Creating network:\n") |
|
224 |
pprint_table(self.stdout, tuple(netinfo.items())) |
|
225 |
return |
|
226 |
|
|
227 |
network = Network.objects.create(**netinfo) |
|
228 |
if userid: |
|
229 |
quotas.issue_and_accept_commission(network) |
|
183 |
network = networks.create(user_id=userid, name=name, flavor=flavor, |
|
184 |
subnet=subnet, gateway=gateway, |
|
185 |
subnet6=subnet6, gateway6=gateway6, |
|
186 |
dhcp=dhcp, public=public, mode=mode, |
|
187 |
link=link, mac_prefix=mac_prefix, tags=tags, |
|
188 |
floating_ip_pool=floating_ip_pool) |
|
230 | 189 |
|
231 | 190 |
# Create network in Backend if needed |
232 | 191 |
if floating_ip_pool: |
b/snf-cyclades-app/synnefo/api/management/commands/server-create.py | ||
---|---|---|
70 | 70 |
help="Password for the new server") |
71 | 71 |
) |
72 | 72 |
|
73 |
@common.convert_api_faults |
|
73 | 74 |
def handle(self, *args, **options): |
74 | 75 |
if args: |
75 | 76 |
raise CommandError("Command doesn't accept any arguments") |
b/snf-cyclades-app/synnefo/logic/networks.py | ||
---|---|---|
62 | 62 |
|
63 | 63 |
|
64 | 64 |
@transaction.commit_on_success |
65 |
def create(user_id, name, flavor, subnet, gateway=None, subnet6=None, |
|
66 |
gateway6=None, public=False, dhcp=True): |
|
65 |
def create(user_id, name, flavor, subnet=None, gateway=None, subnet6=None, |
|
66 |
gateway6=None, public=False, dhcp=True, link=None, mac_prefix=None, |
|
67 |
mode=None, floating_ip_pool=False, tags=None): |
|
67 | 68 |
if flavor is None: |
68 | 69 |
raise faults.BadRequest("Missing request parameter 'type'") |
69 | 70 |
elif flavor not in Network.FLAVORS.keys(): |
70 | 71 |
raise faults.BadRequest("Invalid network type '%s'" % flavor) |
71 | 72 |
|
73 |
if mac_prefix is not None and flavor == "MAC_FILTERED": |
|
74 |
raise faults.BadRequest("Can not override MAC_FILTERED mac-prefix") |
|
75 |
if link is not None and flavor == "PHYSICAL_VLAN": |
|
76 |
raise faults.BadRequest("Can not override PHYSICAL_VLAN link") |
|
77 |
|
|
78 |
if subnet is None and floating_ip_pool: |
|
79 |
raise faults.BadRequest("IPv6 only networks can not be" |
|
80 |
" pools.") |
|
72 | 81 |
# Check that network parameters are valid |
73 | 82 |
util.validate_network_params(subnet, gateway, subnet6, gateway6) |
74 | 83 |
|
75 | 84 |
try: |
76 |
mode, link, mac_prefix, tags = util.values_from_flavor(flavor)
|
|
85 |
fmode, flink, fmac_prefix, ftags = util.values_from_flavor(flavor)
|
|
77 | 86 |
except EmptyPool: |
78 | 87 |
log.error("Failed to allocate resources for network of type: %s", |
79 | 88 |
flavor) |
80 | 89 |
msg = "Failed to allocate resources for network." |
81 | 90 |
raise faults.ServiceUnavailable(msg) |
91 |
|
|
92 |
mode = mode or fmode |
|
93 |
link = link or flink |
|
94 |
mac_prefix = mac_prefix or fmac_prefix |
|
95 |
tags = tags or ftags |
|
96 |
|
|
82 | 97 |
validate_mac(mac_prefix + "0:00:00:00") |
83 | 98 |
|
84 | 99 |
network = Network.objects.create( |
... | ... | |
94 | 109 |
link=link, |
95 | 110 |
mac_prefix=mac_prefix, |
96 | 111 |
tags=tags, |
112 |
public=public, |
|
113 |
floating_ip_pool=floating_ip_pool, |
|
97 | 114 |
action='CREATE', |
98 | 115 |
state='ACTIVE') |
99 | 116 |
|
b/snf-cyclades-app/synnefo/management/common.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
from django.core.management import CommandError |
35 | 35 |
from synnefo.db.models import Backend, VirtualMachine, Network, Flavor |
36 |
from functools import wraps |
|
36 | 37 |
|
37 | 38 |
from snf_django.lib.api import faults |
38 | 39 |
from synnefo.api import util |
... | ... | |
167 | 168 |
" Ganeti Cluster name: %s" % info_name) |
168 | 169 |
|
169 | 170 |
|
171 |
def convert_api_faults(func): |
|
172 |
@wraps(func) |
|
173 |
def wrapper(*args, **kwargs): |
|
174 |
try: |
|
175 |
return func(*args, **kwargs) |
|
176 |
except faults.Fault as e: |
|
177 |
raise CommandError(e.message) |
|
178 |
return wrapper |
|
179 |
|
|
180 |
|
|
170 | 181 |
class Omit(object): |
171 | 182 |
pass |
Also available in: Unified diff