NSX|V3: ensure that network rollback works correctly

In the event that a network creation fails ensure that we do the
rollback correctly. Here the network on the NSX was not deleted
nor the neutron network.

Change-Id: Ib7801958385c4b4ce93e72e6036da043c202e948
This commit is contained in:
Gary Kotton 2017-05-04 04:29:54 -07:00
parent 8f5260689e
commit 8058b53c86
2 changed files with 20 additions and 4 deletions

View File

@ -760,6 +760,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
az = self.get_obj_az_by_hints(net_data)
self._ensure_default_security_group(context, tenant_id)
nsx_net_id = None
if validators.is_attr_set(external) and external:
self._assert_on_external_net_with_qos(net_data)
is_provider_net, net_type, physical_net, vlan_id = (
@ -769,6 +770,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
self._create_network_at_the_backend(context, net_data, az))
is_backend_network = True
try:
rollback_network = False
with db_api.context_manager.writer.using(context):
# Create network in Neutron
created_net = super(NsxV3Plugin, self).create_network(context,
@ -806,6 +808,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
neutron_net_id,
nsx_net_id)
rollback_network = True
if is_backend_network and cfg.CONF.nsx_v3.native_dhcp_metadata:
# Enable native metadata proxy for this network.
tags = self.nsxlib.build_v3_tags_payload(
@ -825,10 +828,12 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin,
except Exception:
with excutils.save_and_reraise_exception():
# Undo creation on the backend
LOG.exception('Failed to create network %s',
created_net['id'])
if net_type != utils.NetworkTypes.L3_EXT:
self.nsxlib.logical_switch.delete(created_net['id'])
LOG.exception('Failed to create network')
if nsx_net_id:
self.nsxlib.logical_switch.delete(nsx_net_id)
if rollback_network:
super(NsxV3Plugin, self).delete_network(
context, created_net['id'])
# this extra lookup is necessary to get the
# latest db model for the extension functions

View File

@ -43,6 +43,7 @@ from neutron_lib.plugins import directory
from oslo_config import cfg
from oslo_utils import uuidutils
from vmware_nsx.api_client import exception as api_exc
from vmware_nsx.common import utils
from vmware_nsx.plugins.nsx_v3 import plugin as nsx_plugin
from vmware_nsx.tests import unit as vmware
@ -245,6 +246,16 @@ class TestNetworksV2(test_plugin.TestNetworksV2, NsxV3PluginTestCaseMixin):
az_hints = net['network']['availability_zone_hints']
self.assertListEqual(az_hints, zone)
def test_network_failure_rollback(self):
cfg.CONF.set_override('native_dhcp_metadata', True, 'nsx_v3')
self.plugin = directory.get_plugin()
with mock.patch.object(self.plugin._port_client, 'create',
side_effect=api_exc.NsxApiException):
self.network()
ctx = context.get_admin_context()
networks = self.plugin.get_networks(ctx)
self.assertListEqual([], networks)
class TestSubnetsV2(test_plugin.TestSubnetsV2, NsxV3PluginTestCaseMixin):