Help Center/ Cloud Bastion Host/ API Reference/ API Description/ Label Management/ Counting the Number of Instances with a Specified Tag
Updated on 2024-11-12 GMT+08:00

Counting the Number of Instances with a Specified Tag

Function

This API is used to count the number of instances with a specified tag.

URI

POST /v2/{project_id}/cbs/instance/count

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Project ID

For details about how to obtain the project ID, see "Obtaining a Project ID".

Request Parameters

Table 2 Request header 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.

For details, see "Obtaining the Token of an IAM User."

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

without_any_tag

No

Boolean

Specifies any tag that is not contained. If this parameter is set to true, all resources without tags are queried.

In this case, the tags, tags_any, not_tags, and not_tags_any fields are ignored.

tags

No

Array of Tags objects

The resources to be queried contain tags listed in tags. Each resource to be queried contains a maximum of 50 keys. Each tag key can have a maximum of 10 tag values. The tag value corresponding to each tag key can be an empty array but the structure cannot be missing.

The key must be unique, and the values of the same key must be unique. Resources containing all tags are returned. Keys in this list are in AND relationship. While values in the key-value pairs are in OR relationship.

If no tag filtering condition is specified, full data is returned.

tags_any

No

Array of Tags objects

The resources to be queried contain any tags listed in tags_any. Each resource to be queried contains a maximum of 50 keys. Each tag key can have a maximum of 10 tag values. The tag value corresponding to each tag key can be an empty array but the structure cannot be missing. Each tag key must be unique, and each value of the same key must be unique.

The response returns resources containing the tags in this list. Keys in this list are in an OR relationship and values in each key-value structure are also in an OR relationship.

If no tag filtering condition is specified, full data is returned.

not_tags

No

Array of Tags objects

The resources to be queried do not contain tags listed in not_tags. Each resource to be queried contains a maximum of 50 keys. Each tag key can have a maximum of 10 tag values. The tag value corresponding to each tag key can be an empty array but the structure cannot be missing. Each tag key must be unique, and each value of the same key must be unique.

The response returns resources containing no tags in this list. Keys in this list are in an AND relationship while values in each key-value structure are in an OR relationship.

If no filtering condition is specified, full data is returned.

not_tags_any

No

Array of Tags objects

The resources to be queried do not contain any tags listed in not_tags_any. Each resource to be queried contains a maximum of 50 keys. Each tag key can have a maximum of 10 tag values. The tag value corresponding to each tag key can be an empty array but the structure cannot be missing. Each tag key must be unique, and each value of the same key must be unique.

The response returns resources containing no tag in this list. Keys in this list are in the OR relationship and values in each key-value structure are also in the OR relationship.

If no tag filtering criteria is specified, full data is returned.

sys_tags

No

Array of Tags objects

Only users with the op_service permission can use this field to filter resources.

Only one tag structure is contained when this API is called by Tag Management Service (TMS).

key: _sys_enterprise_project_id

value: Enterprise project ID list.

Currently, key contains only one value. 0 indicates the default enterprise project.

sys_tags and tenant tag filtering conditions (without_any_tag, tags, tags_any, not_tags, and not_tags_any) cannot be used at the same time.

If sys_tags is not specified, filter the resources with other tag filtering criteria. If no tag filtering criteria is specified, full data is returned.

matches

No

Array of Match objects

Search field. The tag key is the field to be matched, for example, resource_name.

The value is a matched value. The key is a fixed dictionary value and cannot be a duplicate key or unsupported key.

Check whether fuzzy match is required based on the key value. For example, if key is set to resource_name, fuzzy search (case-insensitive) is performed by default. If value is empty, exact match is performed. Most services do not have resources without names. In this case, an empty list is returned.

resource_id indicates exact match. Only resource_name is used now.

Table 4 Tags

Parameter

Mandatory

Type

Description

key

Yes

String

Tag key.

NOTE:

Note:

  • A key can contain 1 to 128 characters.

  • A key can contain UTF-8 letters, digits, spaces, and the following characters: _. : = + - @

  • Do not enter labels starting with sys, which are system labels.

  • Recommended regular expression: ^((?!sys)[\p{L}\p{Z}\p{N}_.:=+\-@]*)$

values

Yes

Array of strings

List of tag values.

NOTE:

Note:

  • A value can contain 0 to 255 characters.

  • Can contain UTF-8 letters, digits, spaces, and the following characters: . : / = + - @ Recommended regular expression: ^([\p{L}\p{Z}\p{N}.:/=+-@]*)$

  • The value can be empty or null.

  • The value of a predefined tag cannot be empty or null.

Table 5 Match

Parameter

Mandatory

Type

Description

key

Yes

String

Tag key. Currently, only resource_name is supported. Other key values will be available later.

value

Yes

String

Value. Each value can contain a maximum of 255 Unicode characters. The character set is not verified.

Response Parameters

Status code: 200

Table 6 Response body parameters

Parameter

Type

Description

total_count

Integer

Total records.

Status code: 400

Table 7 Response body parameters

Parameter

Type

Description

error_code

String

Error code

error_description

String

Incorrect request parameters.

Status code: 401

Table 8 Response body parameters

Parameter

Type

Description

error_code

String

Error code

error_description

String

Failed to verify the token.

Status code: 403

Table 9 Response body parameters

Parameter

Type

Description

error_code

String

Error code

error_description

String

Permissions required.

Status code: 404

Table 10 Response body parameters

Parameter

Type

Description

error_code

String

Error code

error_description

String

Not CBH instances found.

Example Requests

{
  "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" ]
  } ]
}

Example Responses

Status code: 200

Show Project Tags Success

{
  "total_count" : 1000
}

Status Codes

Status Code

Description

200

Show Project Tags Success

400

Bad Request

401

Unauthorized

403

Forbidden

404

Not Found

Error Codes

See Error Codes.