Revision d4993f77

b/snf-cyclades-app/synnefo/api/networks.py
147 147

  
148 148
@util.api_method('POST')
149 149
@quotas.uses_commission
150
@transaction.commit_on_success
150
@transaction.commit_manually
151 151
def create_network(serials, request):
152 152
    # Normal Response Code: 202
153 153
    # Error Response Codes: computeFault (400, 500),
......
158 158
    #                       forbidden (403)
159 159
    #                       overLimit (413)
160 160

  
161
    req = util.get_request_dict(request)
162
    log.info('create_network %s', req)
163

  
164
    try:
165
        d = req['network']
166
        name = d['name']
167
        # TODO: Fix this temp values:
168
        subnet = d.get('cidr', '192.168.1.0/24')
169
        subnet6 = d.get('cidr6', None)
170
        gateway = d.get('gateway', None)
171
        gateway6 = d.get('gateway6', None)
172
        flavor = d.get('type', 'MAC_FILTERED')
173
        public = d.get('public', False)
174
        dhcp = d.get('dhcp', True)
175
    except (KeyError, ValueError):
176
        raise BadRequest('Malformed request.')
177

  
178
    if public:
179
        raise Forbidden('Can not create a public network.')
180

  
181
    if flavor not in Network.FLAVORS.keys():
182
        raise BadRequest("Invalid network flavors %s" % flavor)
183

  
184
    if flavor not in settings.API_ENABLED_NETWORK_FLAVORS:
185
        raise Forbidden("Can not create %s network" % flavor)
186

  
187
    cidr_block = int(subnet.split('/')[1])
188
    if not util.validate_network_size(cidr_block):
189
        raise OverLimit("Unsupported network size.")
190

  
191
    user_id = request.user_uniq
192
    serial = quotas.issue_network_commission(user_id)
193
    serials.append(serial)
194
    # Make the commission accepted, since in the end of this
195
    # transaction the Network will have been created in the DB.
196
    serial.accepted = True
197
    serial.save()
198

  
199 161
    try:
200
        mode, link, mac_prefix, tags = util.values_from_flavor(flavor)
201
        network = Network.objects.create(
202
                name=name,
203
                userid=user_id,
204
                subnet=subnet,
205
                subnet6=subnet6,
206
                gateway=gateway,
207
                gateway6=gateway6,
208
                dhcp=dhcp,
209
                flavor=flavor,
210
                mode=mode,
211
                link=link,
212
                mac_prefix=mac_prefix,
213
                tags=tags,
214
                action='CREATE',
215
                state='PENDING',
216
                serial=serial)
217
    except EmptyPool:
218
        log.error("Failed to allocate resources for network of type: %s",
219
                  flavor)
220
        raise ServiceUnavailable("Failed to allocate resources for network")
221

  
222
    # Create BackendNetwork entries for each Backend
223
    network.create_backend_network()
162
        req = util.get_request_dict(request)
163
        log.info('create_network %s', req)
164

  
165
        try:
166
            d = req['network']
167
            name = d['name']
168
            # TODO: Fix this temp values:
169
            subnet = d.get('cidr', '192.168.1.0/24')
170
            subnet6 = d.get('cidr6', None)
171
            gateway = d.get('gateway', None)
172
            gateway6 = d.get('gateway6', None)
173
            flavor = d.get('type', 'MAC_FILTERED')
174
            public = d.get('public', False)
175
            dhcp = d.get('dhcp', True)
176
        except (KeyError, ValueError):
177
            raise BadRequest('Malformed request.')
178

  
179
        if public:
180
            raise Forbidden('Can not create a public network.')
181

  
182
        if flavor not in Network.FLAVORS.keys():
183
            raise BadRequest("Invalid network flavors %s" % flavor)
184

  
185
        if flavor not in settings.API_ENABLED_NETWORK_FLAVORS:
186
            raise Forbidden("Can not create %s network" % flavor)
187

  
188
        cidr_block = int(subnet.split('/')[1])
189
        if not util.validate_network_size(cidr_block):
190
            raise OverLimit("Unsupported network size.")
191

  
192
        user_id = request.user_uniq
193
        serial = quotas.issue_network_commission(user_id)
194
        serials.append(serial)
195
        # Make the commission accepted, since in the end of this
196
        # transaction the Network will have been created in the DB.
197
        serial.accepted = True
198
        serial.save()
199

  
200
        try:
201
            mode, link, mac_prefix, tags = util.values_from_flavor(flavor)
202
            network = Network.objects.create(
203
                    name=name,
204
                    userid=user_id,
205
                    subnet=subnet,
206
                    subnet6=subnet6,
207
                    gateway=gateway,
208
                    gateway6=gateway6,
209
                    dhcp=dhcp,
210
                    flavor=flavor,
211
                    mode=mode,
212
                    link=link,
213
                    mac_prefix=mac_prefix,
214
                    tags=tags,
215
                    action='CREATE',
216
                    state='PENDING',
217
                    serial=serial)
218
        except EmptyPool:
219
            log.error("Failed to allocate resources for network of type: %s",
220
                      flavor)
221
            raise ServiceUnavailable("Failed to allocate network resources")
222

  
223
        # Create BackendNetwork entries for each Backend
224
        network.create_backend_network()
225
    except:
226
        transaction.rollback()
227
    else:
228
        transaction.commit()
224 229

  
225 230
    # Create the network in the actual backends
226 231
    backend.create_network(network)

Also available in: Unified diff