From e00a398f84562f1a0da69c8c0fd33db538365dc3 Mon Sep 17 00:00:00 2001 From: Alessio Ababilov Date: Mon, 16 Jul 2012 14:07:29 +0300 Subject: Implement network creation in compute API Implements blueprint os-api-network-create The ability to create new networks is currently only exposed by the nova-manage CLI. Here we add support for network creation in the os-networks API extension. With the exception of num_networks and network_size, all the parameters supported by 'nova-manage network create' are supported. Only a single network may be created by each API call. To avoid code duplication, the nova-manage code is refactored and moved into NetworkManager so that it can be re-used by the API. DocImpact Change-Id: I682d498ab35ea43b553b64e13e677fe9eeb8e08b --- .../api/openstack/compute/contrib/test_networks.py | 62 ++++++++++++++++++++++ nova/tests/fake_flags.py | 1 + nova/tests/network/test_linux_net.py | 1 + nova/tests/test_nova_manage.py | 4 +- 4 files changed, 66 insertions(+), 2 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/compute/contrib/test_networks.py b/nova/tests/api/openstack/compute/contrib/test_networks.py index efbb7ceba..808493f1b 100644 --- a/nova/tests/api/openstack/compute/contrib/test_networks.py +++ b/nova/tests/api/openstack/compute/contrib/test_networks.py @@ -15,6 +15,10 @@ # under the License. import copy +import itertools +import math +import netaddr +import uuid import webob @@ -23,6 +27,11 @@ from nova import exception from nova import test from nova.tests.api.openstack import fakes +from nova import flags + + +FLAGS = flags.FLAGS + FAKE_NETWORKS = [ { @@ -75,6 +84,15 @@ FAKE_USER_NETWORKS = [ }, ] +NEW_NETWORK = { + "network": { + "bridge_interface": "eth0", + "cidr": "10.20.105.0/24", + "label": "new net 111", + "vlan_start": 111, + } +} + class FakeNetworkAPI(object): @@ -117,6 +135,32 @@ class FakeNetworkAPI(object): return network raise exception.NetworkNotFound() + def create(self, context, **kwargs): + subnet_bits = int(math.ceil(math.log(kwargs.get( + 'network_size', FLAGS.network_size), 2))) + fixed_net_v4 = netaddr.IPNetwork(kwargs['cidr']) + prefixlen_v4 = 32 - subnet_bits + subnets_v4 = list(fixed_net_v4.subnet( + prefixlen_v4, + count=kwargs.get('num_networks', FLAGS.num_networks))) + new_networks = [] + new_id = max((net['id'] for net in self.networks)) + for index, subnet_v4 in enumerate(subnets_v4): + new_id += 1 + net = {'id': new_id, 'uuid': str(uuid.uuid4())} + + net['cidr'] = str(subnet_v4) + net['netmask'] = str(subnet_v4.netmask) + net['gateway'] = kwargs.get('gateway') or str(subnet_v4[1]) + net['broadcast'] = str(subnet_v4.broadcast) + net['dhcp_start'] = str(subnet_v4[2]) + + for key in FAKE_NETWORKS[0].iterkeys(): + net.setdefault(key, kwargs.get(key)) + new_networks.append(net) + self.networks += new_networks + return new_networks + class NetworksTest(test.TestCase): @@ -204,3 +248,21 @@ class NetworksTest(test.TestCase): req.environ["nova.context"].is_admin = True res_dict = self.controller.show(req, uuid) self.assertEqual(res_dict['network']['project_id'], 'fake') + + def test_network_create(self): + req = fakes.HTTPRequest.blank('/v2/1234/os-networks') + res_dict = self.controller.create(req, NEW_NETWORK) + self.assertTrue('network' in res_dict) + uuid = res_dict['network']['id'] + req = fakes.HTTPRequest.blank('/v2/1234/os-networks/%s' % uuid) + res_dict = self.controller.show(req, uuid) + self.assertTrue(res_dict['network']['label']. + startswith(NEW_NETWORK['network']['label'])) + + def test_network_create_large(self): + req = fakes.HTTPRequest.blank('/v2/1234/os-networks') + large_network = copy.deepcopy(NEW_NETWORK) + large_network['network']['cidr'] = '128.0.0.0/4' + res_dict = self.controller.create(req, large_network) + self.assertEqual(res_dict['network']['cidr'], + large_network['network']['cidr']) diff --git a/nova/tests/fake_flags.py b/nova/tests/fake_flags.py index f6d9496b1..f0fde3588 100644 --- a/nova/tests/fake_flags.py +++ b/nova/tests/fake_flags.py @@ -39,6 +39,7 @@ def set_defaults(conf): conf.set_default('iscsi_num_targets', 8) conf.set_default('network_size', 8) conf.set_default('num_networks', 2) + conf.set_default('vlan_interface', 'eth0') conf.set_default('rpc_backend', 'nova.openstack.common.rpc.impl_fake') conf.set_default('sql_connection', "sqlite://") conf.set_default('sqlite_synchronous', False) diff --git a/nova/tests/network/test_linux_net.py b/nova/tests/network/test_linux_net.py index 94b2ac8d9..47b853a8a 100644 --- a/nova/tests/network/test_linux_net.py +++ b/nova/tests/network/test_linux_net.py @@ -373,6 +373,7 @@ class LinuxNetworkTestCase(test.TestCase): "bridge_interface": "base_interface", "vlan": "fake" } + self.flags(vlan_interface="") driver.plug(network, "fakemac") self.assertEqual(info['passed_interface'], "base_interface") self.flags(vlan_interface="override_interface") diff --git a/nova/tests/test_nova_manage.py b/nova/tests/test_nova_manage.py index 805ac8e0d..6cadeb2ef 100644 --- a/nova/tests/test_nova_manage.py +++ b/nova/tests/test_nova_manage.py @@ -171,13 +171,13 @@ class NetworkCommandsTestCase(test.TestCase): fake_create_networks) self.commands.create( label='Test', - fixed_range_v4='10.2.0.0/24', + cidr='10.2.0.0/24', num_networks=1, network_size=256, multi_host='F', vlan_start=200, vpn_start=2000, - fixed_range_v6='fd00:2::/120', + cidr_v6='fd00:2::/120', gateway='10.2.0.1', gateway_v6='fd00:2::22', bridge='br200', -- cgit