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

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

  
46 44
NETWORK_FLAVORS = Network.FLAVORS.keys()
47 45

  
......
54 52

  
55 53
    option_list = BaseCommand.option_list + (
56 54
        make_option(
57
            "-n",
58
            "--dry-run",
59
            dest="dry_run",
60
            default=False,
61
            action="store_true"),
62
        make_option(
63 55
            '--name',
64 56
            dest='name',
65 57
            help="Name of network"),
......
91 83
        make_option(
92 84
            '--dhcp',
93 85
            dest='dhcp',
94
            action='store_true',
95
            default=False,
86
            default="False",
87
            choices=["True", "False"],
88
            metavar="True|False",
96 89
            help='Automatically assign IPs'),
97 90
        make_option(
98 91
            '--public',
......
142 135
                 ' public networks'),
143 136
    )
144 137

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

  
149
        dry_run = options["dry_run"]
150 143
        name = options['name']
151 144
        subnet = options['subnet']
152 145
        gateway = options['gateway']
......
161 154
        tags = options['tags']
162 155
        userid = options["owner"]
163 156
        floating_ip_pool = parse_bool(options["floating_ip_pool"])
157
        dhcp = parse_bool(options["dhcp"])
164 158

  
165 159
        if not name:
166 160
            raise CommandError("name is required")
......
179 173
        if not userid and not public:
180 174
            raise CommandError("'owner' is required for private networks")
181 175

  
182
        if mac_prefix and flavor == "MAC_FILTERED":
183
            raise CommandError("Can not override MAC_FILTERED mac-prefix")
184
        if link and flavor == "PHYSICAL_VLAN":
185
            raise CommandError("Can not override PHYSICAL_VLAN link")
186

  
187
        if backend_id:
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)
188 181
            backend = get_backend(backend_id)
189 182

  
190
        fmode, flink, fmac_prefix, ftags = values_from_flavor(flavor)
191
        mode = mode or fmode
192
        link = link or flink
193
        mac_prefix = mac_prefix or fmac_prefix
194
        tags = tags or ftags
195

  
196
        try:
197
            validate_mac(mac_prefix + "0:00:00:00")
198
        except InvalidMacAddress:
199
            raise CommandError("Invalid MAC prefix '%s'" % mac_prefix)
200
        subnet, gateway, subnet6, gateway6 = validate_network_info(options)
201

  
202
        if not link or not mode:
203
            raise CommandError("Can not create network."
204
                               " No connectivity link or mode")
205
        netinfo = {
206
            "name": name,
207
            "userid": options["owner"],
208
            "subnet": subnet,
209
            "gateway": gateway,
210
            "gateway6": gateway6,
211
            "subnet6": subnet6,
212
            "dhcp": options["dhcp"],
213
            "flavor": flavor,
214
            "public": public,
215
            "mode": mode,
216
            "link": link,
217
            "mac_prefix": mac_prefix,
218
            "tags": tags,
219
            "floating_ip_pool": floating_ip_pool,
220
            "state": "ACTIVE"}
221

  
222
        if dry_run:
223
            self.stdout.write("Creating network:\n")
224
            pprint_table(self.stdout, tuple(netinfo.items()))
225
            return
226

  
227
        network = Network.objects.create(**netinfo)
228
        if userid:
229
            quotas.issue_and_accept_commission(network)
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)
230 189

  
231 190
        # Create network in Backend if needed
232 191
        if floating_ip_pool:

Also available in: Unified diff