Statistics
| Branch: | Tag: | Revision:

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

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
from synnefo.management.common import get_backend, convert_api_faults
38
from synnefo.webproject.management.utils import parse_bool
39

    
40
from synnefo.db.models import Network, Backend
41
from synnefo.logic import networks
42
from synnefo.logic.backend import create_network
43

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

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

    
143
        name = options['name']
144
        subnet = options['subnet']
145
        gateway = options['gateway']
146
        subnet6 = options['subnet6']
147
        gateway6 = options['gateway6']
148
        backend_id = options['backend_id']
149
        public = options['public']
150
        flavor = options['flavor']
151
        mode = options['mode']
152
        link = options['link']
153
        mac_prefix = options['mac_prefix']
154
        tags = options['tags']
155
        userid = options["owner"]
156
        floating_ip_pool = parse_bool(options["floating_ip_pool"])
157
        dhcp = parse_bool(options["dhcp"])
158

    
159
        if not name:
160
            raise CommandError("name is required")
161
        if not flavor:
162
            raise CommandError("flavor is required")
163

    
164
        if (subnet is None) and (subnet6 is None):
165
            raise CommandError("subnet or subnet6 is required")
166
        if subnet is None and gateway is not None:
167
            raise CommandError("Can not use gateway without subnet")
168
        if subnet6 is None and gateway6 is not None:
169
            raise CommandError("Can not use gateway6 without subnet6")
170

    
171
        if public and not (backend_id or floating_ip_pool):
172
            raise CommandError("backend-id is required")
173
        if not userid and not public:
174
            raise CommandError("'owner' is required for private networks")
175

    
176
        if backend_id is not None:
177
            try:
178
                backend_id = int(backend_id)
179
            except ValueError:
180
                raise CommandError("Invalid backend-id: %s", backend_id)
181
            backend = get_backend(backend_id)
182

    
183
        network = networks.create(user_id=userid, name=name, flavor=flavor,
184
                                  subnet=subnet, gateway=gateway,
185
                                  subnet6=subnet6, gateway6=gateway6,
186
                                  dhcp=dhcp, public=public, mode=mode,
187
                                  link=link, mac_prefix=mac_prefix, tags=tags,
188
                                  floating_ip_pool=floating_ip_pool)
189

    
190
        # Create network in Backend if needed
191
        if floating_ip_pool:
192
            backends = Backend.objects.filter(offline=False)
193
        elif backend_id:
194
            backends = [backend]
195
        else:
196
            backends = []
197

    
198
        for backend in backends:
199
            network.create_backend_network(backend)
200
            self.stdout.write("Trying to connect network to backend '%s'\n" %
201
                              backend)
202
            jobs = create_network(network=network, backend=backend,
203
                                  connect=True)
204
            self.stdout.write("Successfully issued jobs: %s\n" %
205
                              ",".join(map(str, jobs)))