更新时间:2024-06-13 GMT+08:00
分享

查询资源实例数量

功能介绍

使用标签过滤实例,标签管理服务需要提供按标签过滤各服务实例并汇总显示在列表中,需要各服务提供查询能力。注意:tags, tags_any, not_tags, not_tags_any等字段支持的tag的数量。

调用方法

请参见如何调用API

URI

POST /v1/resource-manager/{resource_type}/resource-instances/count

表1 路径参数

参数

是否必选

参数类型

描述

resource_type

String

资源类型

请求参数

表2 请求Body参数

参数

是否必选

参数类型

描述

without_any_tag

Boolean

不包含任意一个标签,该字段为true时查询所有不带标签的资源,此时忽略 “tags”字段。该字段为false或者未提供该参数时,该条件不生效,即返回所有资源或按"tags","matches"等条件过滤。

缺省值:false

tags

Array of Tag objects

包含标签,最多包含20个key,每个key下面的value最多20个,每个key对应的value可以为空数组但结构体不能缺失。key不能重复,同一个key中values不能重复。结果返回包含所有标签的资源列表,key之间是与的关系,key-value结构中value是或的关系。无tag过滤条件时返回全量数据。

matches

Array of Match objects

搜索字段,key为要匹配的字段,如resource_name等。value为匹配的值。key为固定字典值,不能包含重复的key或不支持的key。根据key的值确认是否需要模糊匹配,如resource_name默认为精确搜索(推荐实现前缀搜索),如果value为空字符串精确匹配(多数服务不存在资源名称为空的情况,因此此类情况返回空列表)。resource_id为精确匹配。第一期只做resource_name,后续再扩展。

表3 Tag

参数

是否必选

参数类型

描述

key

String

键。最大长度128个unicode字符。 key不能为空。(搜索时不对此参数做字符集校验),key不能为空或者空字符串,不能为空格,校验和使用之前先trim 前后半角空格。

最小长度:1

最大长度:128

values

Array of strings

值列表。每个值最大长度255个unicode字符,校验和使用之前先trim 前后半角空格。value可为空数组但不可缺省。如果values为空列表,则表示any_value(查询任意value)。value之间为或的关系。(搜索时不对此参数做字符集校验,只做长度校验)

最大长度:255

数组长度:0 - 20

表4 Match

参数

是否必选

参数类型

描述

key

String

键。第一期限定为resource_name,后续扩展。

value

String

值。每个值最大长度255个unicode字符 。不校验字符集范。

最大长度:255

响应参数

状态码: 200

表5 响应Body参数

参数

参数类型

描述

total_count

Integer

总记录数

状态码: 400

表6 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

error_msg

String

错误消息内容。

状态码: 401

表7 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

error_msg

String

错误消息内容。

状态码: 403

表8 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

error_msg

String

错误消息内容。

状态码: 404

表9 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

error_msg

String

错误消息内容。

状态码: 500

表10 响应Body参数

参数

参数类型

描述

error_code

String

错误码。

error_msg

String

错误消息内容。

请求示例

根据标签查询实例数量

POST https://{endpoint}/v1/resource-manager/{resource_type}/resource-instances/count

{
  "tags" : [ {
    "key" : "key1",
    "values" : [ "value1", "value2" ]
  }, {
    "key" : "key2",
    "values" : [ "value1", "value2" ]
  } ],
  "matches" : [ {
    "key" : "resource_name",
    "value" : "resource1"
  } ]
}

响应示例

状态码: 200

操作成功。

{
  "total_count" : 1000
}

SDK代码示例

SDK代码示例如下。

根据标签查询实例数量

 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
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.GlobalCredentials;
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.config.v1.region.ConfigRegion;
import com.huaweicloud.sdk.config.v1.*;
import com.huaweicloud.sdk.config.v1.model.*;

import java.util.List;
import java.util.ArrayList;

public class CountResourcesByTagSolution {

    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 GlobalCredentials()
                .withAk(ak)
                .withSk(sk);

        ConfigClient client = ConfigClient.newBuilder()
                .withCredential(auth)
                .withRegion(ConfigRegion.valueOf("<YOUR REGION>"))
                .build();
        CountResourcesByTagRequest request = new CountResourcesByTagRequest();
        ResourceInstancesReq body = new ResourceInstancesReq();
        List<Match> listbodyMatches = new ArrayList<>();
        listbodyMatches.add(
            new Match()
                .withKey(Match.KeyEnum.fromValue("resource_name"))
                .withValue("resource1")
        );
        List<String> listTagsValues = new ArrayList<>();
        listTagsValues.add("value1");
        listTagsValues.add("value2");
        List<String> listTagsValues1 = new ArrayList<>();
        listTagsValues1.add("value1");
        listTagsValues1.add("value2");
        List<Tag> listbodyTags = new ArrayList<>();
        listbodyTags.add(
            new Tag()
                .withKey("key1")
                .withValues(listTagsValues1)
        );
        listbodyTags.add(
            new Tag()
                .withKey("key2")
                .withValues(listTagsValues)
        );
        body.withMatches(listbodyMatches);
        body.withTags(listbodyTags);
        request.withBody(body);
        try {
            CountResourcesByTagResponse response = client.countResourcesByTag(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());
        }
    }
}

根据标签查询实例数量

 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
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import GlobalCredentials
from huaweicloudsdkconfig.v1.region.config_region import ConfigRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkconfig.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"]

    credentials = GlobalCredentials(ak, sk)

    client = ConfigClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(ConfigRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = CountResourcesByTagRequest()
        listMatchesbody = [
            Match(
                key="resource_name",
                value="resource1"
            )
        ]
        listValuesTags = [
            "value1",
            "value2"
        ]
        listValuesTags1 = [
            "value1",
            "value2"
        ]
        listTagsbody = [
            Tag(
                key="key1",
                values=listValuesTags1
            ),
            Tag(
                key="key2",
                values=listValuesTags
            )
        ]
        request.body = ResourceInstancesReq(
            matches=listMatchesbody,
            tags=listTagsbody
        )
        response = client.count_resources_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)

根据标签查询实例数量

 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 main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/global"
    config "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/config/v1"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/config/v1/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/config/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")

    auth := global.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        Build()

    client := config.NewConfigClient(
        config.ConfigClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.CountResourcesByTagRequest{}
	var listMatchesbody = []model.Match{
        {
            Key: model.GetMatchKeyEnum().RESOURCE_NAME,
            Value: "resource1",
        },
    }
	var listValuesTags = []string{
        "value1",
	    "value2",
    }
	var listValuesTags1 = []string{
        "value1",
	    "value2",
    }
	var listTagsbody = []model.Tag{
        {
            Key: "key1",
            Values: listValuesTags1,
        },
        {
            Key: "key2",
            Values: listValuesTags,
        },
    }
	request.Body = &model.ResourceInstancesReq{
		Matches: &listMatchesbody,
		Tags: &listTagsbody,
	}
	response, err := client.CountResourcesByTag(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

操作成功。

400

输入参数不合法。

401

鉴权失败。

403

用户身份认证失败。

404

资源未找到

500

服务器内部错误。

错误码

请参见错误码

分享:

    相关文档

    相关产品