Querying Resources by Tag
Function
This API is used to query resources by tag.
URI
POST /v2/{project_id}/{resource_type}/resource_instances/action
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
project_id |
Yes |
String |
Specifies the project ID. |
|
resource_type |
Yes |
String |
Specifies the resource type. The options are as follows:
|
Request Parameters
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
X-Auth-Token |
Yes |
String |
Specifies the user token. It can be obtained by calling the IAM API (value of X-Subject-Token in the response header). |
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
tags |
No |
Array of ResourceTags objects |
A maximum of 10 tag keys are allowed in each query operation. Each tag key can have up to 10 tag values. The structure body is mandatory. The tag key cannot be left blank or be an empty string. Each tag key and tag values of one key must be unique. Resources identified by different keys are in AND relationship. |
|
tags_any |
No |
Array of ResourceTags objects |
A maximum of 10 tag keys are allowed in each query operation. Each tag key can have up to 10 tag values. The structure body is mandatory. The tag key cannot be left blank or be an empty string. Each tag key and tag values of one key must be unique. Resources identified by different keys are in OR relationship. |
|
not_tags |
No |
Array of ResourceTags objects |
A maximum of 10 tag keys are allowed in each query operation. Each tag key can have up to 10 tag values. The structure body is mandatory. The tag key cannot be left blank or be an empty string. Each tag key and tag values of one key must be unique. Resources identified by different keys are in NAND relationship. |
|
not_tags_any |
No |
Array of ResourceTags objects |
A maximum of 10 tag keys are allowed in each query operation. Each tag key can have up to 10 tag values. The structure body is mandatory. The tag key cannot be left blank or be an empty string. Each tag key and tag values of one key must be unique. Resources identified by different keys are in NOR relationship. |
|
offset |
No |
String |
Specifies the index position. The query starts from the next data record indexed by this parameter. You do not need to specify this parameter when querying resources on the first page. When you query resources on subsequent pages, set this parameter to the location returned in the response body for the previous query. If you set action to count, this parameter is not required. If you set action to filter, the offset value is 0 by default. The offset value cannot be a negative integer. Default: 0 |
|
limit |
No |
String |
Specifies the number of records to be queried. If you set action to count, this parameter is not required. If you set action to filter, the value of this parameter is 1000 by default. The maximum value of limit is 1000 and the minimum value is 1. limit cannot be a negative number. Default value: 1000 |
|
action |
Yes |
String |
Specifies the operation to be performed. The value can be filter (filtering) or count (querying the total number). filter: queries resources in pages by filter conditions. count: queries the total number of resources meeting the filter conditions. |
|
matches |
No |
Array of TagMatch objects |
Specifies the field to be matched. The key can only be resource_name. The value will be exactly matched. |
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
key |
Yes |
String |
Specifies the tag key.
|
|
values |
Yes |
Array of strings |
Specifies the tag values.
|
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
key |
Yes |
String |
Specifies the key field to be matched. The tag key can only be resource_name. In such a case, the tag value is the ECS name.
|
|
value |
Yes |
String |
Specifies the tag value. The tag key can only be resource_name. In such a case, the tag value is the ECS name.
|
Response Parameters
Status code: 200
|
Parameter |
Type |
Description |
|---|---|---|
|
resources |
Array of TagResource objects |
Specifies the returned resource list. |
|
total_count |
Integer |
Specifies the total number of records. |
|
Parameter |
Type |
Description |
|---|---|---|
|
resource_id |
String |
Specifies the resource ID. |
|
resource_detail |
ResourceDetail object |
Specifies the resource details. The value is a resource object, used for extension. The value is left blank by default.
|
|
tags |
Array of ResourceTag objects |
Specifies the tag list. |
|
resource_name |
String |
Specifies the resource name. |
|
Parameter |
Type |
Description |
|---|---|---|
|
enterprise_project_id |
String |
Specifies the enterprise project ID. |
|
detailId |
String |
Specifies the details ID. |
|
topic_urn |
String |
Specifies the unique identifier of the topic. |
|
display_name |
String |
Specifies the display name. |
|
Parameter |
Type |
Description |
|---|---|---|
|
key |
String |
Specifies the key field to be matched. The tag key can only be resource_name. In such a case, the tag value is the ECS name.
|
|
value |
String |
Specifies the tag value. The tag key can only be resource_name. In such a case, the tag value is the ECS name.
|
Status code: 400
|
Parameter |
Type |
Description |
|---|---|---|
|
request_id |
String |
Specifies the request ID, which is unique. |
|
code |
String |
Specifies the error code. |
|
message |
String |
Describes the error message. |
Status code: 403
|
Parameter |
Type |
Description |
|---|---|---|
|
request_id |
String |
Specifies the request ID, which is unique. |
|
code |
String |
Specifies the error code. |
|
message |
String |
Describes the error message. |
Status code: 404
|
Parameter |
Type |
Description |
|---|---|---|
|
request_id |
String |
Specifies the request ID, which is unique. |
|
code |
String |
Specifies the error code. |
|
message |
String |
Describes the error message. |
Status code: 500
|
Parameter |
Type |
Description |
|---|---|---|
|
request_id |
String |
Specifies the request ID, which is unique. |
|
code |
String |
Specifies the error code. |
|
message |
String |
Describes the error message. |
Example Requests
- Using tag to filter resource instances
POST https://{SMN_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" ] }, { "key" : "key2", "values" : [ "*value21", "value22" ] } ], "tags" : [ { "key" : "key1", "values" : [ "*value1", "value2" ] } ], "tags_any" : [ { "key" : "key1", "values" : [ "value1", "value2" ] } ], "not_tags_any" : [ { "key" : "key1", "values" : [ "value1", "value2" ] } ] } - Using tag to query the total number of resource instances
POST https://{SMN_Endpoint}/v2/{project_id}/{resource_type}/resource_instances/action { "action" : "count", "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" ] } ], "matches" : [ { "key" : "resource_name", "value" : "resource" } ] }
Example Responses
Status code: 200
OK
{
"resources" : {
"resource_detail" : {
"topic_urn" : "urn:smn:regionId:f96188c7ccaf4ffba0c9aa149ab2bd57:resource1",
"display_name" : "testtest",
"enterprise_project_id" : "0"
},
"resource_id" : "cffe4fc4c9a54219b60dbaf7b586e132",
"resource_name" : "resource1",
"tags" : [ {
"key" : "key1",
"value" : "value1"
} ]
},
"total_count" : 1000
}
Status Codes
|
Status Code |
Description |
|---|---|
|
200 |
OK |
|
400 |
Bad Request |
|
403 |
Unauthorized |
|
404 |
Not Found |
|
500 |
Internal Server Error |
Error Codes
See Error Codes.
Last Article: Querying Project Tags
Next Article: Deleting Resource Tags
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.