fixes bug 919158 , default ip blocks are now optional

Change-Id: Ic15f18a7f56992afa214c667a84c834fe58566ea
This commit is contained in:
rajarammallya 2012-01-23 11:37:32 +05:30
parent 0894193d12
commit 94469fc379
5 changed files with 41 additions and 17 deletions

View File

@ -33,10 +33,11 @@ db_api_implementation = "melange.db.sqlalchemy.api"
# Path to the extensions
api_extensions_path = melange/extensions
# Cidr used for auto creating private ip block in a network
default_cidr = 10.0.0.0/24
# Cidr for auto creating first ip block in a network
# If unspecified, auto creating is turned off
# default_cidr = 10.0.0.0/24
#IPV6 Generator Factory
#IPV6 Generator Factory, defaults to rfc2462
#ipv6_generator=melange.ipv6.tenant_based_generator.TenantBasedIpV6Generator
#DNS info for a data_center

View File

@ -985,7 +985,11 @@ class Network(ModelBase):
try:
return cls.find_by(id=id, tenant_id=tenant_id)
except ModelNotFoundError:
ip_block = IpBlock.create(cidr=config.Config.get('default_cidr'),
default_cidr = config.Config.get('default_cidr', None)
if not default_cidr:
raise ModelNotFoundError(_("Network %s not found") % id)
ip_block = IpBlock.create(cidr=default_cidr,
network_id=id,
tenant_id=tenant_id,
type=IpBlock.PRIVATE_TYPE)

View File

@ -470,6 +470,9 @@ class TestInterfaceCLI(tests.BaseTest):
def test_create(self):
factory_models.MacAddressRangeFactory()
factory_models.IpBlockFactory(network_id="network_id",
tenant_id="tenant_id")
exitcode, out, err = run("interface create vif_id=vif_id "
"tenant_id=tenant_id "
"device_id=device_id "

View File

@ -1999,6 +1999,17 @@ class TestNetwork(tests.BaseTest):
self.assertEqual(network.ip_blocks[0].network_id, '1')
self.assertEqual(network.ip_blocks[0].type, 'private')
def test_find_or_create_raises_err_if_no_default_cidr_and_net_blocks(self):
noise_ip_block = factory_models.PublicIpBlockFactory(network_id="9999",
tenant_id="tnt")
with unit.StubConfig(default_cidr=None):
self.assertRaisesExcMessage(models.ModelNotFoundError,
"Network 100 not found",
models.Network.find_or_create_by,
id="100",
tenant_id="tnt")
def test_allocate_ip_to_allocate_both_ipv4_and_ipv6_addresses(self):
ipv4_block = factory_models.PublicIpBlockFactory(network_id="1",
cidr="10.0.0.0/24",

View File

@ -1966,6 +1966,9 @@ class TestInterfaceIpAllocationsController(ControllerTestBase):
self.assertEqual(interface.virtual_interface_id, "123")
def test_create_makes_network_owner_the_interface_owner_by_default(self):
network_1_block = factory_models.IpBlockFactory(tenant_id="tnt_id",
network_id="1")
path = "/ipam/tenants/tnt_id/networks/1/interfaces/123/ip_allocations"
response = self.app.post_json(path)
@ -2049,14 +2052,15 @@ class TestInterfaceIpAllocationsController(ControllerTestBase):
response.json['ip_addresses'])
def test_create_when_network_not_found_creates_default_cidr_block(self):
response = self.app.post("/ipam/tenants/tnt_id/networks/1"
"/interfaces/123/ip_allocations")
with unit.StubConfig(default_cidr="10.0.0.0/24"):
response = self.app.post("/ipam/tenants/tnt_id/networks/1"
"/interfaces/123/ip_allocations")
self.assertEqual(response.status_int, 201)
ip_address_json = response.json['ip_addresses'][0]
created_block = models.IpAddress.find(ip_address_json['id']).ip_block
self.assertEqual(created_block.network_id, "1")
self.assertEqual(created_block.cidr, config.Config.get('default_cidr'))
self.assertEqual(created_block.cidr, "10.0.0.0/24")
self.assertEqual(created_block.type, "private")
self.assertEqual(created_block.tenant_id, "tnt_id")
@ -2217,20 +2221,21 @@ class TestInterfacesController(ControllerTestBase):
self.assertEquals(ipv6_address.interface_id, created_interface.id)
def test_create_when_network_not_found_creates_default_cidr_block(self):
self.app.post_json("/ipam/interfaces",
{'interface': {
'id': "virt_iface",
'device_id': "instance",
'tenant_id': "tnt_id",
'network': {'id': "net1"},
}
})
interface = models.Interface.find_by(virtual_interface_id='virt_iface')
with unit.StubConfig(default_cidr="10.0.0.0/24"):
self.app.post_json("/ipam/interfaces",
{'interface': {
'id': "virt_iface",
'device_id': "instance",
'tenant_id': "tnt_id",
'network': {'id': "net1"},
}
})
interface = models.Interface.find_by(virtual_interface_id='virt_iface')
created_block = models.IpAddress.find_by(
interface_id=interface.id).ip_block
self.assertEqual(created_block.network_id, "net1")
self.assertEqual(created_block.cidr, config.Config.get('default_cidr'))
self.assertEqual(created_block.cidr, "10.0.0.0/24")
self.assertEqual(created_block.type, "private")
self.assertEqual(created_block.tenant_id, "tnt_id")