Use the TagResources operation to attach tags to SLS resources such as projects, Logstores, and dashboards.
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.
Parameters
def tag_resources(self, resource_type, resource_id, **tags):
Request parameters
|
Parameter |
Type |
Required |
Description |
Example |
|
resource_type |
String |
Yes |
Resource type. Valid values:
|
project |
|
resource_id |
String |
Yes |
Resource ID. If resource_type is resource_id format by resource_type:
|
ali-test-project |
|
tags |
Dictionary |
Yes |
Tag key-value pairs. Maximum 20 pairs per request. |
{'key1': 'value1', 'key2': 'value2'} |
Response parameters
For information about the response parameters, see TagResources.
Sample code
import os
from aliyun.log import LogClient
def tag_project(client, project):
tags = {'key1': 'value1', 'key2': 'value2'}
client.tag_resources('project', project, **tags)
def tag_logstore(client, project, logstore):
tags = {'key1': 'value1', 'key2': 'value2'}
client.tag_resources('logstore', project + '#' + logstore, **tags)
if __name__ == '__main__':
# Specify 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', '')
project_name = "ali-test-project"
logstore_name = "test-logstore"
client = LogClient(endpoint, accessKeyId, accessKey, "")
tag_project(client, project_name)
tag_logstore(client, project_name, logstore_name)
References
-
For more information about the APIs for managing tags, see the following topics:
-
More sample code is available in the Alibaba Cloud Simple Log Service SDK for Python GitHub repository.