Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.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
from synnefo.management.common import validate_network_info, get_backend
38
from synnefo.webproject.management.utils import pprint_table
39

    
40
from synnefo import quotas
41
from synnefo.db.models import Network
42
from synnefo.db.utils import validate_mac, InvalidMacAddress
43
from synnefo.logic.backend import create_network
44
from synnefo.api.util import values_from_flavor
45

    
46
NETWORK_FLAVORS = Network.FLAVORS.keys()
47

    
48

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

    
53
    help = "Create a new network"
54

    
55
    option_list = BaseCommand.option_list + (
56
        make_option(
57
            "-n",
58
            "--dry-run",
59
            dest="dry_run",
60
            default=False,
61
            action="store_true"),
62
        make_option(
63
            '--name',
64
            dest='name',
65
            help="Name of network"),
66
        make_option(
67
            '--owner',
68
            dest='owner',
69
            help="The owner of the network"),
70
        make_option(
71
            '--subnet',
72
            dest='subnet',
73
            default=None,
74
            # required=True,
75
            help='Subnet of the network'),
76
        make_option(
77
            '--gateway',
78
            dest='gateway',
79
            default=None,
80
            help='Gateway of the network'),
81
        make_option(
82
            '--subnet6',
83
            dest='subnet6',
84
            default=None,
85
            help='IPv6 subnet of the network'),
86
        make_option(
87
            '--gateway6',
88
            dest='gateway6',
89
            default=None,
90
            help='IPv6 gateway of the network'),
91
        make_option(
92
            '--dhcp',
93
            dest='dhcp',
94
            action='store_true',
95
            default=False,
96
            help='Automatically assign IPs'),
97
        make_option(
98
            '--public',
99
            dest='public',
100
            action='store_true',
101
            default=False,
102
            help='Network is public'),
103
        make_option(
104
            '--flavor',
105
            dest='flavor',
106
            default=None,
107
            choices=NETWORK_FLAVORS,
108
            help='Network flavor. Choices: ' + ', '.join(NETWORK_FLAVORS)),
109
        make_option(
110
            '--mode',
111
            dest='mode',
112
            default=None,
113
            help="Overwrite flavor connectivity mode."),
114
        make_option(
115
            '--link',
116
            dest='link',
117
            default=None,
118
            help="Overwrite flavor connectivity link."),
119
        make_option(
120
            '--mac-prefix',
121
            dest='mac_prefix',
122
            default=None,
123
            help="Overwrite flavor connectivity MAC prefix"),
124
        make_option(
125
            '--tags',
126
            dest='tags',
127
            default=None,
128
            help='The tags of the Network (comma separated strings)'),
129
        make_option(
130
            '--backend-id',
131
            dest='backend_id',
132
            default=None,
133
            help='ID of the backend that the network will be created. Only for'
134
                 ' public networks'),
135
    )
136

    
137
    def handle(self, *args, **options):
138
        if args:
139
            raise CommandError("Command doesn't accept any arguments")
140

    
141
        dry_run = options["dry_run"]
142
        name = options['name']
143
        subnet = options['subnet']
144
        backend_id = options['backend_id']
145
        public = options['public']
146
        flavor = options['flavor']
147
        mode = options['mode']
148
        link = options['link']
149
        mac_prefix = options['mac_prefix']
150
        tags = options['tags']
151
        userid = options["owner"]
152

    
153
        if not name:
154
            raise CommandError("Name is required")
155
        if not subnet:
156
            raise CommandError("Subnet is required")
157
        if not flavor:
158
            raise CommandError("Flavor is required")
159
        if public and not backend_id:
160
            raise CommandError("backend-id is required")
161
        if not userid and not public:
162
            raise CommandError("'owner' is required for private networks")
163

    
164
        if mac_prefix and flavor == "MAC_FILTERED":
165
            raise CommandError("Can not override MAC_FILTERED mac-prefix")
166
        if link and flavor == "PHYSICAL_VLAN":
167
            raise CommandError("Can not override PHYSICAL_VLAN link")
168

    
169
        if backend_id:
170
            backend = get_backend(backend_id)
171

    
172
        fmode, flink, fmac_prefix, ftags = values_from_flavor(flavor)
173
        mode = mode or fmode
174
        link = link or flink
175
        mac_prefix = mac_prefix or fmac_prefix
176
        tags = tags or ftags
177

    
178
        try:
179
            validate_mac(mac_prefix + "0:00:00:00")
180
        except InvalidMacAddress:
181
            raise CommandError("Invalid MAC prefix '%s'" % mac_prefix)
182
        subnet, gateway, subnet6, gateway6 = validate_network_info(options)
183

    
184
        if not link or not mode:
185
            raise CommandError("Can not create network."
186
                               " No connectivity link or mode")
187
        netinfo = {
188
           "name": name,
189
           "userid": options["owner"],
190
           "subnet": subnet,
191
           "gateway": gateway,
192
           "gateway6": gateway6,
193
           "subnet6": subnet6,
194
           "dhcp": options["dhcp"],
195
           "flavor": flavor,
196
           "public": public,
197
           "mode": mode,
198
           "link": link,
199
           "mac_prefix": mac_prefix,
200
           "tags": tags,
201
           "state": "ACTIVE"}
202

    
203
        if dry_run:
204
            self.stdout.write("Creating network:\n")
205
            pprint_table(self.stdout, tuple(netinfo.items()))
206
            return
207

    
208
        network = Network.objects.create(**netinfo)
209
        if userid:
210
            quotas.issue_and_accept_commission(network)
211

    
212
        if backend_id:
213
            # Create BackendNetwork only to the specified Backend
214
            network.create_backend_network(backend)
215
            create_network(network=network, backend=backend, connect=True)