您的位置:首页 > 运维架构

openstack 工作流workflows使用原理讲解

2016-08-03 14:38 369 查看
原文链接:openstack 工作流workflows使用原理讲解

Workflows

工作流是复杂的forms(表单)和tabs,每一个workflow必须包含 Workflow,Step 和 Action

下面举例讲解workflow用法:

接下来的例子讲解了数据是如何从urls、views、workflows、templates之间互相传递的

在 urls.py中, 定义了一个参数. 例如. resource_class_id.

RESOURCE_CLASS = r'^(?P<resource_class_id>[^/]+)/%s$'

urlpatterns = patterns(
'',
url(RESOURCE_CLASS % 'update', UpdateView.as_view(), name='update'))


在views.py中,我们可以传递数据到template(模板)和action(form)中。(action 也能够传递数据到get_context_data 方法或者template中)

class UpdateView(workflows.WorkflowView):
workflow_class = UpdateResourceClass

def get_context_data(self, **kwargs):
context = super(UpdateView, self).get_context_data(**kwargs)
# url中的数据通常在self.kwargs中,这里我们可以将数据传递给template.url
context["resource_class_id"] = self.kwargs['resource_class_id']
# 数据来源于Workflow's Steps,且保存在context['workflow'].context列表中,我们同样可以在template 中使用它们
return context

def _get_object(self, *args, **kwargs):
#url中的数据通常在self.kwargs中,我们能够在这里加载感兴趣的对象
resource_class_id = self.kwargs['resource_class_id']
# eg:my_objects = api.nova.get_by_id(resource_class_id)

def get_initial(self):
resource_class = self._get_object()
# 此处的数据可以被Action的方法以及Workflow's handle方法使用,但是steps必须depend on该值
return {'resource_class_id': resource_class.id,
'name': resource_class.name,
'service_type': resource_class.service_type}


在workflows.py中,我们处理数据,workflows本质就是一个更加复杂的django form(表单)

class ResourcesAction(workflows.Action):
# 下述定义的name域值 在所有的action 方法中都可以获取
# 假若我们期望此值能够在其他steps或者其他workflow中使用,它必须来源于当前step,且建立depend on在其他step中

name = forms.CharField(max_length=255,
label=_("Testing Name"),
help_text="",
required=True)

def handle(self, request, data):
pass
# 如果想使用url中参数值,在该Action对应的step中必须建立depend on关系
# 可以self.initial['resource_class_id'] or data['resource_class_id']获取值

# 如果我们想使用其他step中的数据,那么其他step 必须contribute 数据,且两个step之间是有序的

class UpdateResources(workflows.Step):
# 此处传递Workflow 的数据到action方法handle/clean中,action中想要使用的值,此处depends_on 必须要定义
# Workflow 的context 数据包括url中的数据以及从其他step中contributed 过来的数据
depends_on = ("resource_class_id",)

# 通过contributes 参数,此处的数据可以被其他workflow或者其他step使用,值得注意的是,object_ids key需要手动添加到contributes 中

contributes = ("resources_object_ids", "name")

def contribute(self, data, context):
# 此处能获取workflow的http request数据
request = self.workflow.request
if data:
# 只有在action中定义的数据此处才能获取,如果想获取其他值,则需要覆盖contribute 方法,手动添加到字典里
context["resources_object_ids"] =\
request.POST.getlist("resources_object_ids")

# 合并上面传递来的数据,也可以交给父类去合并
context.update(data)
return context

class UpdateResourceClass(workflows.Workflow):
default_steps = (UpdateResources,)

def handle(self, request, data):
pass
# 这个方法在最后执行(所有Action的handle方法之后)
# 此处可以使用step中所有'contributes=' 和'depends_on=' 的数据
# 此处可以处理复杂的业务逻辑

#此处可用值: data["resources_object_ids"], data["name"] data["resources_class_id"]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐