Commit 584f7d9a authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

Make 'type' parameter for network-create mandatory

Do not use a default value (was "MAC_FILTERED") for "type" parameter of
"network_create" API method. The client must explicitly choose the
network type.
parent 1a1571db
......@@ -164,33 +164,38 @@ def create_network(serials, request):
req = util.get_request_dict(request)
log.info('create_network %s', req)
user_id = request.user_uniq
try:
d = req['network']
name = d['name']
# TODO: Fix this temp values:
subnet = d.get('cidr', '192.168.1.0/24')
subnet6 = d.get('cidr6', None)
gateway = d.get('gateway', None)
gateway6 = d.get('gateway6', None)
flavor = d.get('type', 'MAC_FILTERED')
public = d.get('public', False)
dhcp = d.get('dhcp', True)
except (KeyError, ValueError):
raise BadRequest('Malformed request.')
except KeyError:
raise BadRequest("Malformed request")
# Get and validate flavor. Flavors are still exposed as 'type' in the
# API.
flavor = d.get("type", None)
if flavor is None:
raise BadRequest("Missing request parameter 'type'")
elif flavor not in Network.FLAVORS.keys():
raise BadRequest("Invalid network type '%s'" % flavor)
elif flavor not in settings.API_ENABLED_NETWORK_FLAVORS:
raise Forbidden("Can not create network of type '%s'" % flavor)
public = d.get("public", False)
if public:
raise Forbidden('Can not create a public network.')
raise Forbidden("Can not create a public network.")
if flavor not in Network.FLAVORS.keys():
raise BadRequest("Invalid network flavors %s" % flavor)
if flavor not in settings.API_ENABLED_NETWORK_FLAVORS:
raise Forbidden("Can not create %s network" % flavor)
dhcp = d.get('dhcp', True)
# Get and validate network parameters
subnet = d.get('cidr', '192.168.1.0/24')
subnet6 = d.get('cidr6', None)
gateway = d.get('gateway', None)
gateway6 = d.get('gateway6', None)
# Check that user provided a valid subnet
util.validate_network_params(subnet, gateway, subnet6, gateway6)
user_id = request.user_uniq
# Issue commission
serial = quotas.issue_network_commission(user_id)
serials.append(serial)
# Make the commission accepted, since in the end of this
......
......@@ -72,8 +72,8 @@ class NetworkAPITest(BaseAPITest):
def test_create_network_1(self, mrapi):
request = {
'network': {'name': 'foo'}
}
'network': {'name': 'foo', "type": "MAC_FILTERED"}
}
response = self.post('/api/v1.1/networks/', 'user1',
json.dumps(request), 'json')
self.assertEqual(response.status_code, 202)
......@@ -96,10 +96,12 @@ class NetworkAPITest(BaseAPITest):
self.assertEqual(len(Network.objects.filter(userid='user1')), 0)
def test_invalid_data_2(self, mrapi):
"""Test invalid subnet"""
"""Test invalid data/subnet"""
request = {
'network': {'name': 'foo', 'cidr': '10.0.0.0/8'}
}
'network': {'name': 'foo',
'cidr': '10.0.0.0/8', "type":
"MAC_FILTERED"}
}
response = self.post('/api/v1.1/networks/', 'user1',
json.dumps(request), 'json')
self.assertFault(response, 413, "overLimit")
......@@ -107,7 +109,9 @@ class NetworkAPITest(BaseAPITest):
def test_invalid_data_3(self, mrapi):
"""Test unauthorized to create public network"""
request = {
'network': {'name': 'foo', 'public': True}
'network': {'name': 'foo',
"public": "True",
"type": "MAC_FILTERED"}
}
response = self.post('/api/v1.1/networks/', 'user1',
json.dumps(request), 'json')
......@@ -125,8 +129,10 @@ class NetworkAPITest(BaseAPITest):
def test_invalid_subnet(self, mrapi):
"""Test invalid subnet"""
request = {
'network': {'name': 'foo', 'cidr': '10.0.0.10/27'}
}
'network': {'name': 'foo',
'cidr': '10.0.0.10/27',
"type": "MAC_FILTERED"}
}
response = self.post('/api/v1.1/networks/', 'user1',
json.dumps(request), 'json')
self.assertBadRequest(response)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment