Improve gateway_ip updation for subnet

Allow update 'gateway_ip' to null or empty for
neutron subnet, and keep the same behavior as
creation.
Also, this change adds some related functional
tests for neutron subnet resource.

Closes-Bug: #1510800
Change-Id: Ib5511402e1d118457356a8add0042be2b157eb24
This commit is contained in:
huangtianhua 2015-10-31 11:26:55 +08:00
parent 100f9153b7
commit 794a39cf67
3 changed files with 65 additions and 15 deletions

View File

@ -137,9 +137,12 @@ class Subnet(neutron.NeutronResource):
), ),
GATEWAY_IP: properties.Schema( GATEWAY_IP: properties.Schema(
properties.Schema.STRING, properties.Schema.STRING,
_('The gateway IP address. Set to any of [ null | ~ | "" ] to ' _('The gateway IP address. Set to any of [ null | ~ | "" ] '
'create the subnet without a gateway. If omitted, the first IP ' 'to create/update a subnet without a gateway. '
'address within the subnet is assigned to the gateway.'), 'If omitted when creation, neutron will assign the first '
'free IP address within the subnet to the gateway '
'automatically. If remove this from template when update, '
'the old gateway IP address will be detached.'),
update_allowed=True update_allowed=True
), ),
ENABLE_DHCP: properties.Schema( ENABLE_DHCP: properties.Schema(
@ -354,6 +357,10 @@ class Subnet(neutron.NeutronResource):
if (self.NAME in prop_diff and if (self.NAME in prop_diff and
prop_diff[self.NAME] is None): prop_diff[self.NAME] is None):
prop_diff[self.NAME] = self.physical_resource_name() prop_diff[self.NAME] = self.physical_resource_name()
# If the new value is '', set to None
self._null_gateway_ip(prop_diff)
self.client().update_subnet( self.client().update_subnet(
self.resource_id, {'subnet': prop_diff}) self.resource_id, {'subnet': prop_diff})

View File

@ -121,9 +121,10 @@ class NeutronSubnetTest(common.HeatTestCase):
'enable_dhcp': True, 'enable_dhcp': True,
'host_routes': [{'destination': '192.168.1.0/24', 'host_routes': [{'destination': '192.168.1.0/24',
'nexthop': '194.168.1.2'}], 'nexthop': '194.168.1.2'}],
"allocation_pools": [ 'gateway_ip': '10.0.3.105',
{"start": "10.0.3.20", "end": "10.0.3.100"}, 'allocation_pools': [
{"start": "10.0.3.110", "end": "10.0.3.200"}]}} {'start': '10.0.3.20', 'end': '10.0.3.100'},
{'start': '10.0.3.110', 'end': '10.0.3.200'}]}}
t = self._test_subnet(u_props=update_props) t = self._test_subnet(u_props=update_props)
neutronV20.find_resourceid_by_name_or_id( neutronV20.find_resourceid_by_name_or_id(

View File

@ -24,10 +24,13 @@ resources:
properties: properties:
network: { get_resource: net } network: { get_resource: net }
cidr: 11.11.11.0/24 cidr: 11.11.11.0/24
gateway_ip: 11.11.11.5
allocation_pools: [{start: 11.11.11.10, end: 11.11.11.250}] allocation_pools: [{start: 11.11.11.10, end: 11.11.11.250}]
outputs: outputs:
alloc_pools: alloc_pools:
value: {get_attr: [subnet, allocation_pools]} value: {get_attr: [subnet, allocation_pools]}
gateway_ip:
value: {get_attr: [subnet, gateway_ip]}
''' '''
@ -36,14 +39,14 @@ class UpdateSubnetTest(functional_base.FunctionalTestsBase):
def setUp(self): def setUp(self):
super(UpdateSubnetTest, self).setUp() super(UpdateSubnetTest, self).setUp()
def get_alloc_pools(self, stack_identifier): def get_outputs(self, stack_identifier, output_key):
stack = self.client.stacks.get(stack_identifier) stack = self.client.stacks.get(stack_identifier)
alloc_pools = self._stack_output(stack, 'alloc_pools') output = self._stack_output(stack, output_key)
return alloc_pools return output
def test_update_allocation_pools(self): def test_update_allocation_pools(self):
stack_identifier = self.stack_create(template=test_template) stack_identifier = self.stack_create(template=test_template)
alloc_pools = self.get_alloc_pools(stack_identifier) alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools')
self.assertEqual([{'start': '11.11.11.10', 'end': '11.11.11.250'}], self.assertEqual([{'start': '11.11.11.10', 'end': '11.11.11.250'}],
alloc_pools) alloc_pools)
@ -52,14 +55,14 @@ class UpdateSubnetTest(functional_base.FunctionalTestsBase):
'allocation_pools: [{start: 11.11.11.10, end: 11.11.11.250}]', 'allocation_pools: [{start: 11.11.11.10, end: 11.11.11.250}]',
'allocation_pools: [{start: 11.11.11.10, end: 11.11.11.100}]') 'allocation_pools: [{start: 11.11.11.10, end: 11.11.11.100}]')
self.update_stack(stack_identifier, templ_other_pool) self.update_stack(stack_identifier, templ_other_pool)
new_alloc_pools = self.get_alloc_pools(stack_identifier) new_alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools')
# the new pools should be the new range # the new pools should be the new range
self.assertEqual([{'start': '11.11.11.10', 'end': '11.11.11.100'}], self.assertEqual([{'start': '11.11.11.10', 'end': '11.11.11.100'}],
new_alloc_pools) new_alloc_pools)
def test_update_allocation_pools_to_empty(self): def test_update_allocation_pools_to_empty(self):
stack_identifier = self.stack_create(template=test_template) stack_identifier = self.stack_create(template=test_template)
alloc_pools = self.get_alloc_pools(stack_identifier) alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools')
self.assertEqual([{'start': '11.11.11.10', 'end': '11.11.11.250'}], self.assertEqual([{'start': '11.11.11.10', 'end': '11.11.11.250'}],
alloc_pools) alloc_pools)
@ -68,13 +71,13 @@ class UpdateSubnetTest(functional_base.FunctionalTestsBase):
'allocation_pools: [{start: 11.11.11.10, end: 11.11.11.250}]', 'allocation_pools: [{start: 11.11.11.10, end: 11.11.11.250}]',
'allocation_pools: []') 'allocation_pools: []')
self.update_stack(stack_identifier, templ_empty_pools) self.update_stack(stack_identifier, templ_empty_pools)
new_alloc_pools = self.get_alloc_pools(stack_identifier) new_alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools')
# new_alloc_pools should be [] # new_alloc_pools should be []
self.assertEqual([], new_alloc_pools) self.assertEqual([], new_alloc_pools)
def test_update_to_no_allocation_pools(self): def test_update_to_no_allocation_pools(self):
stack_identifier = self.stack_create(template=test_template) stack_identifier = self.stack_create(template=test_template)
alloc_pools = self.get_alloc_pools(stack_identifier) alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools')
self.assertEqual([{'start': '11.11.11.10', 'end': '11.11.11.250'}], self.assertEqual([{'start': '11.11.11.10', 'end': '11.11.11.250'}],
alloc_pools) alloc_pools)
@ -83,6 +86,45 @@ class UpdateSubnetTest(functional_base.FunctionalTestsBase):
'allocation_pools: [{start: 11.11.11.10, end: 11.11.11.250}]', 'allocation_pools: [{start: 11.11.11.10, end: 11.11.11.250}]',
'') '')
self.update_stack(stack_identifier, templ_no_pools) self.update_stack(stack_identifier, templ_no_pools)
last_alloc_pools = self.get_alloc_pools(stack_identifier) last_alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools')
# last_alloc_pools should be [] # last_alloc_pools should be []
self.assertEqual([], last_alloc_pools) self.assertEqual([], last_alloc_pools)
def test_update_gateway_ip(self):
stack_identifier = self.stack_create(template=test_template)
gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')
self.assertEqual('11.11.11.5', gw_ip)
# Update gateway_ip
templ_other_gw_ip = test_template.replace(
'gateway_ip: 11.11.11.5', 'gateway_ip: 11.11.11.9')
self.update_stack(stack_identifier, templ_other_gw_ip)
new_gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')
# the gateway_ip should be the new one
self.assertEqual('11.11.11.9', new_gw_ip)
def test_update_gateway_ip_to_empty(self):
stack_identifier = self.stack_create(template=test_template)
gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')
self.assertEqual('11.11.11.5', gw_ip)
# Update gateway_ip to null(resolve to '')
templ_empty_gw_ip = test_template.replace(
'gateway_ip: 11.11.11.5', 'gateway_ip: null')
self.update_stack(stack_identifier, templ_empty_gw_ip)
new_gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')
# new gateway_ip should be None
self.assertIsNone(new_gw_ip)
def test_update_to_no_gateway_ip(self):
stack_identifier = self.stack_create(template=test_template)
gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')
self.assertEqual('11.11.11.5', gw_ip)
# Remove the gateway from template
templ_no_gw_ip = test_template.replace(
'gateway_ip: 11.11.11.5', '')
self.update_stack(stack_identifier, templ_no_gw_ip)
new_gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')
# new gateway_ip should be None
self.assertIsNone(new_gw_ip)