def get_flow(context, workflow_engine, operation_type, plan, provider, checkpoint): protectable_registry = ProtectableRegistry() resources = set(Resource(**item) for item in plan.get("resources")) resource_graph = protectable_registry.build_graph(context, resources) checkpoint.resource_graph = resource_graph checkpoint.commit() flow_name = "Protect_" + plan.get('id') protection_flow = workflow_engine.build_flow(flow_name, 'linear') plugins = provider.load_plugins() resources_task_flow = resource_flow.build_resource_flow( operation_type=operation_type, context=context, workflow_engine=workflow_engine, resource_graph=resource_graph, plugins=plugins, parameters=plan.get('parameters'), ) workflow_engine.add_tasks( protection_flow, InitiateProtectTask(), resources_task_flow, CompleteProtectTask(), ) flow_engine = workflow_engine.get_engine(protection_flow, store={'checkpoint': checkpoint}) return flow_engine
def get_flow(context, workflow_engine, operation_type, plan, provider, checkpoint): protectable_registry = ProtectableRegistry() resources = set(Resource(**item) for item in plan.get("resources")) resource_graph = protectable_registry.build_graph(context, resources) checkpoint.resource_graph = resource_graph checkpoint.commit() flow_name = "Protect_" + plan.get('id') protection_flow = workflow_engine.build_flow(flow_name, 'linear') plugins = provider.load_plugins() resources_task_flow = resource_flow.build_resource_flow( operation_type=operation_type, context=context, workflow_engine=workflow_engine, resource_graph=resource_graph, plugins=plugins, parameters=plan.get('parameters'), ) workflow_engine.add_tasks( protection_flow, InitiateProtectTask(), resources_task_flow, CompleteProtectTask(), ) flow_engine = workflow_engine.get_engine(protection_flow, store={ 'checkpoint': checkpoint }) return flow_engine
class ProtectableRegistryTest(base.TestCase): def setUp(self): super(ProtectableRegistryTest, self).setUp() self.protectable_registry = ProtectableRegistry() self._fake_plugin = _FakeProtectablePlugin(None) self.protectable_registry.register_plugin(self._fake_plugin) def test_graph_building(self): A = Resource(_FAKE_TYPE, "A", 'nameA') B = Resource(_FAKE_TYPE, "B", 'nameB') C = Resource(_FAKE_TYPE, "C", 'nameC') test_matrix = ( ( {A: [B], B: [C], C: []}, (A, C) ), ( {A: [C], B: [C], C: []}, (A, C) ), ) for g, resources in test_matrix: self._fake_plugin.graph = g result_graph = self.protectable_registry.build_graph(None, resources) self.assert_graph(result_graph, g) self.protectable_registry._protectable_map = {} def assert_graph(self, g, g_dict): for item in g: expected = set(g_dict[item.value]) found = set(child.value for child in item.child_nodes) self.assertEqual(found, expected) self.assert_graph(item.child_nodes, g_dict)
def build_task_flow(self, ctx): cntxt = ctx["context"] workflow_engine = ctx["workflow_engine"] operation = ctx["operation_type"] resource_context = None resource_graph = None if operation == constants.OPERATION_PROTECT: plan = ctx["plan"] task_flow = workflow_engine.build_flow(flow_name=plan.get('id')) resources = plan.get('resources') parameters = plan.get('parameters') graph_resources = [] for resource in resources: graph_resources.append( Resource(type=resource['type'], id=resource['id'], name=resource['name'])) # TODO(luobin): pass registry in ctx registry = ProtectableRegistry() registry.load_plugins() resource_graph = registry.build_graph(cntxt, graph_resources) resource_context = ResourceGraphContext( cntxt=cntxt, operation=operation, workflow_engine=workflow_engine, task_flow=task_flow, plugin_map=self._plugin_map, parameters=parameters) if operation == constants.OPERATION_RESTORE: restore = ctx['restore'] task_flow = workflow_engine.build_flow(flow_name=restore.get('id')) checkpoint = ctx["checkpoint"] resource_graph = checkpoint.resource_graph parameters = restore.get('parameters') heat_template = ctx["heat_template"] resource_context = ResourceGraphContext( cntxt=cntxt, checkpoint=checkpoint, operation=operation, workflow_engine=workflow_engine, task_flow=task_flow, plugin_map=self._plugin_map, parameters=parameters, heat_template=heat_template) if operation == constants.OPERATION_DELETE: checkpoint = ctx['checkpoint'] task_flow = workflow_engine.build_flow(flow_name=checkpoint.id) resource_graph = checkpoint.resource_graph resource_context = ResourceGraphContext( cntxt=cntxt, checkpoint=checkpoint, operation=operation, workflow_engine=workflow_engine, task_flow=task_flow, plugin_map=self._plugin_map) # TODO(luobin): for other type operations walker_listener = ResourceGraphWalkerListener(resource_context) graph_walker = GraphWalker() graph_walker.register_listener(walker_listener) graph_walker.walk_graph(resource_graph) if operation == constants.OPERATION_PROTECT: return { "task_flow": walker_listener.context.task_flow, "status_getters": walker_listener.context.status_getters, "resource_graph": resource_graph } if operation == constants.OPERATION_RESTORE: return {"task_flow": walker_listener.context.task_flow} if operation == constants.OPERATION_DELETE: return { "task_flow": walker_listener.context.task_flow, "status_getters": walker_listener.context.status_getters }