根据标签查询资源实例列表
功能介绍
根据标签查询资源实例列表
调用方法
请参见如何调用API。
URI
POST /v1/{project_id}/{resource_type}/resource-instances/filter
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
project_id |
是 |
String |
项目ID |
resource_type |
是 |
String |
资源类型。
|
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
limit |
否 |
String |
查询记录数(action为count时无此参数)如果action为filter默认为1000,limit最多为1000,不能为负数,最小值为1。 |
offset |
否 |
String |
索引位置,偏移量(action为count时无此参数)从第一条数据偏移offset条数据后开始查询,如果action为filter默认为0(偏移0条数据,表示从第一条数据开始查询),必须为数字,不能为负数。 |
请求参数
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
X-Auth-Token |
是 |
String |
用户Token。 通过调用IAM服务查询用户Token接口获取(响应消息头中X-Subject-Token的值)。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
matches |
否 |
Array of matches objects |
搜索字段,key为要匹配的字段,如resource_name等。value为匹配的值。key为固定字典值,不能包含重复的key或不支持的key。 根据key的值确认是否需要模糊匹配,如resource_name默认为模糊搜索(不区分大小写),如果value为空字符串精确匹配(多数服务不存在资源名称为空的情况,因此此类情况返回空列表)。resource_id为精确匹配。第一期只做resource_name,后续再扩展。 |
not_tags |
否 |
Array of TagKeyValuesBean objects |
不包含标签,最多包含50个key,每个key下面的value最多10个, 每个key对应的value可以为空数组但结构体不能缺失。Key不能重复,同一个key中values不能重复。结果返回不包含标签的资源列表,key之间是与的关系,key-value结构中value是或的关系。无过滤条件时返回全量数据 |
tags |
否 |
Array of TagKeyValuesBean objects |
包含标签,最多包含50个key,每个key下面的value最多10个,每个key对应的value可以为空数组但结构体不能缺失。Key不能重复,同一个key中values不能重复。结果返回包含所有标签的资源列表,key之间是与的关系,key-value结构中value是或的关系。无tag过滤条件时返回全量数据 |
tags_any |
否 |
Array of TagKeyValuesBean objects |
包含任意标签,最多包含50个key,每个key下面的value最多10个, 每个key对应的value可以为空数组但结构体不能缺失。Key不能重复,同一个key中values不能重复。结果返回包含标签的资源列表,key之间是或的关系,key-value结构中value是或的关系。无过滤条件时返回全量数据 |
not_tags_any |
否 |
Array of TagKeyValuesBean objects |
不包含任意标签,最多包含50个key,每个key下面的value最多10个, 每个key对应的value可以为空数组但结构体不能缺失。Key不能重复,同一个key中values不能重复。结果返回不包含标签的资源列表,key之间是或的关系,key-value结构中value是或的关系。无过滤条件时返回全量数据 |
sys_tags |
否 |
TagKeyValuesBean object |
仅op_service权限可以使用此字段做资源实例过滤条件。目前TMS调用时只包含一个tag结构体。key:_sys_enterprise_project_idvalue:企业项目id列表目前TMS调用时,key下面只包含一个value。0表示默认企业项目sys_tags和租户标签过滤条件(without_any_tag 、tags、tags_any、not_tags、not_tags_any)不能同时使用无sys_tags时按照tag接口处理,无tag过滤条件时返回全量数据 |
without_any_tag |
否 |
Boolean |
不包含任意一个标签,该字段为true时查询所有不带标签的资源,此时忽略 “tags”、“tags_any”、“not_tags”、“not_tags_any”字段 |
响应参数
状态码: 200
参数 |
参数类型 |
描述 |
---|---|---|
resources |
Array of resources objects |
资源实例列表 |
total_count |
Integer |
总记录数 |
参数 |
参数类型 |
描述 |
---|---|---|
resource_id |
String |
资源ID |
resource_name |
String |
资源名称,资源没有名称时默认为空字符串,eip返回ip地址。 |
resource_detail |
Object |
资源详情。 资源对象,用于扩展,默认为空。 |
tags |
Array of tags objects |
标签列表,没有标签默认为空数组 |
sys_tags |
Array of sys_tags objects |
仅op_service权限才可以可以获取此字段: 目前只包含一个resource_tag 结构体 key:_sys_enterprise_project_id value:企业项目id,0表示默认企业项目 非op_service场景不能返回此字段 |
状态码: 400
参数 |
参数类型 |
描述 |
---|---|---|
error |
Object |
错误信息返回体。 |
参数 |
参数类型 |
描述 |
---|---|---|
error_code |
String |
错误请求返回的错误码。 |
error_msg |
String |
错误请求返回的错误信息。 |
状态码: 403
参数 |
参数类型 |
描述 |
---|---|---|
error |
Object |
错误信息返回体。 |
参数 |
参数类型 |
描述 |
---|---|---|
error_code |
String |
错误请求返回的错误码。 |
error_msg |
String |
错误请求返回的错误信息。 |
状态码: 500
参数 |
参数类型 |
描述 |
---|---|---|
error |
Object |
错误信息返回体。 |
参数 |
参数类型 |
描述 |
---|---|---|
error_code |
String |
错误请求返回的错误码。 |
error_msg |
String |
错误请求返回的错误信息。 |
请求示例
/v1/{project_id}/{resource_type}/resource-instances/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" ] } ], "sys_tags" : [ { "key" : "_sys_enterprise_project_id", "values" : [ "5aa119a8-d25b-45a7-8d1b-88e127885635" ] } ] }
响应示例
状态码: 200
成功
{ "resources" : [ { "resource_detail" : null, "resource_id" : "cdfs_cefs_wesas_12_dsad", "resource_name" : "resouece1", "tags" : [ { "key" : "key1", "value" : "value1" }, { "key" : "key2", "value" : "value1" } ], "sys_tags" : [ { "key" : "_sys_enterprise_project_id", "value" : "5aa119a8-d25b-45a7-8d1b-88e127885635" } ] } ], "total_count" : 1000 }
状态码: 400
失败
{ "error" : { "error_code" : "DBSS.XXXX", "error_msg" : "XXX" } }
SDK代码示例
SDK代码示例如下。
Java
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 |
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.dbss.v1.region.DbssRegion; import com.huaweicloud.sdk.dbss.v1.*; import com.huaweicloud.sdk.dbss.v1.model.*; import java.util.List; import java.util.ArrayList; public class ListResourceInstanceByTagSolution { 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); DbssClient client = DbssClient.newBuilder() .withCredential(auth) .withRegion(DbssRegion.valueOf("<YOUR REGION>")) .build(); ListResourceInstanceByTagRequest request = new ListResourceInstanceByTagRequest(); request.withResourceType("{resource_type}"); ResourceInstanceTagRequest body = new ResourceInstanceTagRequest(); List<String> listNotTagsAnyValues = new ArrayList<>(); listNotTagsAnyValues.add("value1"); listNotTagsAnyValues.add("value2"); List<TagKeyValuesBean> listbodyNotTagsAny = new ArrayList<>(); listbodyNotTagsAny.add( new TagKeyValuesBean() .withKey("key1") .withValues(listNotTagsAnyValues) ); List<String> listTagsAnyValues = new ArrayList<>(); listTagsAnyValues.add("value1"); listTagsAnyValues.add("value2"); List<TagKeyValuesBean> listbodyTagsAny = new ArrayList<>(); listbodyTagsAny.add( new TagKeyValuesBean() .withKey("key1") .withValues(listTagsAnyValues) ); List<String> listTagsValues = new ArrayList<>(); listTagsValues.add("*value1"); listTagsValues.add("value2"); List<TagKeyValuesBean> listbodyTags = new ArrayList<>(); listbodyTags.add( new TagKeyValuesBean() .withKey("key1") .withValues(listTagsValues) ); List<String> listNotTagsValues = new ArrayList<>(); listNotTagsValues.add("*value1"); listNotTagsValues.add("value2"); List<TagKeyValuesBean> listbodyNotTags = new ArrayList<>(); listbodyNotTags.add( new TagKeyValuesBean() .withKey("key1") .withValues(listNotTagsValues) ); List<ResourceInstanceTagRequestMatches> listbodyMatches = new ArrayList<>(); listbodyMatches.add( new ResourceInstanceTagRequestMatches() .withKey("resource_name") .withValue("resource1") ); body.withSysTags("[{\"values\":[\"5aa119a8-d25b-45a7-8d1b-88e127885635\"],\"key\":\"_sys_enterprise_project_id\"}]"); body.withNotTagsAny(listbodyNotTagsAny); body.withTagsAny(listbodyTagsAny); body.withTags(listbodyTags); body.withNotTags(listbodyNotTags); body.withMatches(listbodyMatches); request.withBody(body); try { ListResourceInstanceByTagResponse response = client.listResourceInstanceByTag(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
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 |
# coding: utf-8 import os from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkdbss.v1.region.dbss_region import DbssRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkdbss.v1 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 = DbssClient.new_builder() \ .with_credentials(credentials) \ .with_region(DbssRegion.value_of("<YOUR REGION>")) \ .build() try: request = ListResourceInstanceByTagRequest() request.resource_type = "{resource_type}" listValuesNotTagsAny = [ "value1", "value2" ] listNotTagsAnybody = [ TagKeyValuesBean( key="key1", values=listValuesNotTagsAny ) ] listValuesTagsAny = [ "value1", "value2" ] listTagsAnybody = [ TagKeyValuesBean( key="key1", values=listValuesTagsAny ) ] listValuesTags = [ "*value1", "value2" ] listTagsbody = [ TagKeyValuesBean( key="key1", values=listValuesTags ) ] listValuesNotTags = [ "*value1", "value2" ] listNotTagsbody = [ TagKeyValuesBean( key="key1", values=listValuesNotTags ) ] listMatchesbody = [ ResourceInstanceTagRequestMatches( key="resource_name", value="resource1" ) ] request.body = ResourceInstanceTagRequest( sys_tags="[{\"values\":[\"5aa119a8-d25b-45a7-8d1b-88e127885635\"],\"key\":\"_sys_enterprise_project_id\"}]", not_tags_any=listNotTagsAnybody, tags_any=listTagsAnybody, tags=listTagsbody, not_tags=listNotTagsbody, matches=listMatchesbody ) response = client.list_resource_instance_by_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) |
Go
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 |
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" dbss "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dbss/v1" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dbss/v1/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dbss/v1/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 := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectId). Build() client := dbss.NewDbssClient( dbss.DbssClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.ListResourceInstanceByTagRequest{} request.ResourceType = "{resource_type}" var listValuesNotTagsAny = []string{ "value1", "value2", } var listNotTagsAnybody = []model.TagKeyValuesBean{ { Key: "key1", Values: listValuesNotTagsAny, }, } var listValuesTagsAny = []string{ "value1", "value2", } var listTagsAnybody = []model.TagKeyValuesBean{ { Key: "key1", Values: listValuesTagsAny, }, } var listValuesTags = []string{ "*value1", "value2", } var listTagsbody = []model.TagKeyValuesBean{ { Key: "key1", Values: listValuesTags, }, } var listValuesNotTags = []string{ "*value1", "value2", } var listNotTagsbody = []model.TagKeyValuesBean{ { Key: "key1", Values: listValuesNotTags, }, } keyMatches:= "resource_name" valueMatches:= "resource1" var listMatchesbody = []model.ResourceInstanceTagRequestMatches{ { Key: &keyMatches, Value: &valueMatches, }, } var sysTagsSysTags interface{} = "[{\"values\":[\"5aa119a8-d25b-45a7-8d1b-88e127885635\"],\"key\":\"_sys_enterprise_project_id\"}]" request.Body = &model.ResourceInstanceTagRequest{ SysTags: &sysTagsSysTags, NotTagsAny: &listNotTagsAnybody, TagsAny: &listTagsAnybody, Tags: &listTagsbody, NotTags: &listNotTagsbody, Matches: &listMatchesbody, } response, err := client.ListResourceInstanceByTag(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } } |
更多
更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。
状态码
状态码 |
描述 |
---|---|
200 |
成功 |
400 |
失败 |
403 |
认证失败 |
500 |
服务端错误 |
错误码
请参见错误码。