Merge "TrivialFix: Remove the duplicate _destroy_network method"

This commit is contained in:
Jenkins 2017-01-06 06:43:57 +00:00 committed by Gerrit Code Review
commit d383c22477
4 changed files with 16 additions and 36 deletions

View File

@ -177,12 +177,11 @@ class SetInstanceInfoTask(flow_utils.MoganTask):
class BuildNetworkTask(flow_utils.MoganTask): class BuildNetworkTask(flow_utils.MoganTask):
"""Build network for the instance.""" """Build network for the instance."""
def __init__(self, network_api, ironicclient): def __init__(self, manager):
requires = ['instance', 'requested_networks', 'context'] requires = ['instance', 'requested_networks', 'context']
super(BuildNetworkTask, self).__init__(addons=[ACTION], super(BuildNetworkTask, self).__init__(addons=[ACTION],
requires=requires) requires=requires)
self.network_api = network_api self.manager = manager
self.ironicclient = ironicclient
# These exception types will trigger the network to be cleaned. # These exception types will trigger the network to be cleaned.
self.network_cleaned_exc_types = [ self.network_cleaned_exc_types = [
exception.NetworkError, exception.NetworkError,
@ -193,7 +192,7 @@ class BuildNetworkTask(flow_utils.MoganTask):
def _build_networks(self, context, instance, requested_networks): def _build_networks(self, context, instance, requested_networks):
node_uuid = instance.node_uuid node_uuid = instance.node_uuid
ironic_ports = ironic.get_ports_from_node(self.ironicclient, ironic_ports = ironic.get_ports_from_node(self.manager.ironicclient,
node_uuid, node_uuid,
detail=True) detail=True)
LOG.debug(_('Find ports %(ports)s for node %(node)s') % LOG.debug(_('Find ports %(ports)s for node %(node)s') %
@ -213,14 +212,14 @@ class BuildNetworkTask(flow_utils.MoganTask):
# Match the specified port type with physical interface type # Match the specified port type with physical interface type
if vif.get('port_type') == pif.extra.get('port_type'): if vif.get('port_type') == pif.extra.get('port_type'):
try: try:
port = self.network_api.create_port( port = self.manager.network_api.create_port(
context, vif['net_id'], pif.address, instance.uuid) context, vif['net_id'], pif.address, instance.uuid)
port_dict = port['port'] port_dict = port['port']
network_info[port_dict['id']] = { network_info[port_dict['id']] = {
'network': port_dict['network_id'], 'network': port_dict['network_id'],
'mac_address': port_dict['mac_address'], 'mac_address': port_dict['mac_address'],
'fixed_ips': port_dict['fixed_ips']} 'fixed_ips': port_dict['fixed_ips']}
ironic.plug_vif(self.ironicclient, pif.uuid, ironic.plug_vif(self.manager.ironicclient, pif.uuid,
port_dict['id']) port_dict['id'])
except Exception: except Exception:
# Set network_info here, so we can clean up the created # Set network_info here, so we can clean up the created
@ -233,22 +232,6 @@ class BuildNetworkTask(flow_utils.MoganTask):
return network_info return network_info
def _destroy_networks(self, context, instance):
LOG.debug("unplug: instance_uuid=%(uuid)s vif=%(network_info)s",
{'uuid': instance.uuid,
'network_info': str(instance.network_info)})
ports = instance.network_info.keys()
for port in ports:
self.network_api.delete_port(context, port, instance.uuid)
ironic_ports = ironic.get_ports_from_node(self.ironicclient,
instance.node_uuid,
detail=True)
for pif in ironic_ports:
if 'vif_port_id' in pif.extra:
ironic.unplug_vif(self.ironicclient, pif.uuid)
def execute(self, context, instance, requested_networks): def execute(self, context, instance, requested_networks):
network_info = self._build_networks( network_info = self._build_networks(
context, context,
@ -265,7 +248,7 @@ class BuildNetworkTask(flow_utils.MoganTask):
LOG.debug("Instance %s: cleaning up node networks", LOG.debug("Instance %s: cleaning up node networks",
instance.uuid) instance.uuid)
if instance.network_info: if instance.network_info:
self._destroy_networks(context, instance) self.manager.destroy_networks(context, instance)
# Unset network_info here as we have destroyed it. # Unset network_info here as we have destroyed it.
instance.network_info = {} instance.network_info = {}
return True return True
@ -375,8 +358,7 @@ def get_flow(context, manager, instance, requested_networks, request_spec,
instance_flow.add(ScheduleCreateInstanceTask(manager), instance_flow.add(ScheduleCreateInstanceTask(manager),
OnFailureRescheduleTask(manager.engine_rpcapi), OnFailureRescheduleTask(manager.engine_rpcapi),
SetInstanceInfoTask(manager.ironicclient), SetInstanceInfoTask(manager.ironicclient),
BuildNetworkTask(manager.network_api, BuildNetworkTask(manager),
manager.ironicclient),
CreateInstanceTask(manager.ironicclient)) CreateInstanceTask(manager.ironicclient))
# Now load (but do not run) the flow using the provided initial data. # Now load (but do not run) the flow using the provided initial data.

View File

@ -75,7 +75,7 @@ class EngineManager(base_manager.BaseEngineManager):
LOG.debug('Instance has been destroyed from under us while ' LOG.debug('Instance has been destroyed from under us while '
'trying to set it to ERROR', instance=instance) 'trying to set it to ERROR', instance=instance)
def _destroy_networks(self, context, instance): def destroy_networks(self, context, instance):
LOG.debug("unplug: instance_uuid=%(uuid)s vif=%(network_info)s", LOG.debug("unplug: instance_uuid=%(uuid)s vif=%(network_info)s",
{'uuid': instance.uuid, {'uuid': instance.uuid,
'network_info': str(instance.network_info)}) 'network_info': str(instance.network_info)})
@ -213,7 +213,7 @@ class EngineManager(base_manager.BaseEngineManager):
if node: if node:
try: try:
if node.provision_state in _UNPROVISION_STATES: if node.provision_state in _UNPROVISION_STATES:
self._destroy_networks(context, instance) self.destroy_networks(context, instance)
self._destroy_instance(context, instance) self._destroy_instance(context, instance)
else: else:
self._remove_instance_info_from_node(instance) self._remove_instance_info_from_node(instance)

View File

@ -76,11 +76,9 @@ class CreateInstanceFlowTestCase(base.TestCase):
@mock.patch.object(objects.instance.Instance, 'save') @mock.patch.object(objects.instance.Instance, 'save')
@mock.patch.object(create_instance.BuildNetworkTask, '_build_networks') @mock.patch.object(create_instance.BuildNetworkTask, '_build_networks')
def test_create_network_task_execute(self, mock_build_networks, mock_save): def test_create_network_task_execute(self, mock_build_networks, mock_save):
fake_ironicclient = mock.MagicMock() fake_engine_manager = mock.MagicMock()
fake_network_api = mock.MagicMock()
fake_requested_networks = mock.MagicMock() fake_requested_networks = mock.MagicMock()
task = create_instance.BuildNetworkTask( task = create_instance.BuildNetworkTask(fake_engine_manager)
fake_network_api, fake_ironicclient)
instance_obj = obj_utils.get_test_instance(self.ctxt) instance_obj = obj_utils.get_test_instance(self.ctxt)
mock_build_networks.side_effect = None mock_build_networks.side_effect = None
mock_save.side_effect = None mock_save.side_effect = None

View File

@ -37,9 +37,9 @@ class ManageInstanceTestCase(mgr_utils.ServiceSetUpMixin,
@mock.patch.object(ironic, 'unplug_vif') @mock.patch.object(ironic, 'unplug_vif')
@mock.patch.object(ironic, 'get_ports_from_node') @mock.patch.object(ironic, 'get_ports_from_node')
@mock.patch.object(network_api.API, 'delete_port') @mock.patch.object(network_api.API, 'delete_port')
def test__destroy_networks(self, delete_port_mock, def test_destroy_networks(self, delete_port_mock,
get_ports_mock, unplug_vif_mock, get_ports_mock, unplug_vif_mock,
refresh_cache_mock): refresh_cache_mock):
instance = obj_utils.create_test_instance(self.context) instance = obj_utils.create_test_instance(self.context)
delete_port_mock.side_effect = None delete_port_mock.side_effect = None
port = mock.MagicMock() port = mock.MagicMock()
@ -50,7 +50,7 @@ class ManageInstanceTestCase(mgr_utils.ServiceSetUpMixin,
refresh_cache_mock.side_effect = None refresh_cache_mock.side_effect = None
self._start_service() self._start_service()
self.service._destroy_networks(self.context, instance) self.service.destroy_networks(self.context, instance)
self._stop_service() self._stop_service()
delete_port_mock.assert_called_once_with( delete_port_mock.assert_called_once_with(
@ -111,7 +111,7 @@ class ManageInstanceTestCase(mgr_utils.ServiceSetUpMixin,
@mock.patch.object(ironic, 'get_node_by_instance') @mock.patch.object(ironic, 'get_node_by_instance')
@mock.patch.object(manager.EngineManager, '_destroy_instance') @mock.patch.object(manager.EngineManager, '_destroy_instance')
@mock.patch.object(manager.EngineManager, '_destroy_networks') @mock.patch.object(manager.EngineManager, 'destroy_networks')
def test_delete_instance(self, destroy_net_mock, def test_delete_instance(self, destroy_net_mock,
destroy_inst_mock, get_node_mock, destroy_inst_mock, get_node_mock,
refresh_cache_mock): refresh_cache_mock):