Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.3 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.logic.backend import create_network
40
from _common import validate_network_info
41

    
42
NETWORK_TYPES = ['PUBLIC_ROUTED', 'PRIVATE_MAC_FILTERED',
43
                 'PRIVATE_PHYSICAL_VLAN', 'CUSTOM_ROUTED',
44
                 'CUSTOM_BRIDGED']
45

    
46

    
47
class Command(BaseCommand):
48
    can_import_settings = True
49
    output_transaction = True
50

    
51
    help = "Create a new network"
52

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

    
107
    def handle(self, *args, **options):
108
        if args:
109
            raise CommandError("Command doesn't accept any arguments")
110

    
111
        name = options['name']
112
        subnet = options['subnet']
113
        net_type = options['type']
114
        backend_id = options['backend_id']
115
        public = options['public']
116
        link = options['link']
117
        mac_prefix = options['mac_prefix']
118

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

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

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

    
143
        default_link, default_mac_prefix = net_resources(net_type)
144
        if not link:
145
            link = default_link
146
        if not mac_prefix:
147
            mac_prefix = default_mac_prefix
148

    
149
        subnet, gateway, subnet6, gateway6 = validate_network_info(options)
150

    
151
        if not link:
152
            raise CommandError("Can not create network. No connectivity link")
153

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

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