Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / network-create.py @ 71ec054d

History | View | Annotate | Download (7.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, Backend
39
from synnefo.api.util import net_resources, validate_network_size
40
from synnefo.logic.backend import create_network
41
from synnefo import settings
42

    
43
import ipaddr
44

    
45
NETWORK_TYPES = ['PUBLIC_ROUTED', 'PRIVATE_MAC_FILTERED',
46
                 'PRIVATE_PHYSICAL_VLAN', 'CUSTOM_ROUTED',
47
                 'CUSTOM_BRIDGED']
48

    
49

    
50
class Command(BaseCommand):
51
    can_import_settings = True
52
    output_transaction = True
53

    
54
    help = "Create a new network"
55

    
56
    option_list = BaseCommand.option_list + (
57
        make_option('--name',
58
            dest='name',
59
            help="Name of network"),
60
        make_option('--owner',
61
            dest='owner',
62
            help="The owner of the network"),
63
        make_option('--subnet',
64
            dest='subnet',
65
            default=None,
66
            # required=True,
67
            help='Subnet of the network'),
68
        make_option('--gateway',
69
            dest='gateway',
70
            default=None,
71
            help='Gateway of the network'),
72
        make_option('--dhcp',
73
            dest='dhcp',
74
            action='store_true',
75
            default=False,
76
            help='Automatically assign IPs'),
77
        make_option('--public',
78
            dest='public',
79
            action='store_true',
80
            default=False,
81
            help='Network is public'),
82
        make_option('--type',
83
            dest='type',
84
            default='PRIVATE_MAC_FILTERED',
85
            choices=NETWORK_TYPES,
86
            help='Type of network. Choices: ' + ', '.join(NETWORK_TYPES)),
87
        make_option('--subnet6',
88
            dest='subnet6',
89
            default=None,
90
            help='IPv6 subnet of the network'),
91
        make_option('--gateway6',
92
            dest='gateway6',
93
            default=None,
94
            help='IPv6 gateway of the network'),
95
        make_option('--backend-id',
96
            dest='backend_id',
97
            default=None,
98
            help='ID of the backend that the network will be created. Only for'
99
                 ' public networks'),
100
        make_option('--link',
101
            dest='link',
102
            default=None,
103
            help="Connectivity link of the Network. None for default."),
104
        make_option('--mac-prefix',
105
            dest='mac_prefix',
106
            default=None,
107
            help="MAC prefix of the network. None for default")
108
        )
109

    
110
    def handle(self, *args, **options):
111
        if args:
112
            raise CommandError("Command doesn't accept any arguments")
113

    
114
        name = options['name']
115
        subnet = options['subnet']
116
        net_type = options['type']
117
        backend_id = options['backend_id']
118
        public = options['public']
119
        link = options['link']
120
        mac_prefix = options['mac_prefix']
121

    
122
        if not name:
123
            raise CommandError("Name is required")
124
        if not subnet:
125
            raise CommandError("Subnet is required")
126
        if public and not backend_id:
127
            raise CommandError("backend-id is required")
128
        if backend_id and not public:
129
            raise CommandError("Private networks must be created to"
130
                               " all backends")
131

    
132
        if mac_prefix and net_type == "PRIVATE_MAC_FILTERED":
133
            raise CommandError("Can not override MAC_FILTERED mac-prefix")
134
        if link and net_type == "PRIVATE_PHYSICAL_VLAN":
135
            raise CommandError("Can not override PHYSICAL_VLAN link")
136

    
137
        if backend_id:
138
            try:
139
                backend_id = int(backend_id)
140
                backend = Backend.objects.get(id=backend_id)
141
            except ValueError:
142
                raise CommandError("Invalid backend ID")
143
            except Backend.DoesNotExist:
144
                raise CommandError("Backend not found in DB")
145

    
146
        default_link, default_mac_prefix = net_resources(net_type)
147
        if not link:
148
            link = default_link
149
        if not mac_prefix:
150
            mac_prefix = default_mac_prefix
151

    
152
        subnet, gateway, subnet6, gateway6 = validate_network_info(options)
153

    
154
        if not link:
155
            raise CommandError("Can not create network. No connectivity link")
156

    
157
        network = Network.objects.create(
158
                name=name,
159
                userid=options['owner'],
160
                subnet=subnet,
161
                gateway=gateway,
162
                dhcp=options['dhcp'],
163
                type=net_type,
164
                public=public,
165
                link=link,
166
                mac_prefix=mac_prefix,
167
                gateway6=gateway6,
168
                subnet6=subnet6,
169
                state='PENDING')
170

    
171
        if public:
172
            # Create BackendNetwork only to the specified Backend
173
            network.create_backend_network(backend)
174
            create_network(network, backends=[backend])
175
        else:
176
            # Create BackendNetwork entries for all Backends
177
            network.create_backend_network()
178
            create_network(network)
179

    
180

    
181
def validate_network_info(options):
182
    subnet = options['subnet']
183
    gateway = options['gateway']
184
    subnet6 = options['subnet6']
185
    gateway6 = options['gateway6']
186

    
187
    try:
188
        net = ipaddr.IPv4Network(subnet)
189
        prefix = net.prefixlen
190
        if not validate_network_size(prefix):
191
            raise CommandError("Unsupport network mask %d."
192
                               " Must be in range (%s,29] "
193
                               % (prefix, settings.MAX_CIDR_BLOCK))
194
    except ValueError:
195
        raise CommandError('Malformed subnet')
196
    try:
197
        gateway and ipaddr.IPv4Address(gateway) or None
198
    except ValueError:
199
        raise CommandError('Malformed gateway')
200

    
201
    try:
202
        subnet6 and ipaddr.IPv6Network(subnet6) or None
203
    except ValueError:
204
        raise CommandError('Malformed subnet6')
205

    
206
    try:
207
        gateway6 and ipaddr.IPv6Address(gateway6) or None
208
    except ValueError:
209
        raise CommandError('Malformed gateway6')
210

    
211
    return subnet, gateway, subnet6, gateway6