Using Tags to Filter Resources (Streams)
Function
This API is used to filter resources (streams) by tag.
Calling Method
For details, see Calling APIs.
URI
POST /v2/{project_id}/stream/resource_instances/action
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
project_id |
Yes |
String |
Project ID |
Request Parameters
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
X-Auth-Token |
Yes |
String |
User token. It can be obtained by calling the IAM API used to obtain a user token. The value of X-Subject-Token in the response header is the user token. |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
action |
Yes |
String |
Operation to be performed. The value can be filter or count.
Enumeration values:
|
limit |
No |
String |
Number of records to be queried. This parameter is not available when action is set to count. The default value is 1000 when action is set to filter. The maximum value is 1000, and the minimum value is 1. The value cannot be a negative number. Default: 1000 |
offset |
No |
String |
Index position. The query starts from the next data record indexed by this parameter. This parameter is not required when you query data on the first page. The value in the response returned for querying data on the previous page will be included in this parameter for querying data on subsequent pages. 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. |
tags |
No |
Array of Tags objects |
The return result contains resources corresponding to any tag in this parameter. This parameter 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 an empty string. |
tags_any |
No |
Array of Tags objects |
The return result contains resources corresponding to any tag in this parameter. This parameter 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 an empty string. Each tag key must be unique, and each value of the same key must be unique. |
not_tags |
No |
Array of Tags objects |
The return result does not contain resources corresponding to any tag in this parameter. This parameter 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 an empty string. Each tag key must be unique, and each value of the same key must be unique. |
not_tags_any |
No |
Array of Tags objects |
The return result does not contain resources corresponding to any tag in this parameter. This parameter 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 an empty string. Each tag key must be unique, and each value of the same key must be unique. |
matches |
No |
String |
Search criteria. The tag key is the field to match. Currently, only resource_name is supported. The tag value indicates the value to be matched. This field is a fixed dictionary value. |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
key |
No |
String |
Tag key.
Maximum: 36 |
values |
No |
Array of strings |
Tag values. If the value list is empty, this parameter indicates any_value. The values are in the OR relationship. |
Response Parameters
Status code: 200
Parameter |
Type |
Description |
---|---|---|
action |
String |
Operation to be performed. The value can be filter or count.
Enumeration values:
|
limit |
String |
Number of records to be queried. This parameter is not available when action is set to count. The default value is 1000 when action is set to filter. The maximum value is 1000, and the minimum value is 1. The value cannot be a negative number. Default: 1000 |
offset |
String |
Index position. The query starts from the next data record indexed by this parameter. This parameter is not required when you query data on the first page. The value in the response returned for querying data on the previous page will be included in this parameter for querying data on subsequent pages. 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. |
tags |
Array of Tags objects |
The return result contains resources corresponding to any tag in this parameter. This parameter 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 an empty string. |
tags_any |
Array of Tags objects |
The return result contains resources corresponding to any tag in this parameter. This parameter 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 an empty string. Each tag key must be unique, and each value of the same key must be unique. |
not_tags |
Array of Tags objects |
The return result does not contain resources corresponding to any tag in this parameter. This parameter 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 an empty string. Each tag key must be unique, and each value of the same key must be unique. |
not_tags_any |
Array of Tags objects |
The return result does not contain resources corresponding to any tag in this parameter. This parameter 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 an empty string. Each tag key must be unique, and each value of the same key must be unique. |
matches |
String |
Search criteria. The tag key is the field to match. Currently, only resource_name is supported. The tag value indicates the value to be matched. This field is a fixed dictionary value. |
Parameter |
Type |
Description |
---|---|---|
key |
String |
Tag key.
Maximum: 36 |
values |
Array of strings |
Tag values. If the value list is empty, this parameter indicates any_value. The values are in the OR relationship. |
Example Requests
-
Resource (stream) filtering by tag and record querying
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" } ] }
-
Resource (stream) filtering by tag and querying by page
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" ] } ] }
Example Responses
Status code: 200
Request body for filtering resources (streams) by tag
{ "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 Sample Code
The SDK sample code is as follows.
Java
-
Resource (stream) filtering by tag and record querying
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()); } } }
-
Resource (stream) filtering by tag and querying by page
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()); } } }
Python
-
Resource (stream) filtering by tag and record querying
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)
-
Resource (stream) filtering by tag and querying by page
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)
Go
-
Resource (stream) filtering by tag and record querying
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 = List<String>{ "value1", "value2", } var listValuesTags1 = List<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) } }
-
Resource (stream) filtering by tag and querying by page
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) } }
More
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 |
Request body for filtering resources (streams) by tag |
Error Codes
See Error Codes.
Feedback
Was this page helpful?
Provide feedbackThank 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