List resource tags
You can use the Simple Log Service SDK for Python to call the ListTagResources operation and query the tags that are bound to resources.
Prerequisites
-
Simple Log Service is activated. For more information, see Activate Simple Log Service.
-
The Simple Log Service SDK for Python is initialized. For more information, see Initialize Simple Log Service SDK for Python.
Parameter description
def list_tag_resources(self, resource_type, resource_id, **filer_tags):
Request parameters
|
Parameter |
Type |
Required |
Description |
Example |
|
resource_type |
String |
Yes |
The resource type. Valid values:
|
project |
|
resource_id |
String |
No |
The resource ID. If resource_type is The format of resource_id varies based on the value of resource_type:
Important
Either resource_id or filer_tags must be configured. |
ali-test-project |
|
filer_tags |
Dictionary |
No |
The key-value pairs used to filter resources. You can specify up to 20 key-value pairs. Important
Either resource_id or filer_tags must be configured. |
{'key1': 'value1'} |
Response parameters
For details about the response parameters, see ListTagResources.
Sample code
from aliyun.log import LogClient
import os
# Specifiy a Simple Log Service endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"# In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# Initialize a client for Simple Log Service.
client = LogClient(endpoint, accessKeyId, accessKey)
def get_resource_tags():
all_tags = set()
for response in client.list_tag_resources(resource_type, resource_id, **filer_tags):
all_tags.update(response.get_tags())
return all_tags
if __name__ == '__main__':
project_name = "proj-test"
logstore_name = "logstore-test"# Get the tags of the project.
resource_type = 'project'
resource_id = ''
filer_tags = {'proj1': 'v1'}
tags = get_resource_tags()
for tag in tags:
print(f"resource_id: {tag.get_resource_id()}, Tag Key: {tag.get_tag_key()}, Tag Value: {tag.get_tag_value()}")
# Get the tags of the logstore# resource_type = 'logstore'# resource_id = project_name + '#' + logstore_name# filer_tags = {}# tags = get_resource_tags()# for tag in tags:# print(f"resource_id: {tag.get_resource_id()}, Tag Key: {tag.get_tag_key()}, Tag Value: {tag.get_tag_value()}")
Sample response
ali-test-project key1 : value1
ali-test-project key2 : value2
ali-test-project#test key2 : value2
ali-test-project#test key1 : value1
Process finished with exit code 0
References
-
For more information about tag management APIs, see the following topics:
-
For more sample code, see Alibaba Cloud Simple Log Service SDK for Python on GitHub.