Updated on 2024-09-06 GMT+08:00

Querying Resources by Tag

Function

This API is used to query resources by tag.

Calling Method

For details, see Calling APIs.

URI

POST /v2/{project_id}/{resource_type}/resource_instances/action

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

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

resource_type

Yes

String

Specifies the resource type. Possible values are:

smn_topic: topic

smn_sms: SMS

smn_application: mobile push

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

String

Specifies a user token.

It can be obtained by calling an IAM API. The value of X-Subject-Token in the response header is the user token.

Table 3 Request body parameters

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 you query 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 action is set to count, this parameter does not take effect.If you set action to filter, the offset value is 0 by default. The offset value cannot be a negative integer.

limit

No

String

Specifies the maximum number of resources to be queried.

If action is set to count, this parameter does not take effect.

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.

action

Yes

String

Specifies the operation to be performed. The value can be filter (filtering) or count (querying the total number).

filter indicates pagination query. count indicates that the total number of query results meeting the search criteria will be returned.

matches

No

Array of TagMatch objects

Specifies the key-value pair to be matched.

The key can only be resource_name.

The value will be exactly matched.

Table 4 ResourceTags

Parameter

Mandatory

Type

Description

key

Yes

String

Specifies the tag key.

  • A key can contain a maximum of 127 Unicode characters.

  • key must be specified.

values

Yes

Array of strings

Specifies the tag values.

  • Each tag contains a maximum of 10 values.

  • Values of the same tag must be unique.

  • It can contain a maximum of 255 Unicode characters.

  • If this parameter is left blank, any value can be used.

  • All values of a tag key are in the OR relationship.

Table 5 TagMatch

Parameter

Mandatory

Type

Description

key

Yes

String

Specifies the key field to be matched.

key can only be resource_name. In such a case, value is the ECS name.

  • The key must be unique, and the value is used for matching.

  • The key field is a fixed dictionary value.

  • key cannot be left blank.

value

Yes

String

Specifies the value.

key can only be resource_name. In such a case, value is the ECS name.

  • It can contain a maximum of 255 Unicode characters.

  • This field cannot be left blank.

Response Parameters

Status code: 200

Table 6 Response body parameters

Parameter

Type

Description

resources

Array of TagResource objects

Specifies the returned resource list.

total_count

Integer

Specifies the total number of records.

Table 7 TagResource

Parameter

Type

Description

resource_id

String

Specifies the resource ID.

resource_detail

ResourceDetail object

Specifies the resource details.

tags

Array of ResourceTag objects

Specifies the list of tags.

resource_name

String

Specifies the resource name.

Table 8 ResourceDetail

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.

Table 9 ResourceTag

Parameter

Type

Description

key

String

Specifies the key field to be matched.

key can only be resource_name. In such a case, value is the ECS name.

  • The key must be unique, and the value is used for matching.

  • The key field is a fixed dictionary value.

  • key cannot be left blank.

value

String

Specifies the value.

key can only be resource_name. In such a case, value is the ECS name.

  • It can contain a maximum of 255 Unicode characters.

  • This field cannot be left blank.

Status code: 400

Table 10 Response body parameters

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

Table 11 Response body parameters

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

Table 12 Response body parameters

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

Table 13 Response body parameters

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 tags to filter resources

    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 tags to query the total number of resources

    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" : "resouurce"
      } ]
    }

Example Responses

Status code: 200

OK

{
  "resources" : {
    "resource_detail" : {
      "topic_urn" : "urn:smn:regionId:f96188c7ccaf4ffba0c9aa149ab2bd57:resouece1",
      "display_name" : "testtest",
      "enterprise_project_id" : "0"
    },
    "resource_id" : "cffe4fc4c9a54219b60dbaf7b586e132",
    "resource_name" : "resouece1",
    "tags" : [ {
      "key" : "key1",
      "value" : "value1"
    } ]
  },
  "total_count" : 1000
}

SDK Sample Code

The SDK sample code is as follows.

Java

  • Using tags to filter resources

      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
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    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.smn.v2.region.SmnRegion;
    import com.huaweicloud.sdk.smn.v2.*;
    import com.huaweicloud.sdk.smn.v2.model.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class ListResourceInstancesSolution {
    
        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);
    
            SmnClient client = SmnClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(SmnRegion.valueOf("<YOUR REGION>"))
                    .build();
            ListResourceInstancesRequest request = new ListResourceInstancesRequest();
            request.withResourceType("{resource_type}");
            ListInstanceRequestBody body = new ListInstanceRequestBody();
            List<TagMatch> listbodyMatches = new ArrayList<>();
            listbodyMatches.add(
                new TagMatch()
                    .withKey("resource_name")
                    .withValue("resource1")
            );
            List<String> listNotTagsAnyValues = new ArrayList<>();
            listNotTagsAnyValues.add("value1");
            listNotTagsAnyValues.add("value2");
            List<ResourceTags> listbodyNotTagsAny = new ArrayList<>();
            listbodyNotTagsAny.add(
                new ResourceTags()
                    .withKey("key1")
                    .withValues(listNotTagsAnyValues)
            );
            List<String> listNotTagsValues = new ArrayList<>();
            listNotTagsValues.add("*value21");
            listNotTagsValues.add("value22");
            List<String> listNotTagsValues1 = new ArrayList<>();
            listNotTagsValues1.add("*value1");
            listNotTagsValues1.add("value2");
            List<ResourceTags> listbodyNotTags = new ArrayList<>();
            listbodyNotTags.add(
                new ResourceTags()
                    .withKey("key1")
                    .withValues(listNotTagsValues1)
            );
            listbodyNotTags.add(
                new ResourceTags()
                    .withKey("key2")
                    .withValues(listNotTagsValues)
            );
            List<String> listTagsAnyValues = new ArrayList<>();
            listTagsAnyValues.add("value1");
            listTagsAnyValues.add("value2");
            List<ResourceTags> listbodyTagsAny = new ArrayList<>();
            listbodyTagsAny.add(
                new ResourceTags()
                    .withKey("key1")
                    .withValues(listTagsAnyValues)
            );
            List<String> listTagsValues = new ArrayList<>();
            listTagsValues.add("*value1");
            listTagsValues.add("value2");
            List<ResourceTags> listbodyTags = new ArrayList<>();
            listbodyTags.add(
                new ResourceTags()
                    .withKey("key1")
                    .withValues(listTagsValues)
            );
            body.withMatches(listbodyMatches);
            body.withAction("filter");
            body.withLimit("100");
            body.withOffset("100");
            body.withNotTagsAny(listbodyNotTagsAny);
            body.withNotTags(listbodyNotTags);
            body.withTagsAny(listbodyTagsAny);
            body.withTags(listbodyTags);
            request.withBody(body);
            try {
                ListResourceInstancesResponse response = client.listResourceInstances(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());
            }
        }
    }
    
  • Using tags to query the total number of resources

      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.smn.v2.region.SmnRegion;
    import com.huaweicloud.sdk.smn.v2.*;
    import com.huaweicloud.sdk.smn.v2.model.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class ListResourceInstancesSolution {
    
        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);
    
            SmnClient client = SmnClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(SmnRegion.valueOf("<YOUR REGION>"))
                    .build();
            ListResourceInstancesRequest request = new ListResourceInstancesRequest();
            request.withResourceType("{resource_type}");
            ListInstanceRequestBody body = new ListInstanceRequestBody();
            List<TagMatch> listbodyMatches = new ArrayList<>();
            listbodyMatches.add(
                new TagMatch()
                    .withKey("resource_name")
                    .withValue("resouurce")
            );
            List<String> listNotTagsAnyValues = new ArrayList<>();
            listNotTagsAnyValues.add("value1");
            listNotTagsAnyValues.add("value2");
            List<ResourceTags> listbodyNotTagsAny = new ArrayList<>();
            listbodyNotTagsAny.add(
                new ResourceTags()
                    .withKey("key1")
                    .withValues(listNotTagsAnyValues)
            );
            List<String> listNotTagsValues = new ArrayList<>();
            listNotTagsValues.add("value1");
            listNotTagsValues.add("*value2");
            List<ResourceTags> listbodyNotTags = new ArrayList<>();
            listbodyNotTags.add(
                new ResourceTags()
                    .withKey("key1")
                    .withValues(listNotTagsValues)
            );
            List<String> listTagsAnyValues = new ArrayList<>();
            listTagsAnyValues.add("value1");
            listTagsAnyValues.add("value2");
            List<ResourceTags> listbodyTagsAny = new ArrayList<>();
            listbodyTagsAny.add(
                new ResourceTags()
                    .withKey("key1")
                    .withValues(listTagsAnyValues)
            );
            List<String> listTagsValues = new ArrayList<>();
            listTagsValues.add("value1");
            listTagsValues.add("value2");
            List<ResourceTags> listbodyTags = new ArrayList<>();
            listbodyTags.add(
                new ResourceTags()
                    .withKey("key1")
                    .withValues(listTagsValues)
            );
            body.withMatches(listbodyMatches);
            body.withAction("count");
            body.withNotTagsAny(listbodyNotTagsAny);
            body.withNotTags(listbodyNotTags);
            body.withTagsAny(listbodyTagsAny);
            body.withTags(listbodyTags);
            request.withBody(body);
            try {
                ListResourceInstancesResponse response = client.listResourceInstances(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

  • Using tags to filter resources

     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
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdksmn.v2.region.smn_region import SmnRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdksmn.v2 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 = SmnClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(SmnRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = ListResourceInstancesRequest()
            request.resource_type = "{resource_type}"
            listMatchesbody = [
                TagMatch(
                    key="resource_name",
                    value="resource1"
                )
            ]
            listValuesNotTagsAny = [
                "value1",
                "value2"
            ]
            listNotTagsAnybody = [
                ResourceTags(
                    key="key1",
                    values=listValuesNotTagsAny
                )
            ]
            listValuesNotTags = [
                "*value21",
                "value22"
            ]
            listValuesNotTags1 = [
                "*value1",
                "value2"
            ]
            listNotTagsbody = [
                ResourceTags(
                    key="key1",
                    values=listValuesNotTags1
                ),
                ResourceTags(
                    key="key2",
                    values=listValuesNotTags
                )
            ]
            listValuesTagsAny = [
                "value1",
                "value2"
            ]
            listTagsAnybody = [
                ResourceTags(
                    key="key1",
                    values=listValuesTagsAny
                )
            ]
            listValuesTags = [
                "*value1",
                "value2"
            ]
            listTagsbody = [
                ResourceTags(
                    key="key1",
                    values=listValuesTags
                )
            ]
            request.body = ListInstanceRequestBody(
                matches=listMatchesbody,
                action="filter",
                limit="100",
                offset="100",
                not_tags_any=listNotTagsAnybody,
                not_tags=listNotTagsbody,
                tags_any=listTagsAnybody,
                tags=listTagsbody
            )
            response = client.list_resource_instances(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • Using tags to query the total number of resources

     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 huaweicloudsdksmn.v2.region.smn_region import SmnRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdksmn.v2 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 = SmnClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(SmnRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = ListResourceInstancesRequest()
            request.resource_type = "{resource_type}"
            listMatchesbody = [
                TagMatch(
                    key="resource_name",
                    value="resouurce"
                )
            ]
            listValuesNotTagsAny = [
                "value1",
                "value2"
            ]
            listNotTagsAnybody = [
                ResourceTags(
                    key="key1",
                    values=listValuesNotTagsAny
                )
            ]
            listValuesNotTags = [
                "value1",
                "*value2"
            ]
            listNotTagsbody = [
                ResourceTags(
                    key="key1",
                    values=listValuesNotTags
                )
            ]
            listValuesTagsAny = [
                "value1",
                "value2"
            ]
            listTagsAnybody = [
                ResourceTags(
                    key="key1",
                    values=listValuesTagsAny
                )
            ]
            listValuesTags = [
                "value1",
                "value2"
            ]
            listTagsbody = [
                ResourceTags(
                    key="key1",
                    values=listValuesTags
                )
            ]
            request.body = ListInstanceRequestBody(
                matches=listMatchesbody,
                action="count",
                not_tags_any=listNotTagsAnybody,
                not_tags=listNotTagsbody,
                tags_any=listTagsAnybody,
                tags=listTagsbody
            )
            response = client.list_resource_instances(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

  • Using tags to filter resources

      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
    101
    102
    103
    104
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        smn "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/smn/v2"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/smn/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/smn/v2/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 := smn.NewSmnClient(
            smn.SmnClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.ListResourceInstancesRequest{}
    	request.ResourceType = "{resource_type}"
    	var listMatchesbody = []model.TagMatch{
            {
                Key: "resource_name",
                Value: "resource1",
            },
        }
    	var listValuesNotTagsAny = []string{
            "value1",
    	    "value2",
        }
    	var listNotTagsAnybody = []model.ResourceTags{
            {
                Key: "key1",
                Values: listValuesNotTagsAny,
            },
        }
    	var listValuesNotTags = []string{
            "*value21",
    	    "value22",
        }
    	var listValuesNotTags1 = []string{
            "*value1",
    	    "value2",
        }
    	var listNotTagsbody = []model.ResourceTags{
            {
                Key: "key1",
                Values: listValuesNotTags1,
            },
            {
                Key: "key2",
                Values: listValuesNotTags,
            },
        }
    	var listValuesTagsAny = []string{
            "value1",
    	    "value2",
        }
    	var listTagsAnybody = []model.ResourceTags{
            {
                Key: "key1",
                Values: listValuesTagsAny,
            },
        }
    	var listValuesTags = []string{
            "*value1",
    	    "value2",
        }
    	var listTagsbody = []model.ResourceTags{
            {
                Key: "key1",
                Values: listValuesTags,
            },
        }
    	limitListInstanceRequestBody:= "100"
    	offsetListInstanceRequestBody:= "100"
    	request.Body = &model.ListInstanceRequestBody{
    		Matches: &listMatchesbody,
    		Action: "filter",
    		Limit: &limitListInstanceRequestBody,
    		Offset: &offsetListInstanceRequestBody,
    		NotTagsAny: &listNotTagsAnybody,
    		NotTags: &listNotTagsbody,
    		TagsAny: &listTagsAnybody,
    		Tags: &listTagsbody,
    	}
    	response, err := client.ListResourceInstances(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • Using tags to query the total number of resources

     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
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        smn "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/smn/v2"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/smn/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/smn/v2/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 := smn.NewSmnClient(
            smn.SmnClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.ListResourceInstancesRequest{}
    	request.ResourceType = "{resource_type}"
    	var listMatchesbody = []model.TagMatch{
            {
                Key: "resource_name",
                Value: "resouurce",
            },
        }
    	var listValuesNotTagsAny = []string{
            "value1",
    	    "value2",
        }
    	var listNotTagsAnybody = []model.ResourceTags{
            {
                Key: "key1",
                Values: listValuesNotTagsAny,
            },
        }
    	var listValuesNotTags = []string{
            "value1",
    	    "*value2",
        }
    	var listNotTagsbody = []model.ResourceTags{
            {
                Key: "key1",
                Values: listValuesNotTags,
            },
        }
    	var listValuesTagsAny = []string{
            "value1",
    	    "value2",
        }
    	var listTagsAnybody = []model.ResourceTags{
            {
                Key: "key1",
                Values: listValuesTagsAny,
            },
        }
    	var listValuesTags = []string{
            "value1",
    	    "value2",
        }
    	var listTagsbody = []model.ResourceTags{
            {
                Key: "key1",
                Values: listValuesTags,
            },
        }
    	request.Body = &model.ListInstanceRequestBody{
    		Matches: &listMatchesbody,
    		Action: "count",
    		NotTagsAny: &listNotTagsAnybody,
    		NotTags: &listNotTagsbody,
    		TagsAny: &listTagsAnybody,
    		Tags: &listTagsbody,
    	}
    	response, err := client.ListResourceInstances(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    

More

For SDK sample code of more programming languages, see the Sample Code tab in API Explorer. SDK sample code can be automatically generated.

Status Codes

Status Code

Description

200

OK

400

Bad Request

403

Unauthorized

404

Not Found

500

Internal Server Error

Error Codes

See Error Codes.