列举所有

接口

easy_list

参数

参数 类型 是否必须 描述
resource_type str 资源类型,目前仅允许列举”jobs”, “clusters”, “tasks”, “images”, “instances”
*resource_info tuple 位置参数,包含列举资源需要提供的其他所有信息,如列举”instances”时需要提供job_id和task_name
**filters dict 键值对参数,可以设置多个资源过滤条件

返回值

easy_list方法返回一个 list 对象,其中包含了 resource_type 中所有符合过滤条件的资源。

例子

  1. import time
  2. import batchcompute
  3. from batchcompute import CN_SHENZHEN as REGION
  4. from batchcompute import Client, ClientError
  5. ACCESS_KEY_ID = 'Your Access Key Id'
  6. ACCESS_KEY_SECRET = 'Your Access Key Secret'
  7. client = Client(REGION, ACCESS_KEY_ID, ACCESS_KEY_SECRET)
  8. def easy_list():
  9. try:
  10. # List all jobs which named "PythonSDK" and have a description of "test list job".
  11. for job in client.easy_list("jobs", Name="PythonSDK", Description="test list job"):
  12. print (job.Name, job.Id)
  13. # List all jobs which named "PythonSDK" and have a description of "test list job".
  14. # filters can be also unpacked from a dict.
  15. job_filters = {
  16. "Name": "PythonSDK",
  17. "Description": "test list job"
  18. }
  19. for job in client.easy_list("jobs", **job_filters):
  20. print (job.Name, job.Id)
  21. # List all jobs whose state is "Waiting" or "Running".
  22. # `filetrs` with a tuple value also available.
  23. for job in client.easy_list('jobs', State=['Waiting', 'Running']):
  24. print (job.Name, job.Id)
  25. # List all jobs whose state is "Waiting" or "Running".
  26. # `filters` with a function value defined by users.
  27. state_filter = lambda state: state in ['Waiting', 'Running']
  28. for job in client.easy_list('jobs', State=state_filter):
  29. print (job.Name, job.Id)
  30. # List all "Runing" tasks in a job.
  31. job_id = 'job-xxx'
  32. client.easy_list('tasks', job_id, State='Running')
  33. # List all "Running" instances in a task.
  34. job_id = 'job-xxx'
  35. task_name = 'Echo'
  36. client.easy_list('instances', job_id, task_name, State='Running')
  37. except ClientError, e:
  38. print (e.get_status_code(), e.get_code(), e.get_requestid(), e.get_msg())
  39. if __name__ == '__main__':
  40. easy_list()