root / snf-cyclades-app / synnefo / api / management / commands / network-create.py @ 6dd70a5c
History | View | Annotate | Download (5.1 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 |
39 |
from synnefo.api.util import network_specs_from_type |
40 |
from synnefo.logic.backend import create_network |
41 |
|
42 |
import ipaddr |
43 |
|
44 |
NETWORK_TYPES = ['PUBLIC_ROUTED', 'PRIVATE_MAC_FILTERED', |
45 |
'PRIVATE_PHYSICAL_VLAN', 'CUSTOM_ROUTED', |
46 |
'CUSTOM_BRIDGED']
|
47 |
|
48 |
|
49 |
class Command(BaseCommand): |
50 |
help = "Create a new network"
|
51 |
|
52 |
option_list = BaseCommand.option_list + ( |
53 |
make_option('--name',
|
54 |
dest='name',
|
55 |
help="Name of network"),
|
56 |
make_option('--owner',
|
57 |
dest='owner',
|
58 |
help="The owner of the network"),
|
59 |
make_option('--subnet',
|
60 |
dest='subnet',
|
61 |
default=None,
|
62 |
# required=True,
|
63 |
help='Subnet of the network'),
|
64 |
make_option('--gateway',
|
65 |
dest='gateway',
|
66 |
default=None,
|
67 |
help='Gateway of the network'),
|
68 |
make_option('--dhcp',
|
69 |
dest='dhcp',
|
70 |
action='store_true',
|
71 |
default=False,
|
72 |
help='Automatically assign IPs'),
|
73 |
make_option('--public',
|
74 |
dest='public',
|
75 |
action='store_true',
|
76 |
default=False,
|
77 |
help='Network is public'),
|
78 |
make_option('--type',
|
79 |
dest='type',
|
80 |
default='PRIVATE_MAC_FILTERED',
|
81 |
choices=NETWORK_TYPES, |
82 |
help='Type of network. Choices: ' + ', '.join(NETWORK_TYPES)), |
83 |
make_option('--subnet6',
|
84 |
dest='subnet6',
|
85 |
default=None,
|
86 |
help='IPv6 subnet of the network'),
|
87 |
make_option('--gateway6',
|
88 |
dest='gateway6',
|
89 |
default=None,
|
90 |
help='IPv6 gateway of the network'),
|
91 |
) |
92 |
|
93 |
def handle(self, *args, **options): |
94 |
if args:
|
95 |
raise CommandError("Command doesn't accept any arguments") |
96 |
|
97 |
name = options['name']
|
98 |
subnet = options['subnet']
|
99 |
|
100 |
if not name: |
101 |
raise CommandError("Name is required") |
102 |
if not subnet: |
103 |
raise CommandError("Subnet is required") |
104 |
|
105 |
link, mac_prefix = network_specs_from_type(options['type'])
|
106 |
|
107 |
subnet, gateway, subnet6, gateway6 = validate_network_info(options) |
108 |
|
109 |
if not link: |
110 |
raise CommandError("Can not create network. No connectivity link") |
111 |
|
112 |
network = Network.objects.create( |
113 |
name=name, |
114 |
userid=options['owner'],
|
115 |
subnet=subnet, |
116 |
gateway=gateway, |
117 |
dhcp=options['dhcp'],
|
118 |
type=options['type'],
|
119 |
public=options['public'],
|
120 |
link=link, |
121 |
mac_prefix=mac_prefix, |
122 |
gateway6=gateway6, |
123 |
subnet6=subnet6, |
124 |
state='PENDING')
|
125 |
|
126 |
create_network(network) |
127 |
|
128 |
|
129 |
def validate_network_info(options): |
130 |
subnet = options['subnet']
|
131 |
gateway = options['gateway']
|
132 |
subnet6 = options['subnet6']
|
133 |
gateway6 = options['gateway6']
|
134 |
|
135 |
try:
|
136 |
ipaddr.IPv4Network(subnet) |
137 |
except ValueError: |
138 |
raise CommandError('Malformed subnet') |
139 |
try:
|
140 |
gateway and ipaddr.IPv4Address(gateway) or None |
141 |
except ValueError: |
142 |
raise CommandError('Malformed gateway') |
143 |
|
144 |
try:
|
145 |
subnet6 and ipaddr.IPv6Network(subnet6) or None |
146 |
except ValueError: |
147 |
raise CommandError('Malformed subnet6') |
148 |
|
149 |
try:
|
150 |
gateway6 and ipaddr.IPv6Address(gateway6) or None |
151 |
except ValueError: |
152 |
raise CommandError('Malformed gateway6') |
153 |
|
154 |
return subnet, gateway, subnet6, gateway6
|