使用ActionTrail Python SDK

weixin_33842304發表於2018-04-09

ActionTrail提供官方的Python SDK。本文將簡單介紹一下如何使用ActionTrail的Python SDK。

安裝Aliyun Core SDK。

pip install aliyun-python-sdk-core

安裝ActionTrail Python SDK。

pip install aliyun-python-sdk-actiontrail

下面是測試的程式碼。呼叫LookupEventsRequest獲取所有CreateRole事件。

#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore import client
from aliyunsdkactiontrail.request.v20171204 import LookupEventsRequest

clt = client.AcsClient('您的AK id', '您的AK secret', 'cn-hangzhou')

request = LookupEventsRequest.LookupEventsRequest()
request.set_EventName('CreateRole')

response = clt.do_action_with_exception(request)

print response

執行程式碼。

$ python actiontrail_test.py | python -m json.tool
{
    "EndTime": "2018-04-09T00:47:11Z",
    "Events": [
        {
            "acsRegion": "cn-hangzhou",
            "apiVersion": "2015-05-01",
            "eventId": "9554F0A2-DA47-4390-A21C-F3D56CF6CCC0",
            "eventName": "CreateRole",
            "eventSource": "ram-share.aliyuncs.com",
            "eventTime": "2018-04-08T17:27:29Z",
            "eventType": "ApiCall",
            "eventVersion": "1",
            "isGlobal": true,
            "referencedResources": {
                "Role": [
                    "AliyunStreamDefaultRole"
                ]
            },
            "requestId": "9554F0A2-DA47-4390-A21C-F3D56CF6CCC0",
            "requestParameters": {
                "AssumeRolePolicyDocument": "\"{\\\\\\\"Statement\\\\\\\":[{\\\\\\\"Action\\\\\\\":\\\\\\\"sts:AssumeRole\\\\\\\",\\\\\\\"Effect\\\\\\\":\\\\\\\"Allow\\\\\\\",\\\\\\\"Principal\\\\\\\":{\\\\\\\"Service\\\\\\\":[\\\\\\\"stream.aliyuncs.com\\\\\\\"]}}],\\\\\\\"Version\\\\\\\":\\\\\\\"1\\\\\\\"}\"",
                "Description": "\"\u6d41\u8ba1\u7b97\u670d\u52a1\u9ed8\u8ba4\u4f7f\u7528\u6b64\u89d2\u8272\u6765\u8bbf\u95ee\u60a8\u5728\u5176\u4ed6\u4e91\u4ea7\u54c1\u4e2d\u7684\u8d44\u6e90\"",
                "HostId": "\"ram-share.aliyuncs.com\"",
                "RequestId": "\"9554F0A2-DA47-4390-A21C-F3D56CF6CCC0\"",
                "RoleName": "\"AliyunStreamDefaultRole\"",
                "SecureTransport": "\"true\"",
                "needarrayitemname": "\"true\""
            },
            "serviceName": "Ram",
            "sourceIpAddress": "121.0.29.156",
            "userIdentity": {
                "accountId": "1532770894211314",
                "principalId": "1532770894211314",
                "sessionContext": {
                    "attributes": {
                        "creationDate": "2018-04-08T17:27:29Z",
                        "mfaAuthenticated": "false"
                    }
                },
                "type": "root-account",
                "userName": "root"
            }
        }
}

相關文章