diff --git a/mogan/engine/flows/create_instance.py b/mogan/engine/flows/create_instance.py index ac20fa16..e9137ad4 100644 --- a/mogan/engine/flows/create_instance.py +++ b/mogan/engine/flows/create_instance.py @@ -177,12 +177,11 @@ class SetInstanceInfoTask(flow_utils.MoganTask): class BuildNetworkTask(flow_utils.MoganTask): """Build network for the instance.""" - def __init__(self, network_api, ironicclient): + def __init__(self, manager): requires = ['instance', 'requested_networks', 'context'] super(BuildNetworkTask, self).__init__(addons=[ACTION], requires=requires) - self.network_api = network_api - self.ironicclient = ironicclient + self.manager = manager # These exception types will trigger the network to be cleaned. self.network_cleaned_exc_types = [ exception.NetworkError, @@ -193,7 +192,7 @@ class BuildNetworkTask(flow_utils.MoganTask): def _build_networks(self, context, instance, requested_networks): 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, detail=True) 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 if vif.get('port_type') == pif.extra.get('port_type'): try: - port = self.network_api.create_port( + port = self.manager.network_api.create_port( context, vif['net_id'], pif.address, instance.uuid) port_dict = port['port'] network_info[port_dict['id']] = { 'network': port_dict['network_id'], 'mac_address': port_dict['mac_address'], 'fixed_ips': port_dict['fixed_ips']} - ironic.plug_vif(self.ironicclient, pif.uuid, + ironic.plug_vif(self.manager.ironicclient, pif.uuid, port_dict['id']) except Exception: # Set network_info here, so we can clean up the created @@ -233,22 +232,6 @@ class BuildNetworkTask(flow_utils.MoganTask): 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): network_info = self._build_networks( context, @@ -265,7 +248,7 @@ class BuildNetworkTask(flow_utils.MoganTask): LOG.debug("Instance %s: cleaning up node networks", instance.uuid) if instance.network_info: - self._destroy_networks(context, instance) + self.manager.destroy_networks(context, instance) # Unset network_info here as we have destroyed it. instance.network_info = {} return True @@ -371,8 +354,7 @@ def get_flow(context, manager, instance, requested_networks, request_spec, instance_flow.add(ScheduleCreateInstanceTask(manager), OnFailureRescheduleTask(manager.engine_rpcapi), SetInstanceInfoTask(manager.ironicclient), - BuildNetworkTask(manager.network_api, - manager.ironicclient), + BuildNetworkTask(manager), CreateInstanceTask(manager.ironicclient)) # Now load (but do not run) the flow using the provided initial data. diff --git a/mogan/engine/manager.py b/mogan/engine/manager.py index fdf1239b..f22dfff5 100644 --- a/mogan/engine/manager.py +++ b/mogan/engine/manager.py @@ -75,7 +75,7 @@ class EngineManager(base_manager.BaseEngineManager): LOG.debug('Instance has been destroyed from under us while ' '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", {'uuid': instance.uuid, 'network_info': str(instance.network_info)}) @@ -213,7 +213,7 @@ class EngineManager(base_manager.BaseEngineManager): if node: try: if node.provision_state in _UNPROVISION_STATES: - self._destroy_networks(context, instance) + self.destroy_networks(context, instance) self._destroy_instance(context, instance) else: self._remove_instance_info_from_node(instance) diff --git a/mogan/tests/unit/engine/flows/test_create_instance_flow.py b/mogan/tests/unit/engine/flows/test_create_instance_flow.py index 7b4327bd..9528ef66 100644 --- a/mogan/tests/unit/engine/flows/test_create_instance_flow.py +++ b/mogan/tests/unit/engine/flows/test_create_instance_flow.py @@ -76,11 +76,9 @@ class CreateInstanceFlowTestCase(base.TestCase): @mock.patch.object(objects.instance.Instance, 'save') @mock.patch.object(create_instance.BuildNetworkTask, '_build_networks') def test_create_network_task_execute(self, mock_build_networks, mock_save): - fake_ironicclient = mock.MagicMock() - fake_network_api = mock.MagicMock() + fake_engine_manager = mock.MagicMock() fake_requested_networks = mock.MagicMock() - task = create_instance.BuildNetworkTask( - fake_network_api, fake_ironicclient) + task = create_instance.BuildNetworkTask(fake_engine_manager) instance_obj = obj_utils.get_test_instance(self.ctxt) mock_build_networks.side_effect = None mock_save.side_effect = None diff --git a/mogan/tests/unit/engine/test_manager.py b/mogan/tests/unit/engine/test_manager.py index 17122057..bc61ac82 100644 --- a/mogan/tests/unit/engine/test_manager.py +++ b/mogan/tests/unit/engine/test_manager.py @@ -37,9 +37,9 @@ class ManageInstanceTestCase(mgr_utils.ServiceSetUpMixin, @mock.patch.object(ironic, 'unplug_vif') @mock.patch.object(ironic, 'get_ports_from_node') @mock.patch.object(network_api.API, 'delete_port') - def test__destroy_networks(self, delete_port_mock, - get_ports_mock, unplug_vif_mock, - refresh_cache_mock): + def test_destroy_networks(self, delete_port_mock, + get_ports_mock, unplug_vif_mock, + refresh_cache_mock): instance = obj_utils.create_test_instance(self.context) delete_port_mock.side_effect = None port = mock.MagicMock() @@ -50,7 +50,7 @@ class ManageInstanceTestCase(mgr_utils.ServiceSetUpMixin, refresh_cache_mock.side_effect = None self._start_service() - self.service._destroy_networks(self.context, instance) + self.service.destroy_networks(self.context, instance) self._stop_service() 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(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, destroy_inst_mock, get_node_mock, refresh_cache_mock):