Querying Resources Using Tags
Function
This API is used to query resources using tags.
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, the following identity policy-based permissions are required.
Action
Access Level
Resource Type (*: required)
Condition Key
Alias
Dependencies
dns:tag:get
Read
zone
-
-
-
ptr
-
URI
POST /v2/{project_id}/{resource_type}/resource_instances/action
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| project_id | Yes | String | Definition Project ID. You can obtain the ID from the management console or by calling the IAM API. Constraints N/A Range N/A Default Value N/A |
| resource_type | Yes | String | Definition Resource type. Constraints N/A Range Default Value N/A |
Request Parameters
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| X-Auth-Token | Yes | String | Definition User token, which is used for identity authentication. It must be carried in the request header to verify the user identity during API calls. You can obtain it by calling the IAM API for obtaining a user token. The value of X-Subject-Token in the response header is the user token. For details about how to obtain a user token, see Obtaining a User Token. Constraints N/A Range N/A Default Value N/A |
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| tags | No | Array of tagValuesReq objects | Definition The instances to be queried contain all tags listed in this field. The value is an array. Each element contains a tag key and tag value, which are used to manage and filter resource tags. Constraints Each tag contains a maximum of 10 keys, and each key contains 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. |
| tags_any | No | Array of tagValuesReq objects | Definition The instances to be queried contain any tag listed in this field. This field is an array. Each element contains a tag key and a tag value array. They are used to query resources that contain any of the specified tags. Constraints Each tag contains a maximum of 10 keys, and each key contains 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. |
| not_tags | No | Array of tagValuesReq objects | Definition The instances to be queried do not contain all tags listed in this field. This field is an array. Each element contains a tag key and a tag value array. They are used to query resources that do not contain all specified tags. Constraints Each tag contains a maximum of 10 keys, and each key contains 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. |
| not_tags_any | No | Array of tagValuesReq objects | Definition The instances to be queried do not contain any tag listed in this field. This field is an array. Each element contains a tag key and a tag value array. They are used to query resources that do not contain any of the specified tags. Constraints Each tag contains a maximum of 10 keys, and each key contains 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. |
| limit | No | Integer | Definition The number of records returned on each page during pagination query. Constraints If action is set to filter, the default value is 1000. If action is set to count, this parameter is not available. Range 1–1000 Default Value N/A |
| offset | No | Integer | Definition The offset of pagination query. It specifies the number of rows or records to skip from the beginning of the result set before retrieving the desired data. Constraints
Range 0 to 2147483647 Default Value 0 |
| action | Yes | String | Definition Operation to be performed (case sensitive). Constraints N/A Range Default Value N/A |
| matches | No | Array of match objects | Definition Search field. This field specifies the key-value pair to be matched in the query. This field is an array. Each element contains the field name and value to be matched. Constraints If value is left blank, exact matching will work. Otherwise, fuzzy matching will work. |
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| key | No | String | Definition Tag key. Constraints The key cannot be left blank. (This field is not verified during the search.) Range A tag key can contain a maximum of 128 Unicode characters. Default Value N/A |
| values | No | Array of strings | Definition Tag values. This field is a string array. Each element is a tag value, which is used together with the corresponding tag key to filter resources. Each value contains a maximum of 255 Unicode characters. Constraints |
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| key | Yes | String | Definition Field name of the search criteria, which is used to specify the field based on which search and matching are performed. Constraints N/A Range Default Value N/A |
| value | No | String | Definition Field value of the search criteria, which is used to match the corresponding field content of the resource. Constraints N/A Range Each value contains a maximum of 255 Unicode characters. Default Value N/A |
Response Parameters
Status code: 200
| Parameter | Type | Description |
|---|---|---|
| resources | Array of resourceItem objects | Definition Resource tag list. This field is an array. Each element contains the resource ID, resource details, and tag. |
| total_count | Integer | Definition Total number of resources. Range N/A |
| Parameter | Type | Description |
|---|---|---|
| resource_id | String | Definition Resource ID. Range N/A |
| resource_detail | Object | Definition Resource details. This field is reserved for subsequent extension, and its value defaults to an empty string. |
| tags | Array of tag objects | Definition Tag list. If there is no tag, an empty array is used by default. This field is an array. Each element contains a tag key and a tag value. |
| resource_name | String | Definition Resource name. The value is an empty string by default. Range N/A |
Example Requests
Querying resources by tag, with the start offset of pagination query set to 100, and the number of resources displayed on each page to 100
POST https://{Endpoint}/v2/{project_id}/{resource_type}/resource_instances/action
{
"offset" : 100,
"limit" : 100,
"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" ]
} ]
} Example Responses
Status code: 200
{
"resources" : [ {
"resource_detail" : null,
"resource_id" : "cdfs_cefs_wesas_12_dsad",
"resource_name" : "resource1",
"tags" : [ {
"key" : "key1",
"value" : "value1"
}, {
"key" : "key2",
"value" : "value1"
} ]
} ],
"total_count" : 1000
} SDK Sample Code
The SDK sample code is as follows.
Querying resources by tag, with the start offset of pagination query set to 100, and the number of resources displayed on each page to 100
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.dns.v2.region.DnsRegion; import com.huaweicloud.sdk.dns.v2.*; import com.huaweicloud.sdk.dns.v2.model.*; import java.util.List; import java.util.ArrayList; public class ListTagSolution { 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); DnsClient client = DnsClient.newBuilder() .withCredential(auth) .withRegion(DnsRegion.valueOf("<YOUR REGION>")) .build(); ListTagRequest request = new ListTagRequest(); request.withResourceType("{resource_type}"); ListTagReq body = new ListTagReq(); List<Match> listbodyMatches = new ArrayList<>(); listbodyMatches.add( new Match() .withKey("resource_name") .withValue("resource1") ); List<String> listNotTagsAnyValues = new ArrayList<>(); listNotTagsAnyValues.add("value1"); listNotTagsAnyValues.add("value2"); List<TagValues> listbodyNotTagsAny = new ArrayList<>(); listbodyNotTagsAny.add( new TagValues() .withKey("key1") .withValues(listNotTagsAnyValues) ); List<String> listNotTagsValues = new ArrayList<>(); listNotTagsValues.add("*value1"); listNotTagsValues.add("value2"); List<TagValues> listbodyNotTags = new ArrayList<>(); listbodyNotTags.add( new TagValues() .withKey("key1") .withValues(listNotTagsValues) ); List<String> listTagsAnyValues = new ArrayList<>(); listTagsAnyValues.add("value1"); listTagsAnyValues.add("value2"); List<TagValues> listbodyTagsAny = new ArrayList<>(); listbodyTagsAny.add( new TagValues() .withKey("key1") .withValues(listTagsAnyValues) ); List<String> listTagsValues = new ArrayList<>(); listTagsValues.add("*value1"); listTagsValues.add("value2"); List<TagValues> listbodyTags = new ArrayList<>(); listbodyTags.add( new TagValues() .withKey("key1") .withValues(listTagsValues) ); body.withMatches(listbodyMatches); body.withAction("filter"); body.withOffset(100); body.withLimit(100); body.withNotTagsAny(listbodyNotTagsAny); body.withNotTags(listbodyNotTags); body.withTagsAny(listbodyTagsAny); body.withTags(listbodyTags); request.withBody(body); try { ListTagResponse response = client.listTag(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 by tag, with the start offset of pagination query set to 100, and the number of resources displayed on each page to 100
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 huaweicloudsdkdns.v2.region.dns_region import DnsRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkdns.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"] projectId = "{project_id}" credentials = BasicCredentials(ak, sk, projectId) client = DnsClient.new_builder() \ .with_credentials(credentials) \ .with_region(DnsRegion.value_of("<YOUR REGION>")) \ .build() try: request = ListTagRequest() request.resource_type = "{resource_type}" listMatchesbody = [ Match( key="resource_name", value="resource1" ) ] listValuesNotTagsAny = [ "value1", "value2" ] listNotTagsAnybody = [ TagValues( key="key1", values=listValuesNotTagsAny ) ] listValuesNotTags = [ "*value1", "value2" ] listNotTagsbody = [ TagValues( key="key1", values=listValuesNotTags ) ] listValuesTagsAny = [ "value1", "value2" ] listTagsAnybody = [ TagValues( key="key1", values=listValuesTagsAny ) ] listValuesTags = [ "*value1", "value2" ] listTagsbody = [ TagValues( key="key1", values=listValuesTags ) ] request.body = ListTagReq( matches=listMatchesbody, action="filter", offset=100, limit=100, not_tags_any=listNotTagsAnybody, not_tags=listNotTagsbody, tags_any=listTagsAnybody, tags=listTagsbody ) response = client.list_tag(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 by tag, with the start offset of pagination query set to 100, and the number of resources displayed on each page to 100
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 113 | package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" dns "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dns/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dns/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dns/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") projectId := "{project_id}" auth, err := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectId). SafeBuild() if err != nil { fmt.Println(err) return } hcClient, err := dns.DnsClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). SafeBuild() if err != nil { fmt.Println(err) return } client := dns.NewDnsClient(hcClient) request := &model.ListTagRequest{} request.ResourceType = "{resource_type}" valueMatches:= "resource1" var listMatchesbody = []model.Match{ { Key: "resource_name", Value: &valueMatches, }, } var listValuesNotTagsAny = []string{ "value1", "value2", } keyNotTagsAny:= "key1" var listNotTagsAnybody = []model.TagValues{ { Key: &keyNotTagsAny, Values: &listValuesNotTagsAny, }, } var listValuesNotTags = []string{ "*value1", "value2", } keyNotTags:= "key1" var listNotTagsbody = []model.TagValues{ { Key: &keyNotTags, Values: &listValuesNotTags, }, } var listValuesTagsAny = []string{ "value1", "value2", } keyTagsAny:= "key1" var listTagsAnybody = []model.TagValues{ { Key: &keyTagsAny, Values: &listValuesTagsAny, }, } var listValuesTags = []string{ "*value1", "value2", } keyTags:= "key1" var listTagsbody = []model.TagValues{ { Key: &keyTags, Values: &listValuesTags, }, } offsetListTagReq:= int32(100) limitListTagReq:= int32(100) request.Body = &model.ListTagReq{ Matches: &listMatchesbody, Action: "filter", Offset: &offsetListTagReq, Limit: &limitListTagReq, NotTagsAny: &listNotTagsAnybody, NotTags: &listNotTagsbody, TagsAny: &listTagsAnybody, Tags: &listTagsbody, } response, err := client.ListTag(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 | Response to the request for querying resources by tag |
Error Codes
See Error Codes.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot