
# 查询实例任务列表 - ListInstanceTasks
#### 功能介绍
用户可以调用此接口查询设备接入实例任务列表。
接口约束：仅支持查询180天内的实例任务，实例任务的最大数量为20条。
#### 调用方法
请参见[如何调用API](https://support.huaweicloud.com/api-iothub/iot_06_v5_0004.html)。
#### 授权信息
账号具备所有API的调用权限，如果使用账号下的IAM用户调用当前API，该IAM用户需具备调用API所需的权限。
- 如果使用角色与策略授权，具体权限要求请参见[权限和授权项](https://support.huaweicloud.com/api-iothub/iot_06_v5_0201.html)。
- 如果使用身份策略授权，需具备如下身份策略权限。
  
  | 授权项                          | 访问级别 | 资源类型（\*为必须） | 条件键                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             | 别名 | 依赖的授权项 |
  |:---|:---|:---|:---|:---|:---|
  | iotda:instance:queryTaskList | List | instance \* | - g:EnterpriseProjectId   - g:ResourceTag/\<tag-key\>    | -  | -      |
     
  
 
#### URI
GET /v5/iot/{project_id}/iotda-instances/{instance_id}/tasks
表1路径参数 
| 参数          | 是否必选 | 参数类型   | 描述                                                                                                                                      |
|:---|:---|:---|:---|
| project_id  | 是    | String | **参数说明** ：项目ID。获取方法请参见 [获取项目ID](https://support.huaweicloud.com/api-iothub/iot_06_v5_1001.html)。                                        |
| instance_id | 是    | String | **参数说明**：实例ID。 **取值范围**：长度不超过36，由小写字母\[a-f\]、数字、连接符（-）的组成。 |
   
表2Query参数 
| 参数     | 是否必选 | 参数类型    | 描述                                                                                                                                                                                                                                                             |
|:---|:---|:---|:---|
| offset | 否    | Integer | **参数说明**：表示从marker后偏移offset条记录开始查询。默认为0，取值范围为0-500的整数。当offset为0时，表示从marker后第一条记录开始输出。 - 限制offset最大值是出于API性能考虑，您可以搭配marker使用该参数实现翻页，例如每页50条记录，1-11页内都可以直接使用offset跳转到指定页，但到11页后，由于offset限制为500，您需要使用第11页返回的marker作为下次查询的marker，以实现翻页到12-22页。 取值范围：0-500的整数，默认为0。 |
| limit  | 否    | Integer | **参数说明**：分页查询时每页显示的记录数。 **取值范围**：1-500的整数，默认为500。                                                                                                                                 |
| marker | 否    | String  | 上一次分页查询结果中最后一条记录的ID，在上一次分页查询时由物联网平台返回获得。分页查询时物联网平台是按marker也就是记录ID降序查询的，越新的数据记录ID也会越大。若填写marker，则本次只查询记录ID小于marker的数据记录。若不填写，则从记录ID最大也就是最新的一条数据开始查询。如果需要依次查询所有数据，则每次查询时必须填写上一次查询响应中的marker值。                                                                    |
   
#### 请求参数
无
#### 响应参数
**状态码：200**
表3响应Body参数 
| 参数    | 参数类型                                                                                                               | 描述                                      |
|:---|:---|:---|
| page  | [Page] object                           | 查询批量分页结构体，定义了分页页码、每页记录数、记录总数、该页记录的最大Id。 |
| tasks | Array of [InstanceTask] objects | 实例任务列表                                  |
   
 表4Page 
| 参数     | 参数类型   | 描述                               |
|:---|:---|:---|
| count  | Long   | 满足查询条件的记录总数。                     |
| marker | String | 本次分页查询结果中最后一条记录的ID，可在下一次分页查询时使用。 |
   
 表5InstanceTask 
| 参数             | 参数类型                                                                                                       | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
|:---|:---|:---|
| task_id        | String                                                                                                     | **参数说明**：实例任务Id。 **取值范围**：长度不超过64，由小写字母\[a-f\]、数字、连接符（-）的组成。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| type           | String                                                                                                     | **参数说明**：实例任务类型。 **取值范围**： - CREATE：创建实例任务   - MODIFY：实例规格变更任务   - DELETE：实例删除任务   - FREEZE：实例冻结任务   - UNFREEZE：实例解冻任务   - UPDATE_ACCESS_CONFIG：修改实例接入信息任务   - UPDATE_ALLOW_LISTS： 修改实例接入白名单任务   - OPEN_SNAT：启用实例SNAT配置任务    |
| status         | String                                                                                                     | **参数说明**：任务状态。 **取值范围**： - PENDING：等待执行   - RUNNING：执行中   - SUCCESS：执行成功   - FAILED：执行失败                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| status_detail  | String                                                                                                     | **参数说明**：任务状态描述。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| create_time    | String                                                                                                     | **参数说明**：实例任务的创建时间。格式为："yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| start_time     | String                                                                                                     | **参数说明**：实例任务的开始时间。格式为："yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| end_time       | String                                                                                                     | **参数说明**：实例任务的结束时间。格式为："yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| target_config  | [TargetConfig] object   | 实例规格变更时，实例的目标规格配置。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| operate_window | [OperateWindow] object | 实例维护时间窗，用户在变更实例规格时，可以指定在该时间窗内进行变更。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
   
 表6TargetConfig 
| 参数          | 参数类型                                                                                         | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
|:---|:---|:---|
| flavor      | [Flavor] object | **参数说明**：设备接入实例的规格参数。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| charge_mode | String                                                                                       | **参数说明**：实例的付费方式。 **取值范围**： -  prePaid：包年/包月   - postPaid：按需计费    |
   
 表7Flavor 
| 参数   | 参数类型    | 描述                                                                                                                                                       |
|:---|:---|:---|
| type | String  | **参数说明** ：待创建设备接入实例的规格名称。详情请参见[产品规格说明](https://support.huaweicloud.com/productdesc-iothub/iot_04_0014.html)中的规格编码。                                       |
| size | Integer | **参数说明** ：待创建设备接入标准版实例的单元数量，企业版不支持该参数。详情请参见[产品规格说明](https://support.huaweicloud.com/productdesc-iothub/iot_04_0014.html)。当instance_type是standard时，该参数必填。 |
   
 表8OperateWindow 
| 参数         | 参数类型   | 描述                                   |
|:---|:---|:---|
| start_time | String | **参数说明**：变更时间窗开始时间，UTC时间，格式为："HH:mm" |
| end_time   | String | **参数说明**：变更时间窗结束时间，UTC时间，格式为："HH:mm" |
   
#### 请求示例
查询实例任务列表。
```
GET https://{endpoint}/v5/iot/{project_id}/iotda-instances/{instance_id}/tasks
```
#### 响应示例
**状态码：200**
OK
```
{
  "page" : {
    "count" : 1,
    "marker" : 1805431584415871305
  },
  "tasks" : [ {
    "task_id" : "075f413d-a3bb-4243-974d-8eb9221c2df8",
    "type" : "CREATE",
    "status" : "SUCCESS",
    "create_time" : "2020-08-19T01:52:49.493Z",
    "start_time" : "2020-08-19T01:52:49.492Z",
    "end_time" : "2020-08-19T01:52:49.492Z",
    "target_config" : null,
    "operate_window" : null
  }, {
    "task_id" : "075f413d-a3bb-4243-974d-8eb9221c1234",
    "type" : "MODIFY",
    "status" : "PENDING",
    "create_time" : "2021-08-19T01:52:49.493Z",
    "start_time" : "2021-08-19T01:52:49.492Z",
    "end_time" : "2021-08-19T01:52:49.492Z",
    "target_config" : {
      "flavor" : {
        "type" : "iotda.enterprise.1000tps.10wonlinedevice",
        "size" : null
      }
    },
    "operate_window" : {
      "start_time" : "18:00",
      "end_time" : "22:00"
    }
  } ]
}
```
#### SDK代码示例
SDK代码示例如下。
- [Java]
  ```
  package com.huaweicloud.sdk.test;
  import com.huaweicloud.sdk.core.auth.ICredential;
  import com.huaweicloud.sdk.core.auth.BasicCredentials;
  import com.huaweicloud.sdk.core.exception.ConnectionException;
  import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
  import com.huaweicloud.sdk.core.exception.ServiceResponseException;
  import com.huaweicloud.sdk.iotdm.v5.region.IoTDMRegion;
  import com.huaweicloud.sdk.iotdm.v5.*;
  import com.huaweicloud.sdk.iotdm.v5.model.*;
  public class ListInstanceTasksSolution {
      public static void main(String[] args) {
          // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
          // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
          String ak = System.getenv("CLOUD_SDK_AK");
          String sk = System.getenv("CLOUD_SDK_SK");
          String projectId = "{project_id}";
          ICredential auth = new BasicCredentials()
                  .withProjectId(projectId)
                  .withAk(ak)
                  .withSk(sk);
          IoTDMClient client = IoTDMClient.newBuilder()
                  .withCredential(auth)
                  .withRegion(IoTDMRegion.valueOf("<YOUR REGION>"))
                  .build();
          ListInstanceTasksRequest request = new ListInstanceTasksRequest();
          request.withInstanceId("{instance_id}");
          try {
              ListInstanceTasksResponse response = client.listInstanceTasks(request);
              System.out.println(response.toString());
          } catch (ConnectionException e) {
              e.printStackTrace();
          } catch (RequestTimeoutException e) {
              e.printStackTrace();
          } catch (ServiceResponseException e) {
              e.printStackTrace();
              System.out.println(e.getHttpStatusCode());
              System.out.println(e.getRequestId());
              System.out.println(e.getErrorCode());
              System.out.println(e.getErrorMsg());
          }
      }
  }
  ```
- [Python]
  ```
  # coding: utf-8
  import os
  from huaweicloudsdkcore.auth.credentials import BasicCredentials
  from huaweicloudsdkiotdm.v5.region.iotdm_region import IoTDMRegion
  from huaweicloudsdkcore.exceptions import exceptions
  from huaweicloudsdkiotdm.v5 import *
  if __name__ == "__main__":
      # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
      # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
      ak = os.environ["CLOUD_SDK_AK"]
      sk = os.environ["CLOUD_SDK_SK"]
      projectId = "{project_id}"
      credentials = BasicCredentials(ak, sk, projectId)
      client = IoTDMClient.new_builder() \
          .with_credentials(credentials) \
          .with_region(IoTDMRegion.value_of("<YOUR REGION>")) \
          .build()
      try:
          request = ListInstanceTasksRequest()
          request.instance_id = "{instance_id}"
          response = client.list_instance_tasks(request)
          print(response)
      except exceptions.ClientRequestException as e:
          print(e.status_code)
          print(e.request_id)
          print(e.error_code)
          print(e.error_msg)
  ```
- [Go]
  ```
  package main
  import (
  "fmt"
  "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
      iotdm "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotdm/v5"
  "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotdm/v5/model"
      region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotdm/v5/region"
  )
  func main() {
      // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
      // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
      ak := os.Getenv("CLOUD_SDK_AK")
      sk := os.Getenv("CLOUD_SDK_SK")
      projectId := "{project_id}"
      auth, err := basic.NewCredentialsBuilder().
          WithAk(ak).
          WithSk(sk).
          WithProjectId(projectId).
          SafeBuild()
      if err != nil {
          fmt.Println(err)
          return
      }
      hcClient, err := iotdm.IoTDMClientBuilder().
           WithRegion(region.ValueOf("<YOUR REGION>")).
           WithCredential(auth).
           SafeBuild()
      if err != nil {
          fmt.Println(err)
          return
      }
      client := iotdm.NewIoTDMClient(hcClient)
      request := &model.ListInstanceTasksRequest{}
  request.InstanceId = "{instance_id}"
  response, err := client.ListInstanceTasks(request)
  if err == nil {
          fmt.Printf("%+v\n", response)
      } else {
          fmt.Println(err)
      }
  }
  ```
- [更多]
  更多编程语言的SDK代码示例，请参见[API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/IoTDM/sdk?api=ListInstanceTasks)的代码示例页签，可生成自动对应的SDK代码示例。
#### 状态码
| 状态码 | 描述           |
|:---|:---|
| 200 | OK           |
| 400 | Bad Request  |
| 401 | Unauthorized |
| 403 | Forbidden    |
   
#### 错误码
请参见[错误码](https://support.huaweicloud.com/api-iothub/ErrorCode.html)。
