Revision a1d0f147

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

  
156 156

  
157 157
@api.api_method(http_method='POST', user_required=True, logger=log)
158
@transaction.commit_manually
158
@transaction.commit_on_success
159 159
def create_network(request):
160 160
    # Normal Response Code: 202
161 161
    # Error Response Codes: computeFault (400, 500),
......
166 166
    #                       forbidden (403)
167 167
    #                       overLimit (413)
168 168

  
169
    req = utils.get_request_dict(request)
170
    log.info('create_network %s', req)
171

  
172
    user_id = request.user_uniq
169 173
    try:
170
        req = utils.get_request_dict(request)
171
        log.info('create_network %s', req)
172

  
173
        user_id = request.user_uniq
174
        try:
175
            d = req['network']
176
            name = d['name']
177
        except KeyError:
178
            raise faults.BadRequest("Malformed request")
179

  
180
        # Get and validate flavor. Flavors are still exposed as 'type' in the
181
        # API.
182
        flavor = d.get("type", None)
183
        if flavor is None:
184
            raise faults.BadRequest("Missing request parameter 'type'")
185
        elif flavor not in Network.FLAVORS.keys():
186
            raise faults.BadRequest("Invalid network type '%s'" % flavor)
187
        elif flavor not in settings.API_ENABLED_NETWORK_FLAVORS:
188
            raise faults.Forbidden("Can not create network of type '%s'" %
189
                                   flavor)
190

  
191
        public = d.get("public", False)
192
        if public:
193
            raise faults.Forbidden("Can not create a public network.")
194

  
195
        dhcp = d.get('dhcp', True)
196

  
197
        # Get and validate network parameters
198
        subnet = d.get('cidr', '192.168.1.0/24')
199
        subnet6 = d.get('cidr6', None)
200
        gateway = d.get('gateway', None)
201
        gateway6 = d.get('gateway6', None)
202
        # Check that user provided a valid subnet
203
        util.validate_network_params(subnet, gateway, subnet6, gateway6)
204

  
205
        try:
206
            mode, link, mac_prefix, tags = util.values_from_flavor(flavor)
207
            validate_mac(mac_prefix + "0:00:00:00")
208
            network = Network.objects.create(
209
                name=name,
210
                userid=user_id,
211
                subnet=subnet,
212
                subnet6=subnet6,
213
                gateway=gateway,
214
                gateway6=gateway6,
215
                dhcp=dhcp,
216
                flavor=flavor,
217
                mode=mode,
218
                link=link,
219
                mac_prefix=mac_prefix,
220
                tags=tags,
221
                action='CREATE',
222
                state='ACTIVE')
223
        except EmptyPool:
224
            log.error("Failed to allocate resources for network of type: %s",
225
                      flavor)
226
            raise faults.ServiceUnavailable("Failed to allocate network"
227
                                            " resources")
228

  
229
        # Issue commission to Quotaholder and accept it since at the end of
230
        # this transaction the Network object will be created in the DB.
231
        # Note: the following call does a commit!
232
        quotas.issue_and_accept_commission(network)
233
    except:
234
        transaction.rollback()
235
        raise
236
    else:
237
        transaction.commit()
174
        d = req['network']
175
        name = d['name']
176
    except KeyError:
177
        raise faults.BadRequest("Malformed request")
178

  
179
    # Get and validate flavor. Flavors are still exposed as 'type' in the
180
    # API.
181
    flavor = d.get("type", None)
182
    if flavor is None:
183
        raise faults.BadRequest("Missing request parameter 'type'")
184
    elif flavor not in Network.FLAVORS.keys():
185
        raise faults.BadRequest("Invalid network type '%s'" % flavor)
186
    elif flavor not in settings.API_ENABLED_NETWORK_FLAVORS:
187
        raise faults.Forbidden("Can not create network of type '%s'" %
188
                               flavor)
189

  
190
    public = d.get("public", False)
191
    if public:
192
        raise faults.Forbidden("Can not create a public network.")
193

  
194
    dhcp = d.get('dhcp', True)
195

  
196
    # Get and validate network parameters
197
    subnet = d.get('cidr', '192.168.1.0/24')
198
    subnet6 = d.get('cidr6', None)
199
    gateway = d.get('gateway', None)
200
    gateway6 = d.get('gateway6', None)
201
    # Check that user provided a valid subnet
202
    util.validate_network_params(subnet, gateway, subnet6, gateway6)
203

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

  
227
    # Issue commission to Quotaholder and accept it since at the end of
228
    # this transaction the Network object will be created in the DB.
229
    # Note: the following call does a commit!
230
    quotas.issue_and_accept_commission(network)
238 231

  
239 232
    networkdict = network_to_dict(network, request.user_uniq)
240 233
    response = render_network(request, networkdict, status=202)

Also available in: Unified diff