Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.5 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 network_link_from_type, 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

    
53
    help = "Create a new network"
54

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

    
101
    def handle(self, *args, **options):
102
        if args:
103
            raise CommandError("Command doesn't accept any arguments")
104

    
105
        name = options['name']
106
        subnet = options['subnet']
107
        typ = options['type']
108
        backend_id = options['backend_id']
109
        public = options['public']
110

    
111
        if not name:
112
            raise CommandError("Name is required")
113
        if not subnet:
114
            raise CommandError("Subnet is required")
115
        if public and not backend_id:
116
            raise CommandError("backend-id is required")
117
        if backend_id and not public:
118
            raise CommandError("Private networks must be created to"
119
                               " all backends")
120

    
121
        if backend_id:
122
            try:
123
                backend_id = int(backend_id)
124
                backend = Backend.objects.get(id=backend_id)
125
            except ValueError:
126
                raise CommandError("Invalid backend ID")
127
            except Backend.DoesNotExist:
128
                raise CommandError("Backend not found in DB")
129

    
130
        link = network_link_from_type(typ)
131

    
132
        subnet, gateway, subnet6, gateway6 = validate_network_info(options)
133

    
134
        if not link:
135
            raise CommandError("Can not create network. No connectivity link")
136

    
137
        network = Network.objects.create(
138
                name=name,
139
                userid=options['owner'],
140
                subnet=subnet,
141
                gateway=gateway,
142
                dhcp=options['dhcp'],
143
                type=options['type'],
144
                public=public,
145
                link=link,
146
                gateway6=gateway6,
147
                subnet6=subnet6,
148
                state='PENDING')
149

    
150
        if public:
151
            # Create BackendNetwork only to the specified Backend
152
            network.create_backend_network(backend)
153
            create_network(network, backends=[backend])
154
        else:
155
            # Create BackendNetwork entries for all Backends
156
            network.create_backend_network()
157
            create_network(network)
158

    
159

    
160
def validate_network_info(options):
161
    subnet = options['subnet']
162
    gateway = options['gateway']
163
    subnet6 = options['subnet6']
164
    gateway6 = options['gateway6']
165

    
166
    try:
167
        net = ipaddr.IPv4Network(subnet)
168
        prefix = net.prefixlen
169
        if not validate_network_size(prefix):
170
            raise CommandError("Unsupport network mask %d."
171
                               " Must be in range (%s,29] "
172
                               % (prefix, settings.MAX_CIDR_BLOCK))
173
    except ValueError:
174
        raise CommandError('Malformed subnet')
175
    try:
176
        gateway and ipaddr.IPv4Address(gateway) or None
177
    except ValueError:
178
        raise CommandError('Malformed gateway')
179

    
180
    try:
181
        subnet6 and ipaddr.IPv6Network(subnet6) or None
182
    except ValueError:
183
        raise CommandError('Malformed subnet6')
184

    
185
    try:
186
        gateway6 and ipaddr.IPv6Address(gateway6) or None
187
    except ValueError:
188
        raise CommandError('Malformed gateway6')
189

    
190
    return subnet, gateway, subnet6, gateway6