更新时间:2024-05-21 GMT+08:00
分享

创建关键操作通知

功能介绍

配置关键操作通知,可在发生特定操作时,使用预先创建好的SMN主题,向用户手机、邮箱发送消息,也可直接发送http/https消息。常用于实时感知高危操作、触发特定操作或对接用户自有审计分析系统。

调用方法

请参见如何调用API

URI

POST /v3/{project_id}/notifications

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

项目ID,参见获取账号ID和项目ID章节。

请求参数

表2 请求Body参数

参数

是否必选

参数类型

描述

notification_name

String

标识关键操作名称。

operation_type

String

标识操作类型。 目前支持的操作类型有完整类型(complete)和自定义类型(customized)。 完整类型下,CTS发送通知的对象为已对接服务的所有事件,此时不用指定operations和notify_user_list字段。 自定义类型下,CTS发送通知的对象是在operations列表中指定的事件。

枚举值:

  • complete

  • customized

operations

Array of Operations objects

操作事件列表。

notify_user_list

Array of NotificationUsers objects

通知用户列表,目前最多支持对10个用户组和50个用户发起的操作进行配置。

topic_id

String

消息通知服务的topic_urn或者函数工作流的func_urn。- 消息通知服务的topic_urn可以通过消息通知服务的查询主题列表API获取,示例:urn:smn:regionId:f96188c7ccaf4ffba0c9aa149ab2bd57:test_topic_v2。- 函数工作流的func_urn可以通过函数工作流的获取函数列表API获取,示例:urn:fss:xxxxxxxxx:7aad83af3e8d42e99ac194e8419e2c9b:function:default:test。

filter

Filter object

关键操作通知高级过滤条件。

agency_name

String

云服务委托名称。 参数值为"cts_admin_trust"时,创建关键操作通知时会自动创建云服务委托:cts_admin_trust。

枚举值:

  • cts_admin_trust

表3 Operations

参数

是否必选

参数类型

描述

service_type

String

标识云服务类型。必须为已对接CTS的云服务的英文缩写,且服务类型一般为大写字母。 已对接的云服务列表参见《云审计服务用户指南》“支持审计的服务及详细操作列表”章节,单击对应云服务的文档链接,可以查看到该云服务的英文缩写。

resource_type

String

标识资源类型。

trace_names

Array of strings

标识事件名称。

表4 NotificationUsers

参数

是否必选

参数类型

描述

user_group

String

IAM用户组。

user_list

Array of strings

IAM用户。

表5 Filter

参数

是否必选

参数类型

描述

condition

String

多条件关系。

  • AND(默认值) 表示所有过滤条件满足后生效。

  • OR 表示有任意一个条件满足时生效。

枚举值:

  • AND(默认值)

  • OR

is_support_filter

Boolean

是否打开高级筛选开关。

rule

Array of strings

高级过滤条件规则,示例如下:"key != value",格式为:字段 规则 值。-字段取值范围:api_version,code,trace_rating,trace_type,resource_id,resource_name。-规则:!= 或 =。- 值:api_version正则约束:^(a-zA-Z0-9_-.){1,64}$;code:最小长度1,最大长度256;trace_rating枚举值:"normal", "warning", "incident";trace_type枚举值:"ConsoleAction", "ApiCall", "SystemAction";resource_id:最小长度1,最大长度350;resource_name:最小长度1,最大长度256

响应参数

状态码: 201

表6 响应Body参数

参数

参数类型

描述

notification_name

String

通知名称。

operation_type

String

操作类型。和自定义。

  • complete:完整类型,对所有已对接云审计服务的所有操作发送SMN通知。

  • customized:自定义类型,对指定云服务的指定操作发送SMN通知。

枚举值:

  • customized

  • complete

operations

Array of Operations objects

操作事件列表。

notify_user_list

Array of NotificationUsers objects

通知用户列表,目前最多支持对10个用户组和50个用户发起的操作进行配置。

status

String

通知状态。启用和停用。

  • disabled:停用关键操作通知。

  • enabled:启用关键操作通知。

枚举值:

  • enabled

  • disabled

topic_id

String

消息通知服务(SMN)主题的唯一的资源标识,可通过查询主题列表获取该标识。

notification_id

String

通知的唯一标识ID。

notification_type

String

通知类型。 -smn:消息通知服务。 -fun:函数工作流。

枚举值:

  • smn

  • fun

project_id

String

项目ID。

create_time

Long

通知规则创建时间。

filter

Filter object

关键操作通知高级筛选条件。

agency_name

String

云服务委托名称。 参数值为"cts_admin_trust"时,创建关键操作通知时会自动创建云服务委托:cts_admin_trust。

枚举值:

  • cts_admin_trust

表7 Operations

参数

参数类型

描述

service_type

String

标识云服务类型。必须为已对接CTS的云服务的英文缩写,且服务类型一般为大写字母。 已对接的云服务列表参见《云审计服务用户指南》“支持审计的服务及详细操作列表”章节,单击对应云服务的文档链接,可以查看到该云服务的英文缩写。

resource_type

String

标识资源类型。

trace_names

Array of strings

标识事件名称。

表8 NotificationUsers

参数

参数类型

描述

user_group

String

IAM用户组。

user_list

Array of strings

IAM用户。

表9 Filter

参数

参数类型

描述

condition

String

多条件关系。

  • AND(默认值) 表示所有过滤条件满足后生效。

  • OR 表示有任意一个条件满足时生效。

枚举值:

  • AND(默认值)

  • OR

is_support_filter

Boolean

是否打开高级筛选开关。

rule

Array of strings

高级过滤条件规则,示例如下:"key != value",格式为:字段 规则 值。-字段取值范围:api_version,code,trace_rating,trace_type,resource_id,resource_name。-规则:!= 或 =。- 值:api_version正则约束:^(a-zA-Z0-9_-.){1,64}$;code:最小长度1,最大长度256;trace_rating枚举值:"normal", "warning", "incident";trace_type枚举值:"ConsoleAction", "ApiCall", "SystemAction";resource_id:最小长度1,最大长度350;resource_name:最小长度1,最大长度256

状态码: 400

表10 响应Body参数

参数

参数类型

描述

error_code

String

错误码标识,CTS.XXX。

error_msg

String

错误描述。

状态码: 401

表11 响应Body参数

参数

参数类型

描述

error_code

String

错误码标识,CTS.XXX。

error_msg

String

错误描述。

状态码: 403

表12 响应Body参数

参数

参数类型

描述

error_code

String

错误码标识,CTS.XXX。

error_msg

String

错误描述。

状态码: 404

表13 响应Body参数

参数

参数类型

描述

error_code

String

错误码标识,CTS.XXX。

error_msg

String

错误描述。

状态码: 500

表14 响应Body参数

参数

参数类型

描述

error_code

String

错误码标识,CTS.XXX。

error_msg

String

错误描述。

状态码: 503

表15 响应Body参数

参数

参数类型

描述

error_code

String

错误码标识,CTS.XXX。

error_msg

String

错误描述。

请求示例

  • 创建完整类型关键操作通知请求样例。

    POST https://{endpoint}/v3/{project_id}/notifications
    
    {
      "notification_name" : "test",
      "filter" : {
        "is_support_filter" : true,
        "rule" : [ "code != 200", "api_version = v1.0", "trace_rating = normal", "trace_type != ApiCall", "resource_id = xxx", "resource_name = xxx" ],
        "condition" : "OR"
      },
      "operation_type" : "complete",
      "agency_name" : "cts_admin_trust",
      "topic_id" : "urn:smn:{regionid}:24edf66e79d04187acb99a463e610764:test"
    }
  • 创建自定义类型关键操作通知请求样例。

    POST https://{endpoint}/v3/{project_id}/notifications
    
    {
      "notification_name" : "test",
      "operation_type" : "customized",
      "agency_name" : "cts_admin_trust",
      "filter" : {
        "is_support_filter" : true,
        "rule" : [ "code != 200", "api_version = v1.0", "trace_rating = normal", "trace_type != ApiCall", "resource_id = xxx", "resource_name = xxx" ],
        "condition" : "OR"
      },
      "operations" : [ {
        "service_type" : "CTS",
        "resource_type" : "tracker",
        "trace_names" : [ "createTracker", "deleteTracker" ]
      }, {
        "service_type" : "CTS",
        "resource_type" : "notification",
        "trace_names" : [ "deleteNotification", "updateNotification" ]
      }, {
        "service_type" : "AOM",
        "resource_type" : "pe",
        "trace_names" : [ "deletePolicyGroup", "updatePolicyGroup", "createPolicyGroup" ]
      } ],
      "notify_user_list" : [ {
        "user_group" : "admin",
        "user_list" : [ "test1", "test2" ]
      }, {
        "user_group" : "CTS view",
        "user_list" : [ "test3", "test4" ]
      } ],
      "topic_id" : "urn:smn:{regionid}:24edf66e79d04187acb99a463e610764:test"
    }

响应示例

状态码: 201

创建成功。

{
  "create_time" : 1634001495876,
  "notification_id" : "cda8fd83-d08c-46f0-b914-1453a6a85c00",
  "notification_name" : "test",
  "agency_name" : "cts_admin_trust",
  "notification_type" : "smn",
  "notify_user_list" : [ {
    "user_group" : "admin",
    "user_list" : [ "test1", "test2" ]
  }, {
    "user_group" : "CTS view",
    "user_list" : [ "test3", "test4" ]
  } ],
  "operation_type" : "customized",
  "operations" : [ {
    "resource_type" : "tracker",
    "service_type" : "CTS",
    "trace_names" : [ "createTracker", "deleteTracker" ]
  }, {
    "resource_type" : "notification",
    "service_type" : "CTS",
    "trace_names" : [ "deleteNotification", "updateNotification" ]
  }, {
    "resource_type" : "pe",
    "service_type" : "AOM",
    "trace_names" : [ "deletePolicyGroup", "updatePolicyGroup", "createPolicyGroup" ]
  } ],
  "project_id" : "24edf66e79d04187acb99a463e610764",
  "status" : "enabled",
  "topic_id" : "urn:smn:{regionid}:24edf66e79d04187acb99a463e610764:test"
}

SDK代码示例

SDK代码示例如下。

  • 创建完整类型关键操作通知请求样例。

     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
    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.cts.v3.region.CtsRegion;
    import com.huaweicloud.sdk.cts.v3.*;
    import com.huaweicloud.sdk.cts.v3.model.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class CreateNotificationSolution {
    
        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");
    
            ICredential auth = new BasicCredentials()
                    .withAk(ak)
                    .withSk(sk);
    
            CtsClient client = CtsClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(CtsRegion.valueOf("<YOUR REGION>"))
                    .build();
            CreateNotificationRequest request = new CreateNotificationRequest();
            CreateNotificationRequestBody body = new CreateNotificationRequestBody();
            List<String> listFilterRule = new ArrayList<>();
            listFilterRule.add("code != 200");
            listFilterRule.add("api_version = v1.0");
            listFilterRule.add("trace_rating = normal");
            listFilterRule.add("trace_type != ApiCall");
            listFilterRule.add("resource_id = xxx");
            listFilterRule.add("resource_name = xxx");
            Filter filterbody = new Filter();
            filterbody.withCondition(Filter.ConditionEnum.fromValue("OR"))
                .withIsSupportFilter(true)
                .withRule(listFilterRule);
            body.withFilter(filterbody);
            body.withTopicId("urn:smn:{regionid}:24edf66e79d04187acb99a463e610764:test");
            body.withOperationType(CreateNotificationRequestBody.OperationTypeEnum.fromValue("complete"));
            body.withNotificationName("test");
            request.withBody(body);
            try {
                CreateNotificationResponse response = client.createNotification(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());
            }
        }
    }
    
  • 创建自定义类型关键操作通知请求样例。

      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
    111
    112
    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.cts.v3.region.CtsRegion;
    import com.huaweicloud.sdk.cts.v3.*;
    import com.huaweicloud.sdk.cts.v3.model.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class CreateNotificationSolution {
    
        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");
    
            ICredential auth = new BasicCredentials()
                    .withAk(ak)
                    .withSk(sk);
    
            CtsClient client = CtsClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(CtsRegion.valueOf("<YOUR REGION>"))
                    .build();
            CreateNotificationRequest request = new CreateNotificationRequest();
            CreateNotificationRequestBody body = new CreateNotificationRequestBody();
            List<String> listFilterRule = new ArrayList<>();
            listFilterRule.add("code != 200");
            listFilterRule.add("api_version = v1.0");
            listFilterRule.add("trace_rating = normal");
            listFilterRule.add("trace_type != ApiCall");
            listFilterRule.add("resource_id = xxx");
            listFilterRule.add("resource_name = xxx");
            Filter filterbody = new Filter();
            filterbody.withCondition(Filter.ConditionEnum.fromValue("OR"))
                .withIsSupportFilter(true)
                .withRule(listFilterRule);
            List<String> listNotifyUserListUserList = new ArrayList<>();
            listNotifyUserListUserList.add("test3");
            listNotifyUserListUserList.add("test4");
            List<String> listNotifyUserListUserList1 = new ArrayList<>();
            listNotifyUserListUserList1.add("test1");
            listNotifyUserListUserList1.add("test2");
            List<NotificationUsers> listbodyNotifyUserList = new ArrayList<>();
            listbodyNotifyUserList.add(
                new NotificationUsers()
                    .withUserGroup("admin")
                    .withUserList(listNotifyUserListUserList1)
            );
            listbodyNotifyUserList.add(
                new NotificationUsers()
                    .withUserGroup("CTS view")
                    .withUserList(listNotifyUserListUserList)
            );
            List<String> listOperationsTraceNames = new ArrayList<>();
            listOperationsTraceNames.add("deletePolicyGroup");
            listOperationsTraceNames.add("updatePolicyGroup");
            listOperationsTraceNames.add("createPolicyGroup");
            List<String> listOperationsTraceNames1 = new ArrayList<>();
            listOperationsTraceNames1.add("deleteNotification");
            listOperationsTraceNames1.add("updateNotification");
            List<String> listOperationsTraceNames2 = new ArrayList<>();
            listOperationsTraceNames2.add("createTracker");
            listOperationsTraceNames2.add("deleteTracker");
            List<Operations> listbodyOperations = new ArrayList<>();
            listbodyOperations.add(
                new Operations()
                    .withServiceType("CTS")
                    .withResourceType("tracker")
                    .withTraceNames(listOperationsTraceNames2)
            );
            listbodyOperations.add(
                new Operations()
                    .withServiceType("CTS")
                    .withResourceType("notification")
                    .withTraceNames(listOperationsTraceNames1)
            );
            listbodyOperations.add(
                new Operations()
                    .withServiceType("AOM")
                    .withResourceType("pe")
                    .withTraceNames(listOperationsTraceNames)
            );
            body.withFilter(filterbody);
            body.withTopicId("urn:smn:{regionid}:24edf66e79d04187acb99a463e610764:test");
            body.withNotifyUserList(listbodyNotifyUserList);
            body.withOperations(listbodyOperations);
            body.withOperationType(CreateNotificationRequestBody.OperationTypeEnum.fromValue("customized"));
            body.withNotificationName("test");
            request.withBody(body);
            try {
                CreateNotificationResponse response = client.createNotification(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());
            }
        }
    }
    
  • 创建完整类型关键操作通知请求样例。

     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
    # coding: utf-8
    
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkcts.v3.region.cts_region import CtsRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkcts.v3 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 = __import__('os').getenv("CLOUD_SDK_AK")
        sk = __import__('os').getenv("CLOUD_SDK_SK")
    
        credentials = BasicCredentials(ak, sk) \
    
        client = CtsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(CtsRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = CreateNotificationRequest()
            listRuleFilter = [
                "code != 200",
                "api_version = v1.0",
                "trace_rating = normal",
                "trace_type != ApiCall",
                "resource_id = xxx",
                "resource_name = xxx"
            ]
            filterbody = Filter(
                condition="OR",
                is_support_filter=True,
                rule=listRuleFilter
            )
            request.body = CreateNotificationRequestBody(
                filter=filterbody,
                topic_id="urn:smn:{regionid}:24edf66e79d04187acb99a463e610764:test",
                operation_type="complete",
                notification_name="test"
            )
            response = client.create_notification(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 创建自定义类型关键操作通知请求样例。

     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
    # coding: utf-8
    
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkcts.v3.region.cts_region import CtsRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkcts.v3 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 = __import__('os').getenv("CLOUD_SDK_AK")
        sk = __import__('os').getenv("CLOUD_SDK_SK")
    
        credentials = BasicCredentials(ak, sk) \
    
        client = CtsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(CtsRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = CreateNotificationRequest()
            listRuleFilter = [
                "code != 200",
                "api_version = v1.0",
                "trace_rating = normal",
                "trace_type != ApiCall",
                "resource_id = xxx",
                "resource_name = xxx"
            ]
            filterbody = Filter(
                condition="OR",
                is_support_filter=True,
                rule=listRuleFilter
            )
            listUserListNotifyUserList = [
                "test3",
                "test4"
            ]
            listUserListNotifyUserList1 = [
                "test1",
                "test2"
            ]
            listNotifyUserListbody = [
                NotificationUsers(
                    user_group="admin",
                    user_list=listUserListNotifyUserList1
                ),
                NotificationUsers(
                    user_group="CTS view",
                    user_list=listUserListNotifyUserList
                )
            ]
            listTraceNamesOperations = [
                "deletePolicyGroup",
                "updatePolicyGroup",
                "createPolicyGroup"
            ]
            listTraceNamesOperations1 = [
                "deleteNotification",
                "updateNotification"
            ]
            listTraceNamesOperations2 = [
                "createTracker",
                "deleteTracker"
            ]
            listOperationsbody = [
                Operations(
                    service_type="CTS",
                    resource_type="tracker",
                    trace_names=listTraceNamesOperations2
                ),
                Operations(
                    service_type="CTS",
                    resource_type="notification",
                    trace_names=listTraceNamesOperations1
                ),
                Operations(
                    service_type="AOM",
                    resource_type="pe",
                    trace_names=listTraceNamesOperations
                )
            ]
            request.body = CreateNotificationRequestBody(
                filter=filterbody,
                topic_id="urn:smn:{regionid}:24edf66e79d04187acb99a463e610764:test",
                notify_user_list=listNotifyUserListbody,
                operations=listOperationsbody,
                operation_type="customized",
                notification_name="test"
            )
            response = client.create_notification(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 创建完整类型关键操作通知请求样例。

     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
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        cts "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cts/v3"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cts/v3/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cts/v3/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")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := cts.NewCtsClient(
            cts.CtsClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.CreateNotificationRequest{}
    	var listRuleFilter = []string{
            "code != 200",
    	    "api_version = v1.0",
    	    "trace_rating = normal",
    	    "trace_type != ApiCall",
    	    "resource_id = xxx",
    	    "resource_name = xxx",
        }
    	filterbody := &model.Filter{
    		Condition: model.GetFilterConditionEnum().OR,
    		IsSupportFilter: true,
    		Rule: listRuleFilter,
    	}
    	topicIdCreateNotificationRequestBody:= "urn:smn:{regionid}:24edf66e79d04187acb99a463e610764:test"
    	request.Body = &model.CreateNotificationRequestBody{
    		Filter: filterbody,
    		TopicId: &topicIdCreateNotificationRequestBody,
    		OperationType: model.GetCreateNotificationRequestBodyOperationTypeEnum().COMPLETE,
    		NotificationName: "test",
    	}
    	response, err := client.CreateNotification(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • 创建自定义类型关键操作通知请求样例。

      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
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        cts "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cts/v3"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cts/v3/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cts/v3/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")
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
    
        client := cts.NewCtsClient(
            cts.CtsClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.CreateNotificationRequest{}
    	var listRuleFilter = []string{
            "code != 200",
    	    "api_version = v1.0",
    	    "trace_rating = normal",
    	    "trace_type != ApiCall",
    	    "resource_id = xxx",
    	    "resource_name = xxx",
        }
    	filterbody := &model.Filter{
    		Condition: model.GetFilterConditionEnum().OR,
    		IsSupportFilter: true,
    		Rule: listRuleFilter,
    	}
    	var listUserListNotifyUserList = []string{
            "test3",
    	    "test4",
        }
    	var listUserListNotifyUserList1 = []string{
            "test1",
    	    "test2",
        }
    	var listNotifyUserListbody = []model.NotificationUsers{
            {
                UserGroup: "admin",
                UserList: listUserListNotifyUserList1,
            },
            {
                UserGroup: "CTS view",
                UserList: listUserListNotifyUserList,
            },
        }
    	var listTraceNamesOperations = []string{
            "deletePolicyGroup",
    	    "updatePolicyGroup",
    	    "createPolicyGroup",
        }
    	var listTraceNamesOperations1 = []string{
            "deleteNotification",
    	    "updateNotification",
        }
    	var listTraceNamesOperations2 = []string{
            "createTracker",
    	    "deleteTracker",
        }
    	var listOperationsbody = []model.Operations{
            {
                ServiceType: "CTS",
                ResourceType: "tracker",
                TraceNames: listTraceNamesOperations2,
            },
            {
                ServiceType: "CTS",
                ResourceType: "notification",
                TraceNames: listTraceNamesOperations1,
            },
            {
                ServiceType: "AOM",
                ResourceType: "pe",
                TraceNames: listTraceNamesOperations,
            },
        }
    	topicIdCreateNotificationRequestBody:= "urn:smn:{regionid}:24edf66e79d04187acb99a463e610764:test"
    	request.Body = &model.CreateNotificationRequestBody{
    		Filter: filterbody,
    		TopicId: &topicIdCreateNotificationRequestBody,
    		NotifyUserList: &listNotifyUserListbody,
    		Operations: &listOperationsbody,
    		OperationType: model.GetCreateNotificationRequestBodyOperationTypeEnum().CUSTOMIZED,
    		NotificationName: "test",
    	}
    	response, err := client.CreateNotification(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

201

创建成功。

400

服务器未能处理请求。

401

请求鉴权校验失败,访问被拒绝。

403

请求权限校验失败,访问被禁止。

404

服务器无法找到被请求的资源或部分关键操作通知删除失败。

500

服务内部异常,请求未完成;或部分追踪器删除失败。

503

被请求的服务无效。建议直接修改该请求,不要重试该请求。

错误码

请参见错误码

分享:

    相关文档

    相关产品