文档首页/ 数据接入服务 DIS/ API参考/ API说明/ 标签管理/ 使用标签过滤资源(通道等)
更新时间:2024-10-21 GMT+08:00
分享

使用标签过滤资源(通道等)

功能介绍

该接口用于使用标签过滤资源(通道等)。

调用方法

请参见如何调用API

URI

POST /v2/{project_id}/stream/resource_instances/action

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

项目ID。

请求参数

表2 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

用户Token。

通过调用IAM服务获取用户Token接口获取(响应消息头中X-Subject-Token的值)。

表3 请求Body参数

参数

是否必选

参数类型

描述

action

String

操作标识(仅限于filter,count)

  • filter:分页查询

  • count:查询总条数,只需按照条件将总条数返回即可

枚举值:

  • filter

  • count

limit

String

查询记录数(action为count时无此参数)如果action为filter默认为1000,limit最多为1000,不能为负数,最小值为1

缺省值:1000

offset

String

索引位置, 从offset指定的下一条数据开始查询。 查询第一页数据时,不需要传入此参数,查询后续页码数据时,将查询前一页数据时响应体中的值带入此参数(action为count时无此参数)如果action为filter默认为0,必须为数字,不能为负数

tags

Array of Tags objects

返回结果包含该参数中所有标签对应的资源,该参数最多包含10个key,每个key下面的value最多10个,结构体不能缺失,key不能为空或者空字符串。

tags_any

Array of Tags objects

返回结果包含该参数中任意一个标签对应的资源,该参数最多包含10个key,每个key下面的value最多10个,结构体不能缺失,key不能为空或者空字符串。Key不能重复,同一个key中values不能重复。

not_tags

Array of Tags objects

返回结果不包含该参数中所有标签对应的资源,该参数最多包含10个key,每个key下面的value最多10个, 结构体不能缺失,key不能为空或者空字符串。Key不能重复,同一个key中values不能重复。

not_tags_any

Array of Tags objects

返回结果不包含该参数中任意一个标签对应的资源,该参数最多包含10个key,每个key下面的value最多10个,结构体不能缺失,key不能为空或者空字符串。Key不能重复,同一个key中values不能重复。

matches

String

搜索字段,key为要匹配的字段,当前仅支持resource_name。value为匹配的值。此字段为固定字典值

表4 Tags

参数

是否必选

参数类型

描述

key

String

键。

  • 不能为空。

  • 对于同一资源键值唯一。

  • 字符集:A-Z,a-z , 0-9,‘-’,‘_’,UNICODE字符(\u4E00-\u9FFF)。

最大长度:36

values

Array of strings

标签值列表。

如果values为空列表,则表示any_value。value之间为或的关系。

响应参数

状态码: 200

表5 响应Body参数

参数

参数类型

描述

action

String

操作标识(仅限于filter,count)

  • filter:分页查询

  • count:查询总条数,只需按照条件将总条数返回即可

枚举值:

  • filter

  • count

limit

String

查询记录数(action为count时无此参数)如果action为filter默认为1000,limit最多为1000,不能为负数,最小值为1

缺省值:1000

offset

String

索引位置, 从offset指定的下一条数据开始查询。 查询第一页数据时,不需要传入此参数,查询后续页码数据时,将查询前一页数据时响应体中的值带入此参数(action为count时无此参数)如果action为filter默认为0,必须为数字,不能为负数

tags

Array of Tags objects

返回结果包含该参数中所有标签对应的资源,该参数最多包含10个key,每个key下面的value最多10个,结构体不能缺失,key不能为空或者空字符串。

tags_any

Array of Tags objects

返回结果包含该参数中任意一个标签对应的资源,该参数最多包含10个key,每个key下面的value最多10个,结构体不能缺失,key不能为空或者空字符串。Key不能重复,同一个key中values不能重复。

not_tags

Array of Tags objects

返回结果不包含该参数中所有标签对应的资源,该参数最多包含10个key,每个key下面的value最多10个, 结构体不能缺失,key不能为空或者空字符串。Key不能重复,同一个key中values不能重复。

not_tags_any

Array of Tags objects

返回结果不包含该参数中任意一个标签对应的资源,该参数最多包含10个key,每个key下面的value最多10个,结构体不能缺失,key不能为空或者空字符串。Key不能重复,同一个key中values不能重复。

matches

String

搜索字段,key为要匹配的字段,当前仅支持resource_name。value为匹配的值。此字段为固定字典值

表6 Tags

参数

参数类型

描述

key

String

键。

  • 不能为空。

  • 对于同一资源键值唯一。

  • 字符集:A-Z,a-z , 0-9,‘-’,‘_’,UNICODE字符(\u4E00-\u9FFF)。

最大长度:36

values

Array of strings

标签值列表。

如果values为空列表,则表示any_value。value之间为或的关系。

请求示例

  • 使用标签过滤资源(通道等),查询记录总数。

    POST https://{Endpoint}/v2/{project_id}/stream/resource_instances/action
    
    {
      "action" : "count",
      "tags" : [ {
        "key" : "key1",
        "values" : [ "value1", "value2" ]
      }, {
        "key" : "key2",
        "values" : [ "value1", "value2" ]
      } ],
      "matches" : [ {
        "key" : "resource_name",
        "value" : "resource1"
      } ]
    }
  • 使用标签过滤资源(通道等),分页查询。

    POST https://{Endpoint}/v2/{project_id}/stream/resource_instances/action
    
    {
      "offset" : "0",
      "limit" : "100",
      "action" : "filter",
      "matches" : [ {
        "key" : "resource_name",
        "value" : "resource1"
      } ],
      "tags" : [ {
        "key" : "key1",
        "values" : [ "*value1", "value2" ]
      } ]
    }

响应示例

状态码: 200

使用标签过滤资源(通道等)请求体。

{
  "resources" : [ {
    "resource_detail" : null,
    "resource_id" : "cdfs_cefs_wesas_12_dsad",
    "resource_name" : "resouece1",
    "tags" : [ {
      "key" : "key1",
      "value" : "value1"
    }, {
      "key" : "key2",
      "value" : "value1"
    } ]
  } ],
  "total_count" : 1000
}

SDK代码示例

SDK代码示例如下。

  • 使用标签过滤资源(通道等),查询记录总数。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    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.dis.v2.region.DisRegion;
    import com.huaweicloud.sdk.dis.v2.*;
    import com.huaweicloud.sdk.dis.v2.model.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class ListResourcesByTagsSolution {
    
        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");
    
            ICredential auth = new BasicCredentials()
                    .withAk(ak)
                    .withSk(sk);
    
            DisClient client = DisClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(DisRegion.valueOf("<YOUR REGION>"))
                    .build();
            ListResourcesByTagsRequest request = new ListResourcesByTagsRequest();
            ListResourceInstancesReq body = new ListResourceInstancesReq();
            List<String> listTagsValues = new ArrayList<>();
            listTagsValues.add("value1");
            listTagsValues.add("value2");
            List<String> listTagsValues1 = new ArrayList<>();
            listTagsValues1.add("value1");
            listTagsValues1.add("value2");
            List<Tags> listbodyTags = new ArrayList<>();
            listbodyTags.add(
                new Tags()
                    .withKey("key1")
                    .withValues(listTagsValues1)
            );
            listbodyTags.add(
                new Tags()
                    .withKey("key2")
                    .withValues(listTagsValues)
            );
            body.withMatches("[{key=resource_name, value=resource1}]");
            body.withTags(listbodyTags);
            body.withAction(ListResourceInstancesReq.ActionEnum.fromValue("count"));
            request.withBody(body);
            try {
                ListResourcesByTagsResponse response = client.listResourcesByTags(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());
            }
        }
    }
    
  • 使用标签过滤资源(通道等),分页查询。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    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.dis.v2.region.DisRegion;
    import com.huaweicloud.sdk.dis.v2.*;
    import com.huaweicloud.sdk.dis.v2.model.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class ListResourcesByTagsSolution {
    
        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");
    
            ICredential auth = new BasicCredentials()
                    .withAk(ak)
                    .withSk(sk);
    
            DisClient client = DisClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(DisRegion.valueOf("<YOUR REGION>"))
                    .build();
            ListResourcesByTagsRequest request = new ListResourcesByTagsRequest();
            ListResourceInstancesReq body = new ListResourceInstancesReq();
            List<String> listTagsValues = new ArrayList<>();
            listTagsValues.add("*value1");
            listTagsValues.add("value2");
            List<Tags> listbodyTags = new ArrayList<>();
            listbodyTags.add(
                new Tags()
                    .withKey("key1")
                    .withValues(listTagsValues)
            );
            body.withMatches("[{key=resource_name, value=resource1}]");
            body.withTags(listbodyTags);
            body.withOffset("0");
            body.withLimit("100");
            body.withAction(ListResourceInstancesReq.ActionEnum.fromValue("filter"));
            request.withBody(body);
            try {
                ListResourcesByTagsResponse response = client.listResourcesByTags(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());
            }
        }
    }
    
  • 使用标签过滤资源(通道等),查询记录总数。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkdis.v2.region.dis_region import DisRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkdis.v2 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"]
    
        credentials = BasicCredentials(ak, sk)
    
        client = DisClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(DisRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = ListResourcesByTagsRequest()
            listValuesTags = [
                "value1",
                "value2"
            ]
            listValuesTags1 = [
                "value1",
                "value2"
            ]
            listTagsbody = [
                Tags(
                    key="key1",
                    values=listValuesTags1
                ),
                Tags(
                    key="key2",
                    values=listValuesTags
                )
            ]
            request.body = ListResourceInstancesReq(
                matches="[{key=resource_name, value=resource1}]",
                tags=listTagsbody,
                action="count"
            )
            response = client.list_resources_by_tags(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 使用标签过滤资源(通道等),分页查询。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkdis.v2.region.dis_region import DisRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkdis.v2 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"]
    
        credentials = BasicCredentials(ak, sk)
    
        client = DisClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(DisRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = ListResourcesByTagsRequest()
            listValuesTags = [
                "*value1",
                "value2"
            ]
            listTagsbody = [
                Tags(
                    key="key1",
                    values=listValuesTags
                )
            ]
            request.body = ListResourceInstancesReq(
                matches="[{key=resource_name, value=resource1}]",
                tags=listTagsbody,
                offset="0",
                limit="100",
                action="filter"
            )
            response = client.list_resources_by_tags(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 使用标签过滤资源(通道等),查询记录总数。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        dis "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dis/v2"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dis/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dis/v2/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")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := dis.NewDisClient(
            dis.DisClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.ListResourcesByTagsRequest{}
    	var listValuesTags = []string{
            "value1",
    	    "value2",
        }
    	var listValuesTags1 = []string{
            "value1",
    	    "value2",
        }
    	keyTags:= "key1"
    	keyTags1:= "key2"
    	var listTagsbody = []model.Tags{
            {
                Key: &keyTags,
                Values: &listValuesTags1,
            },
            {
                Key: &keyTags1,
                Values: &listValuesTags,
            },
        }
    	matchesListResourceInstancesReq:= "[{key=resource_name, value=resource1}]"
    	request.Body = &model.ListResourceInstancesReq{
    		Matches: &matchesListResourceInstancesReq,
    		Tags: &listTagsbody,
    		Action: model.GetListResourceInstancesReqActionEnum().COUNT,
    	}
    	response, err := client.ListResourcesByTags(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • 使用标签过滤资源(通道等),分页查询。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        dis "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dis/v2"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dis/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dis/v2/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")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := dis.NewDisClient(
            dis.DisClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.ListResourcesByTagsRequest{}
    	var listValuesTags = []string{
            "*value1",
    	    "value2",
        }
    	keyTags:= "key1"
    	var listTagsbody = []model.Tags{
            {
                Key: &keyTags,
                Values: &listValuesTags,
            },
        }
    	matchesListResourceInstancesReq:= "[{key=resource_name, value=resource1}]"
    	offsetListResourceInstancesReq:= "0"
    	limitListResourceInstancesReq:= "100"
    	request.Body = &model.ListResourceInstancesReq{
    		Matches: &matchesListResourceInstancesReq,
    		Tags: &listTagsbody,
    		Offset: &offsetListResourceInstancesReq,
    		Limit: &limitListResourceInstancesReq,
    		Action: model.GetListResourceInstancesReqActionEnum().FILTER,
    	}
    	response, err := client.ListResourcesByTags(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

使用标签过滤资源(通道等)请求体。

错误码

请参见错误码

相关文档