Revision 7b5263e4 snf-cyclades-app/synnefo/api/management/commands/network-create.py

b/snf-cyclades-app/synnefo/api/management/commands/network-create.py
34 34
from optparse import make_option
35 35

  
36 36
from django.core.management.base import BaseCommand, CommandError
37
from synnefo.management.common import get_backend, convert_api_faults
37
from synnefo.management.common import convert_api_faults
38 38
from snf_django.management.utils import parse_bool
39 39

  
40
from synnefo.db.models import Network, Backend
40
from synnefo.db.models import Network
41 41
from synnefo.logic import networks, subnets
42 42
from synnefo.management import pprint
43 43

  
......
56 56
            dest='name',
57 57
            help="Name of the network"),
58 58
        make_option(
59
            '--name4',
60
            dest='name4',
61
            help="Name of the IPv4 subnet"),
62
        make_option(
63
            '--name6',
64
            dest='name6',
65
            help="Name of the IPv6 subnet"),
66
        make_option(
67 59
            '--owner',
68 60
            dest='owner',
69 61
            help="The owner of the network"),
......
96 88
            metavar="True|False",
97 89
            help='Automatically assign IPs'),
98 90
        make_option(
99
            '--slaac',
100
            dest='slaac',
101
            default="False",
102
            choices=["True", "False"],
103
            metavar="True|False",
104
            help='Automatically assign IPs for the IPv6 subnet'),
105
        make_option(
106 91
            '--public',
107 92
            dest='public',
108 93
            action='store_true',
......
140 125
            default="False",
141 126
            choices=["True", "False"],
142 127
            metavar="True|False",
143
            help="Use the network as a Floating IP pool. Floating IP pools"
144
                 " are created in all available backends."),
145
        make_option(
146
            "--backend-ids",
147
            dest="backend_ids",
148
            default=None,
149
            help="Comma seperated list of Ganeti backends IDs that the network"
150
                 " will be created. Only for public networks. Use 'all' to"
151
                 " create network in all available backends."),
128
            help="Use the network as a Floating IP pool."),
152 129
    )
153 130

  
154 131
    @convert_api_faults
......
157 134
            raise CommandError("Command doesn't accept any arguments")
158 135

  
159 136
        name = options['name']
160
        name4 = options['name4']
161
        name6 = options['name6']
162 137
        subnet = options['subnet']
163 138
        gateway = options['gateway']
164 139
        subnet6 = options['subnet6']
165 140
        gateway6 = options['gateway6']
166
        backend_ids = options['backend_ids']
167 141
        public = options['public']
168 142
        flavor = options['flavor']
169 143
        mode = options['mode']
......
173 147
        userid = options["owner"]
174 148
        floating_ip_pool = parse_bool(options["floating_ip_pool"])
175 149
        dhcp = parse_bool(options["dhcp"])
176
        slaac = parse_bool(options["slaac"])
177 150

  
178 151
        if name is None:
179 152
            name = ""
......
182 155

  
183 156
        if (subnet is None) and (subnet6 is None):
184 157
            raise CommandError("subnet or subnet6 is required")
158

  
185 159
        if subnet is None and gateway is not None:
186 160
            raise CommandError("Cannot use gateway without subnet")
187
        if subnet is None and dhcp is not None:
188
            raise CommandError("Cannot use dhcp without subnet")
189
        if subnet is None and name4 is not None:
190
            raise CommandError("Cannot use name without subnet")
191

  
192 161
        if subnet6 is None and gateway6 is not None:
193 162
            raise CommandError("Cannot use gateway6 without subnet6")
194
        if subnet6 is None and slaac is not None:
195
            raise CommandError("Cannot use slaac without subnet6")
196
        if public and not (backend_ids or floating_ip_pool):
197
            raise CommandError("backend-ids is required")
198
        if subnet6 is None and name6 is not None:
199
            raise CommandError("Cannot use name6 without subnet6")
200
        if not userid and not public:
201
            raise CommandError("'owner' is required for private networks")
202 163

  
203
        backends = []
204
        if backend_ids is not None:
205
            if backend_ids == "all":
206
                backends = Backend.objects.filter(offline=False)
207
            else:
208
                for backend_id in backend_ids.split(","):
209
                    try:
210
                        backend_id = int(backend_id)
211
                    except ValueError:
212
                        raise CommandError("Invalid backend-id: %s"
213
                                           % backend_id)
214
                    backend = get_backend(backend_id)
215
                    backends.append(backend)
164
        if not (userid or public):
165
            raise CommandError("'owner' is required for private networks")
216 166

  
217 167
        network = networks.create(userid=userid, name=name, flavor=flavor,
218 168
                                  public=public, mode=mode,
219 169
                                  link=link, mac_prefix=mac_prefix, tags=tags,
220
                                  floating_ip_pool=floating_ip_pool,
221
                                  backends=backends, lazy_create=False)
170
                                  floating_ip_pool=floating_ip_pool)
222 171

  
223
        sub4 = subnets._create_subnet(network.id, cidr=subnet, name=name4,
224
                                      ipversion=4, gateway=gateway, dhcp=dhcp,
225
                                      user_id=userid)
172
        if subnet is not None:
173
            name = "IPv4 Subnet of Network %s" % network.id
174
            subnets.create_subnet(network.id, cidr=subnet, name=name,
175
                                  ipversion=4, gateway=gateway, dhcp=dhcp,
176
                                  user_id=userid)
226 177

  
227
        sub6 = subnets._create_subnet(network.id, cidr=subnet6, name=name6,
228
                                      ipversion=6, gateway=gateway6,
229
                                      dhcp=slaac, user_id=userid)
178
        if subnet6 is not None:
179
            name = "IPv6 Subnet of Network %s" % network.id
180
            subnets.create_subnet(network.id, cidr=subnet6, name=name,
181
                                  ipversion=6, gateway=gateway6,
182
                                  dhcp=dhcp, user_id=userid)
230 183

  
231 184
        self.stdout.write("Created network '%s' in DB:\n" % network)
232 185
        pprint.pprint_network(network, stdout=self.stdout)
233 186
        pprint.pprint_network_subnets(network, stdout=self.stdout)
187

  
188
        networks.create_network_in_backends(network)
189
        # TODO: Add --wait option to track job progress and report successful
190
        # creation in each backend.
191
        self.stdout.write("\nSuccessfully issued job to create network in"
192
                          " in backends\n")

Also available in: Unified diff