全部产品
云市场

快速入门

更新时间:2019-06-19 20:13:05

本节介绍如何快速使用OOS Python SDK完成常见操作,如创建模板、启动执行、查询执行等。

创建模板

以下代码用于创建模板:

  1. # coding: utf-8
  2. from aliyunsdkcore.client import AcsClient
  3. from aliyunsdkoos.request.v20190601.CreateTemplateRequest import CreateTemplateRequest
  4. client = AcsClient(ak='<AccessKeyId>', secret='<AccessKeySecret>', region_id='cn-hangzhou')
  5. content = '''
  6. {
  7. "FormatVersion": "OOS-2019-06-01",
  8. "Description": "Descirbe instances of given status",
  9. "Parameters": {
  10. "Status": {
  11. "Type": "String",
  12. "Description": "(Required) The status of the Ecs instance."
  13. }
  14. },
  15. "Tasks": [
  16. {
  17. "Properties": {
  18. "Parameters": { "Status": "{{ Status }}" },
  19. "API": "DescribeInstances",
  20. "Service": "ECS"
  21. },
  22. "Name": "describeInstances",
  23. "Action": "ACS::ExecuteAPI"
  24. }
  25. ]
  26. }
  27. '''
  28. def create_template(template_name, content):
  29. """
  30. 创建模板
  31. """
  32. request = CreateTemplateRequest()
  33. request.set_TemplateName(template_name)
  34. request.set_Content(content)
  35. resp = client.do_action_with_exception(request)
  36. return resp
  37. resp = create_template(template_name='MyTemplate', content=content)
  38. print(resp)

启动执行

以下代码用于启动执行:

  1. # coding: utf-8
  2. import json
  3. from aliyunsdkcore.client import AcsClient
  4. from aliyunsdkoos.request.v20190601.StartExecutionRequest import StartExecutionRequest
  5. client = AcsClient(ak='<AccessKeyId>', secret='<AccessKeySecret>', region_id='cn-hangzhou')
  6. def start_execution(template_name, parameters=None):
  7. """
  8. 启动执行
  9. """
  10. request = StartExecutionRequest()
  11. request.set_TemplateName(template_name)
  12. if parameters:
  13. parameters = json.dumps(parameters)
  14. request.set_Parameters(parameters)
  15. resp = client.do_action_with_exception(request)
  16. return resp
  17. resp = start_execution(template_name='MyTemplate', parameters={"Status":"Running"})
  18. print(resp)

查询执行

以下代码用于查询执行:

  1. # coding: utf-8
  2. from aliyunsdkcore.client import AcsClient
  3. from aliyunsdkoos.request.v20190601.ListExecutionsRequest import ListExecutionsRequest
  4. client = AcsClient(ak='<AccessKeyId>', secret='<AccessKeySecret>', region_id='cn-hangzhou')
  5. def list_exexutions(execution_id=None):
  6. """
  7. 查询执行
  8. """
  9. request = ListExecutionsRequest()
  10. if execution_id:
  11. request.set_ExecutionId(execution_id)
  12. resp = client.do_action_with_exception(request)
  13. return resp
  14. resp = list_exexutions(execution_id='<ExecutionId>')
  15. print(resp)