Help Center/ Direct Connect/ API Reference/ API/ Tags/ Querying Resources by Tag
Updated on 2026-11-11 GMT+08:00

Querying Resources by Tag

Function

This API is used to query resources such as virtual gateways and virtual interfaces by tag.

Calling Method

For details, see Calling APIs.

Authorization Information

Each account has all the permissions required to call all APIs, but IAM users must be assigned the required permissions.

  • If you are using role/policy-based authorization, see Permissions Policies and Supported Actions for details on the required permissions.
  • If you are using identity policy-based authorization, no identity policy-based permissions are required for calling this API.

URI

POST /v3/{project_id}/{resource_type}/resource-instances/action

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Definition

Specifies the project ID.

Constraints

N/A

Range

N/A

Default Value

N/A

resource_type

Yes

String

Definition

Specifies the Direct Connect resource type.

Constraints

N/A

Range

  • dc-directconnect: Direct Connect connection

  • dc-vgw: virtual gateway

  • dc-vif: virtual interface

Default Value

N/A

Request Parameters

Table 2 Request body parameters

Parameter

Mandatory

Type

Description

offset

No

String

Definition

Specifies the index position. The query starts from the next image indexed by this parameter. You do not need to specify this parameter when you query resources on the first page. When you query resources on subsequent pages, set this parameter to the value returned in the response body for the previous query. This parameter is not available when action is set to count. If action is set to filter, the value must be a number, and the default value is 0. The value cannot be a negative number.

Constraints

N/A

Range

The value can contain only digits (0 to 9) and must contain at least one character.

Default Value

N/A

limit

No

String

Definition

Specifies the number of query records. This parameter is not available when action is set to count.

Constraints

The value cannot be a negative number.

Range

The value can contain only digits (0 to 9) and at least one character. The value ranges from 1 to 1000.

Default Value

If action is set to filter, the default value is 1000.

action

Yes

String

Definition

Specifies the operation to perform. The value can be filter or count.

filter indicates pagination query. count indicates that the total number of query results meeting the search criteria will be returned. Returning other fields is not allowed.

Constraints

N/A

Range

  • filter

  • count

Default Value

N/A

matches

No

Array of Match objects

Definition

Specifies the search field. The tag key is the field to be matched, for example, resource_name. The tag value indicates the value to be matched. This field is a fixed dictionary value.

You can determine whether fuzzy match is required based on different fields. For example, if key is set to resource_name, fuzzy search (case insensitive) is used by default. If value is an empty string, exact match is used. For search by resource_id, exact match is used. Only resource_name is supported currently. More fields will be supported later.

Constraints

N/A

not_tags

No

Array of Tags objects

Definition

Resources that do not contain all tags specified by this parameter will be returned. Each tag can contain a maximum of 10 keys, and each key can contain a maximum of 10 values. The structure body cannot be missing, and the key cannot be left blank or set to an empty string. Tag keys must be unique, and tag values of the same key must be unique. Keys are in AND relationship, and values in one tag are in OR relationship. If no filter criteria are specified, all data is returned.

Constraints

N/A

tags

No

Array of Tags objects

Definition

Resources containing all tags specified by this parameter are returned. Each tag can contain a maximum of 10 keys, and each key can contain a maximum of 10 values. The structure body cannot be missing, and the key cannot be left blank or set to an empty string. Tag keys must be unique, and tag values of the same key must be unique. Keys are in AND relationship, and values in one tag are in OR relationship. If no tag filtering criteria is specified, all data is returned.

Constraints

N/A

tags_any

No

Array of Tags objects

Definition

Resources containing any tag specified by this parameter are returned. Each tag can contain a maximum of 10 keys, and each key can contain a maximum of 10 values. The structure body cannot be missing, and the key cannot be left blank or set to an empty string. Tag keys must be unique, and tag values of the same key must be unique. Keys are in OR relationship, and values in one tag are in OR relationship. If no filter criteria are specified, all data is returned.

Constraints

N/A

not_tags_any

No

Array of Tags objects

Definition

Resources that do not contain any tag specified by this parameter will be returned. Each tag can contain a maximum of 10 keys, and each key can contain a maximum of 10 values. The structure body cannot be missing, and the key cannot be left blank or set to an empty string. Tag keys must be unique, and tag values of the same key must be unique. Keys are in OR relationship, and values in one tag are in OR relationship. If no filter criteria are specified, all data is returned.

Constraints

N/A

sys_tags

No

Array of Tags objects

Definition

Only users with the permission op_service can use this field to filter resources. Only one tag structure is contained when this API is called by Tag Management Service (TMS). The key is _sys_enterprise_project_id, and the value is the enterprise project ID list. Currently, each key can contain only one value. 0 indicates the default enterprise project. sys_tags and tenant tag filtering conditions (without_any_tag, tags, tags_any, not_tags, and not_tags_any) cannot be used at the same time.

Constraints

N/A

Table 3 Match

Parameter

Mandatory

Type

Description

key

Yes

String

Definition

Specifies the key. The value can be dc-directconnect, dc-vgw, or dc-vif.

Constraints

N/A

Range

N/A

Default Value

N/A

value

Yes

String

Definition

Specifies the value.

Constraints

N/A

Range

The value can contain a maximum of 255 Unicode characters.

Default Value

N/A

Table 4 Tags

Parameter

Mandatory

Type

Description

key

Yes

String

Definition

Specifies the key. It cannot be left blank.

Constraints

N/A

Range

A tag key can contain a maximum of 128 Unicode characters.

Default Value

N/A

values

Yes

Array of strings

Definition

Specifies the value list.

Constraints

Each value contains a maximum of 255 Unicode characters.

Response Parameters

Status code: 200

Table 5 Response body parameters

Parameter

Type

Description

resources

Array of Resource objects

Definition

Specifies the resource list.

total_count

Integer

Definition

Specifies the total number of records.

Range

N/A

request_id

String

Definition

Specifies the request ID.

Range

N/A

Table 6 Resource

Parameter

Type

Description

resource_detail

Object

Definition

Specifies the resource details.

The value is a resource object, used for extension. This parameter is left empty by default.

resource_id

String

Definition

Specifies the resource ID.

Range

N/A

resource_name

String

Definition

Specifies the resource name. This parameter is left blank by default if there is no resource name.

Range

N/A

tags

Array of Tag objects

Definition

Specifies the tag list. If there is no tag, the value is an empty array by default.

sys_tags

Array of Tag objects

Definition

Specifies the tag list. If there is no tag, the value is an empty array by default.

Table 7 Tag

Parameter

Type

Description

key

String

Definition

Specifies the tag key. Each key cannot start or end with a space, or start with _sys_. Only letters, digits, spaces, and the following special characters are allowed: _.:=+-@

Range

1 to 128 characters

value

String

Definition

Specifies the tag value. The value can contain letters, digits, spaces, and the following special characters: _.:/=+-@

Range

The value can contain a maximum of 255 characters.

Example Requests

  • Querying resources (action set to filter)

    POST https://{endpoint}/v3/cfa563efb77d4b6d9960781d82530fd8/dc-vgw/resource-instances/action
    
    {
      "offset" : "10",
      "limit" : "10",
      "action" : "filter",
      "matches" : [ {
        "key" : "resource_name",
        "value" : "resource1"
      } ],
      "not_tags" : [ {
        "key" : "key1",
        "values" : [ "*value1", "value2" ]
      } ],
      "tags" : [ {
        "key" : "key1",
        "values" : [ "*value1", "value2" ]
      } ],
      "tags_any" : [ {
        "key" : "key1",
        "values" : [ "value1", "value2" ]
      } ],
      "not_tags_any" : [ {
        "key" : "key1",
        "values" : [ "value1", "value2" ]
      } ]
    }
  • Querying the total number of resources (action set to count)

    POST https://{endpoint}/v3/cfa563efb77d4b6d9960781d82530fd8/dc-vgw/resource-instances/action
    
    {
      "action" : "count",
      "not_tags" : [ {
        "key" : "key1",
        "values" : [ "value1", "*value2" ]
      } ],
      "tags" : [ {
        "key" : "key1",
        "values" : [ "value1", "value2" ]
      }, {
        "key" : "key2",
        "values" : [ "value1", "value2" ]
      } ],
      "tags_any" : [ {
        "key" : "key1",
        "values" : [ "value1", "value2" ]
      } ],
      "not_tags_any" : [ {
        "key" : "key1",
        "values" : [ "value1", "value2" ]
      } ],
      "matches" : [ {
        "key" : "resource_name",
        "value" : "resource1"
      } ]
    }

Example Responses

Status code: 200

{
  "resources" : [ ],
  "total_count" : 0,
  "request_id" : "9a4f4dfc4fb2fc101e65bba07d908535"
}

SDK Sample Code

The SDK sample code is as follows.

  • Querying resources (action set to filter)

      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
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    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.dc.v3.region.DcRegion;
    import com.huaweicloud.sdk.dc.v3.*;
    import com.huaweicloud.sdk.dc.v3.model.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class ListTagResourceInstancesSolution {
    
        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);
    
            DcClient client = DcClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(DcRegion.valueOf("<YOUR REGION>"))
                    .build();
            ListTagResourceInstancesRequest request = new ListTagResourceInstancesRequest();
            request.withResourceType(ListTagResourceInstancesRequest.ResourceTypeEnum.fromValue("{resource_type}"));
            ListTagResourceInstancesRequestBody body = new ListTagResourceInstancesRequestBody();
            List<String> listNotTagsAnyValues = new ArrayList<>();
            listNotTagsAnyValues.add("value1");
            listNotTagsAnyValues.add("value2");
            List<Tags> listbodyNotTagsAny = new ArrayList<>();
            listbodyNotTagsAny.add(
                new Tags()
                    .withKey("key1")
                    .withValues(listNotTagsAnyValues)
            );
            List<String> listTagsAnyValues = new ArrayList<>();
            listTagsAnyValues.add("value1");
            listTagsAnyValues.add("value2");
            List<Tags> listbodyTagsAny = new ArrayList<>();
            listbodyTagsAny.add(
                new Tags()
                    .withKey("key1")
                    .withValues(listTagsAnyValues)
            );
            List<String> listTagsValues = new ArrayList<>();
            listTagsValues.add("*value1");
            listTagsValues.add("value2");
            List<Tags> listbodyTags = new ArrayList<>();
            listbodyTags.add(
                new Tags()
                    .withKey("key1")
                    .withValues(listTagsValues)
            );
            List<String> listNotTagsValues = new ArrayList<>();
            listNotTagsValues.add("*value1");
            listNotTagsValues.add("value2");
            List<Tags> listbodyNotTags = new ArrayList<>();
            listbodyNotTags.add(
                new Tags()
                    .withKey("key1")
                    .withValues(listNotTagsValues)
            );
            List<Match> listbodyMatches = new ArrayList<>();
            listbodyMatches.add(
                new Match()
                    .withKey("resource_name")
                    .withValue("resource1")
            );
            body.withNotTagsAny(listbodyNotTagsAny);
            body.withTagsAny(listbodyTagsAny);
            body.withTags(listbodyTags);
            body.withNotTags(listbodyNotTags);
            body.withMatches(listbodyMatches);
            body.withAction(ListTagResourceInstancesRequestBody.ListTagResourceInstancesRequestBody.fromValue("filter"));
            body.withLimit("10");
            body.withOffset("10");
            request.withBody(body);
            try {
                ListTagResourceInstancesResponse response = client.listTagResourceInstances(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());
            }
        }
    }
    
  • Querying the total number of resources (action set to count)

      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
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    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.dc.v3.region.DcRegion;
    import com.huaweicloud.sdk.dc.v3.*;
    import com.huaweicloud.sdk.dc.v3.model.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class ListTagResourceInstancesSolution {
    
        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);
    
            DcClient client = DcClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(DcRegion.valueOf("<YOUR REGION>"))
                    .build();
            ListTagResourceInstancesRequest request = new ListTagResourceInstancesRequest();
            request.withResourceType(ListTagResourceInstancesRequest.ResourceTypeEnum.fromValue("{resource_type}"));
            ListTagResourceInstancesRequestBody body = new ListTagResourceInstancesRequestBody();
            List<String> listNotTagsAnyValues = new ArrayList<>();
            listNotTagsAnyValues.add("value1");
            listNotTagsAnyValues.add("value2");
            List<Tags> listbodyNotTagsAny = new ArrayList<>();
            listbodyNotTagsAny.add(
                new Tags()
                    .withKey("key1")
                    .withValues(listNotTagsAnyValues)
            );
            List<String> listTagsAnyValues = new ArrayList<>();
            listTagsAnyValues.add("value1");
            listTagsAnyValues.add("value2");
            List<Tags> listbodyTagsAny = new ArrayList<>();
            listbodyTagsAny.add(
                new Tags()
                    .withKey("key1")
                    .withValues(listTagsAnyValues)
            );
            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)
            );
            List<String> listNotTagsValues = new ArrayList<>();
            listNotTagsValues.add("value1");
            listNotTagsValues.add("*value2");
            List<Tags> listbodyNotTags = new ArrayList<>();
            listbodyNotTags.add(
                new Tags()
                    .withKey("key1")
                    .withValues(listNotTagsValues)
            );
            List<Match> listbodyMatches = new ArrayList<>();
            listbodyMatches.add(
                new Match()
                    .withKey("resource_name")
                    .withValue("resource1")
            );
            body.withNotTagsAny(listbodyNotTagsAny);
            body.withTagsAny(listbodyTagsAny);
            body.withTags(listbodyTags);
            body.withNotTags(listbodyNotTags);
            body.withMatches(listbodyMatches);
            body.withAction(ListTagResourceInstancesRequestBody.ListTagResourceInstancesRequestBody.fromValue("count"));
            request.withBody(body);
            try {
                ListTagResourceInstancesResponse response = client.listTagResourceInstances(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());
            }
        }
    }
    
  • Querying resources (action set to filter)

     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
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkdc.v3.region.dc_region import DcRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkdc.v3 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 = DcClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(DcRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = ListTagResourceInstancesRequest()
            request.resource_type = "{resource_type}"
            listValuesNotTagsAny = [
                "value1",
                "value2"
            ]
            listNotTagsAnybody = [
                Tags(
                    key="key1",
                    values=listValuesNotTagsAny
                )
            ]
            listValuesTagsAny = [
                "value1",
                "value2"
            ]
            listTagsAnybody = [
                Tags(
                    key="key1",
                    values=listValuesTagsAny
                )
            ]
            listValuesTags = [
                "*value1",
                "value2"
            ]
            listTagsbody = [
                Tags(
                    key="key1",
                    values=listValuesTags
                )
            ]
            listValuesNotTags = [
                "*value1",
                "value2"
            ]
            listNotTagsbody = [
                Tags(
                    key="key1",
                    values=listValuesNotTags
                )
            ]
            listMatchesbody = [
                Match(
                    key="resource_name",
                    value="resource1"
                )
            ]
            request.body = ListTagResourceInstancesRequestBody(
                not_tags_any=listNotTagsAnybody,
                tags_any=listTagsAnybody,
                tags=listTagsbody,
                not_tags=listNotTagsbody,
                matches=listMatchesbody,
                action="filter",
                limit="10",
                offset="10"
            )
            response = client.list_tag_resource_instances(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • Querying the total number of resources (action set to count)

     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
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkdc.v3.region.dc_region import DcRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkdc.v3 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 = DcClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(DcRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = ListTagResourceInstancesRequest()
            request.resource_type = "{resource_type}"
            listValuesNotTagsAny = [
                "value1",
                "value2"
            ]
            listNotTagsAnybody = [
                Tags(
                    key="key1",
                    values=listValuesNotTagsAny
                )
            ]
            listValuesTagsAny = [
                "value1",
                "value2"
            ]
            listTagsAnybody = [
                Tags(
                    key="key1",
                    values=listValuesTagsAny
                )
            ]
            listValuesTags = [
                "value1",
                "value2"
            ]
            listValuesTags1 = [
                "value1",
                "value2"
            ]
            listTagsbody = [
                Tags(
                    key="key1",
                    values=listValuesTags1
                ),
                Tags(
                    key="key2",
                    values=listValuesTags
                )
            ]
            listValuesNotTags = [
                "value1",
                "*value2"
            ]
            listNotTagsbody = [
                Tags(
                    key="key1",
                    values=listValuesNotTags
                )
            ]
            listMatchesbody = [
                Match(
                    key="resource_name",
                    value="resource1"
                )
            ]
            request.body = ListTagResourceInstancesRequestBody(
                not_tags_any=listNotTagsAnybody,
                tags_any=listTagsAnybody,
                tags=listTagsbody,
                not_tags=listNotTagsbody,
                matches=listMatchesbody,
                action="count"
            )
            response = client.list_tag_resource_instances(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • Querying resources (action set to filter)

      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
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        dc "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dc/v3"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dc/v3/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dc/v3/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 := dc.DcClientBuilder().
             WithRegion(region.ValueOf("<YOUR REGION>")).
             WithCredential(auth).
             SafeBuild()
    
    
        if err != nil {
            fmt.Println(err)
            return
        }
    
        client := dc.NewDcClient(hcClient)
    
        request := &model.ListTagResourceInstancesRequest{}
    	request.ResourceType = model.GetListTagResourceInstancesRequestResourceTypeEnum().RESOURCE_TYPE
    	var listValuesNotTagsAny = []string{
            "value1",
    	    "value2",
        }
    	var listNotTagsAnybody = []model.Tags{
            {
                Key: "key1",
                Values: listValuesNotTagsAny,
            },
        }
    	var listValuesTagsAny = []string{
            "value1",
    	    "value2",
        }
    	var listTagsAnybody = []model.Tags{
            {
                Key: "key1",
                Values: listValuesTagsAny,
            },
        }
    	var listValuesTags = []string{
            "*value1",
    	    "value2",
        }
    	var listTagsbody = []model.Tags{
            {
                Key: "key1",
                Values: listValuesTags,
            },
        }
    	var listValuesNotTags = []string{
            "*value1",
    	    "value2",
        }
    	var listNotTagsbody = []model.Tags{
            {
                Key: "key1",
                Values: listValuesNotTags,
            },
        }
    	var listMatchesbody = []model.Match{
            {
                Key: "resource_name",
                Value: "resource1",
            },
        }
    	limitListTagResourceInstancesRequestBody:= "10"
    	offsetListTagResourceInstancesRequestBody:= "10"
    	request.Body = &model.ListTagResourceInstancesRequestBody{
    		NotTagsAny: &listNotTagsAnybody,
    		TagsAny: &listTagsAnybody,
    		Tags: &listTagsbody,
    		NotTags: &listNotTagsbody,
    		Matches: &listMatchesbody,
    		Action: model.GetListTagResourceInstancesRequestBodyActionEnum().FILTER,
    		Limit: &limitListTagResourceInstancesRequestBody,
    		Offset: &offsetListTagResourceInstancesRequestBody,
    	}
    	response, err := client.ListTagResourceInstances(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • Querying the total number of resources (action set to count)

      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
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        dc "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dc/v3"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dc/v3/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dc/v3/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 := dc.DcClientBuilder().
             WithRegion(region.ValueOf("<YOUR REGION>")).
             WithCredential(auth).
             SafeBuild()
    
    
        if err != nil {
            fmt.Println(err)
            return
        }
    
        client := dc.NewDcClient(hcClient)
    
        request := &model.ListTagResourceInstancesRequest{}
    	request.ResourceType = model.GetListTagResourceInstancesRequestResourceTypeEnum().RESOURCE_TYPE
    	var listValuesNotTagsAny = []string{
            "value1",
    	    "value2",
        }
    	var listNotTagsAnybody = []model.Tags{
            {
                Key: "key1",
                Values: listValuesNotTagsAny,
            },
        }
    	var listValuesTagsAny = []string{
            "value1",
    	    "value2",
        }
    	var listTagsAnybody = []model.Tags{
            {
                Key: "key1",
                Values: listValuesTagsAny,
            },
        }
    	var listValuesTags = []string{
            "value1",
    	    "value2",
        }
    	var listValuesTags1 = []string{
            "value1",
    	    "value2",
        }
    	var listTagsbody = []model.Tags{
            {
                Key: "key1",
                Values: listValuesTags1,
            },
            {
                Key: "key2",
                Values: listValuesTags,
            },
        }
    	var listValuesNotTags = []string{
            "value1",
    	    "*value2",
        }
    	var listNotTagsbody = []model.Tags{
            {
                Key: "key1",
                Values: listValuesNotTags,
            },
        }
    	var listMatchesbody = []model.Match{
            {
                Key: "resource_name",
                Value: "resource1",
            },
        }
    	request.Body = &model.ListTagResourceInstancesRequestBody{
    		NotTagsAny: &listNotTagsAnybody,
    		TagsAny: &listTagsAnybody,
    		Tags: &listTagsbody,
    		NotTags: &listNotTagsbody,
    		Matches: &listMatchesbody,
    		Action: model.GetListTagResourceInstancesRequestBodyActionEnum().COUNT,
    	}
    	response, err := client.ListTagResourceInstances(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    

For SDK sample code of more programming languages, see the Sample Code tab in API Explorer. SDK sample code can be automatically generated.

Status Codes

Status Code

Description

200

OK

Error Codes

See Error Codes.