• Elastic Cloud Server

ecs
  1. Help Center
  2. Elastic Cloud Server
  3. API Reference
  4. Extension ECS APIs
  5. Tag Management
  6. Querying ECSs by Tag

Querying ECSs by Tag

Function Description

This interface is used to filter ECSs by tag and obtain all tags of an ECS.

URI

POST /v1/{project_id}/servers/resource_instances/action

Table 1 describes the parameters in the URI.
Table 1 Parameter description

Parameter

Mandatory

Description

project_id

Yes

Specifies the project ID.

Request

Request parameters

Table 2 describes the request parameters.
Table 2 Request parameters

Parameter

Mandatory

Type

Description

tags

No

List<tag>

Displays the ECSs with all the specified tags. For details, see Table 3.

  • A maximum of 10 keys are included. Each key can have a maximum of 10 values.
  • The structure body must be included.
  • This field cannot be left blank.
  • A key must be unique.
  • Values of the same key must be unique.

not_tags

No

List<tag>

Displays the ECSs with none of specified tags.

  • A maximum of 10 keys are included. Each key can have a maximum of 10 values.
  • The structure body must be included.
  • This field cannot be left blank.
  • Keys must be unique.
  • Values of the same key must be unique.

limit

No

String

Limits the maximum number of queried ECSs. The value cannot be a negative number. The maximum value is 1000.

  • If the action value is count, this parameter is invalid.
  • If the action value is filter, the default value is 1000.

offset

No

String

Specifies index position. The query starts from the next piece of data indexed by this parameter. The value must be a number and cannot be a negative number.

This parameter is not required when data on the first page is queried. When you query the subsequent page data, the value in the response body for the query of the data on the previous page is contained in this parameter.

  • If the action value is count, this parameter is invalid.
  • If the action value is filter, the default value is 0.

action

Yes

String

Specifies the operation, which can be filter or count.

  • filter: filters ECSs by tag. The ECSs that meet the filter criteria are displayed on pages.
  • count: searches ECSs by tag. The number of ECSs that meet the search criteria is displayed.

matches

No

List<match>

Specifies the search field, which is used to search for ECSs.

Currently, only resource_name can be used for search. For more information, see Table 4.

Table 3 tag field description

Parameter

Mandatory

Type

Description

key

Yes

String

Specifies the tag key.

  • A key contains a maximum of 127 Unicode characters.
  • This field cannot be left blank.

values

No

List<String>

Specifies the tag value.

  • Each tag contains a maximum of 10 values.
  • Values of the same tag must be unique.
  • Each value contains a maximum of 255 Unicode characters.
  • If this parameter is not specified, any value can be used.
  • The values are in the OR relationship.
Table 4 match field description

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.

  • The key must be unique, and the value is used for matching.
  • This field is a fixed dictionary value.
  • This field cannot be left blank.

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.

  • Each value contains a maximum of 255 Unicode characters.
  • This field cannot be left blank.

Response

Response parameters

Table 5 describes the response parameters.

Table 5 Response parameters

Parameter

Type

Description

resources

List<resource>

Specifies returned ECSs. For details, see Table 6.

total_count

Integer

Specifies the total number of queried ECSs.

Table 6 resource field description

Parameter

Type

Description

resource_id

String

Specifies the ECS ID.

resource_detail

String

Queries ECS details.

tags

List<resource_tag>

Specifies tags.

resource_name

String

Specifies the resource name, which is the ECS name.

Table 7 resource_tag field description

Parameter

Type

Description

key

String

Specifies the tag key.

  • It contains a maximum of 36 Unicode characters.
  • This field cannot be left blank.
  • Can only consist of digits, letters, hyphens (-), and underscores (_).

value

String

Specifies the tag value.

  • Each value contains a maximum of 43 Unicode characters.
  • This field can be left blank.
  • Can only consist of digits, letters, hyphens (-), and underscores (_).

Examples

  • Example request
    POST /v1/{project_id}/servers/resource_instances/action
    {
        "offset": "100", 
        "limit": "100", 
        "action": "filter", 
        "matches":[
        {
            "key": "resource_name", 
            "value": "ecs_test"
         }], 
        "tags": [
        {
            "key": "key1", 
            "values": [
                "value1", 
                "value2"
            ]
        }]
    }
  • Example response
    • Response body when action is set to filter
      {
            "resources": [
               {
                  "resource_detail": null, 
                  "resource_id": "cdfs_cefs_wesas_12_dsad", 
                  "resource_name": "ecs_test", 
                  "tags": [
                      {
                         "key": "key1",
                         "value": "value1"
                      }
                   ]
               }
             ], 
            "total_count": 1000
      }
    • Response body when action is set to count
      {
             "total_count": 1000
      }

Returned Values

See General Request Returned Values.

Error Code

See Error Code Description.