
# 新增告警分组规则 - AddGroupRule
#### 功能介绍
该接口用于新增告警分组规则。
#### 调用方法
请参见[如何调用API](https://support.huaweicloud.com/api-aom/aom_04_0057.html)。
#### 授权信息
账号具备所有API的调用权限，如果使用账号下的IAM用户调用当前API，该IAM用户需具备调用API所需的权限。
- 如果使用角色与策略授权，具体权限要求请参见[权限和授权项](https://support.huaweicloud.com/api-aom/aom_04_0031.html)。
- 如果使用身份策略授权，需具备如下身份策略权限。
  
  | 授权项                       | 访问级别  | 资源类型（\*为必须） | 条件键 | 别名 | 依赖的授权项 |
  |:---|:---|:---|:---|:---|:---|
  | aom:routeGroupRule:create | Write | -           | -   | -  | -      |
     
  
 
#### URI
POST /v2/{project_id}/alert/group-rules
表1路径参数 
| 参数         | 是否必选 | 参数类型   | 描述                                                                                                     |
|:---|:---|:---|:---|
| project_id | 是    | String | 项目ID，可以从控制台获取，也可以从调用API处获取。获取方式请参见：[获取项目ID](https://support.huaweicloud.com/api-aom/aom_04_0024.html)。 |
   
#### 请求参数
表2请求Header参数 
| 参数                    | 是否必选 | 参数类型   | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                              |
|:---|:---|:---|:---|
| X-Auth-Token          | 是    | String | 从IAM服务获取的用户Token。获取方式请参见：[获取Token](https://support.huaweicloud.com/api-aom/aom_04_0059.html)。                                                                                                                                                                                                                                                                                                                                                   |
| Content-Type          | 是    | String | 消息体的类型（格式）。指定类型为"application/json"。                                                                                                                                                                                                                                                                                                                                                                                                             |
| Enterprise-Project-Id | 否    | String | 企业项目id。获取方式请参见：[获取企业项目ID](https://support.huaweicloud.com/api-aom/aom_04_0024.html)。 - 新增单个企业项目下告警分组规则，填写企业项目id。   - 不填，新增默认企业项目下告警分组规则。    |
   
表3请求Body参数 
| 参数                   | 是否必选 | 参数类型                                                                       | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
|:---|:---|:---|:---|
| name                 | 是    | String                                                                     | 告警分组规则名称。 只能由大小写字母、数字、下划线组成，且不能以下划线开头和结尾，最多不能超过100个字符。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| desc                 | 否    | String                                                                     | 告警分组规则的描述。最多不能超过1024个字符。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| match_v3             | 否    | [GroupV3Tag] object                     | 告警分组编排详情。（如果创建新版告警分组规则（支持编排能力），该参数必填。否则，该参数为空）                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| detail               | 否    | Array of [GroupRuleDetail] objects | 告警分组规则的分组条件列表。（如果创建新版告警分组规则（支持编排能力），该参数为空。否则，该参数必填） 分组条件最多可设置10个并行条件，每个并行条件下最多可设置10个串行条件，每个并行条件下可设置一个或多个AOM告警通知规则。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| group_by             | 否    | Array of strings                                                           | 通知合并规则。（如果创建新版告警分组规则（支持编排能力），该参数为空。否则，该参数必填） - resource_provider：告警源。   - event_severity：严重度。   - tags：标签，当告警分组条件里有标签条件时才支持。    目前仅支持如下通知规则组合: 1.按告警源 \["resource_provider"\] 2.按告警源+严重度 \["resource_provider","event_severity"\] 3.按告警源+所有标签 \["resource_provider","分组条件里的标签名","event_severity","resource_type"\] |
| group_wait           | 否    | Integer                                                                    | 首次等待时间。（如果创建新版告警分组规则（支持编排能力），该参数为空。否则，该参数必填） 取值范围为0s-10min 单位为秒，分钟需转为秒，当为1min时，需填写60s                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| group_interval       | 否    | Integer                                                                    | 变化等待时间。（如果创建新版告警分组规则（支持编排能力），该参数为空。否则，该参数必填） 取值范围为5s-30min，分钟需转为秒，当为1min时，需填写60s                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| group_repeat_waiting | 否    | Integer                                                                    | 重复等待。（如果创建新版告警分组规则（支持编排能力），该参数为空。否则，该参数必填） 取值范围为0min-15day，分钟需转为秒，当为1min时，需填写60s。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| ai_group_by          | 否    | Array of strings                                                           | 智能通知合并规则。智能合并模式开关功能当前仅在华北-北京四，华南-广州区域开放。系统默认关闭智能合并模式开关，打开开关后，用户可以按照选择的告警属性智能合并告警。 数组为空时，表示不开启智能通知合并。 - event_name : 告警名称。   - alarm_info：告警信息。   - tag：所有标签 仅当分组条件里有标签时生效                                                                                                                                                                                                          |
   
 表4GroupV3Tag 
| 参数    | 是否必选 | 参数类型                                                                             | 描述                                                            |
|:---|:---|:---|:---|
| nodes | 是    | Array of [GroupConditionNode] objects | 节点详情列表。 必须包含开始节点，结束节点，card节点。 |
| edges | 是    | Array of [Edge] objects                             | 边详情列表。                                                        |
   
 表5GroupConditionNode 
| 参数           | 是否必选 | 参数类型                                                                         | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
|:---|:---|:---|:---|
| id           | 是    | String                                                                       | 节点id。命名规范为：flow-"8位随机字符串"。 随机字符串只能由字母、数字、下划线和中划线组成。 例如：flow-lvzgw9Td                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| type         | 否    | String                                                                       | 节点类型。 - Start：开始节点。   - Card：卡片节点。   - End：结束节点。                                                                                                                          |
| businessType | 否    | String                                                                       | **参数解释：** 业务类型。 **约束限制：** 当nodeType为Card时，该参数必填。 **取值范围：** - GroupCondition：分组条件节点   - CombineConf：通知合并节点    **默认取值：** 不涉及。 |
| value        | 否    | [GroupConditionSubNode] object | 告警分组规则节点详情。（当nodeType为Card时，该参数必填）                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
   
 表6GroupConditionSubNode 
| 参数            | 是否必选 | 参数类型                                                                                   | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
|:---|:---|:---|:---|
| nodes         | 否    | Array of [GroupConditionSubNode] objects | 子节点列表。（当businessType为GroupCondition时，该参数必填，当businessType为CombineConf，该参数为空）                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| id            | 是    | String                                                                                 | 节点id。命名规范：{businessType}-"8位随机字符串"。 随机字符串只能由字母、数字、下划线和中划线组成 例如：GroupCondition-Lh0dTxyd                                                                                                                                                                                                                                                                                                                                                                                        |
| type          | 否    | String                                                                                 | 节点类型。（当businessType为GroupCondition时，该参数必填，当businessType为CombineConf，该参数为空） - bool：布尔。   - text：文本。   - numerical：数字。    |
| value         | 否    | String                                                                                 | 节点之间关系。（当businessType为GroupCondition时，该参数必填，当businessType为CombineConf，该参数为空） - and：且。   - or：或。                                                                                                                                           |
| match         | 否    | [MatchTag] object                                     | 匹配条件列表。（当businessType为GroupCondition时，该参数必填，当businessType为CombineConf，该参数为空）                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| combineConfig | 否    | [CombineConfig] object                           | 通知合并节点详情。（当businessType为GroupCondition时，该参数为空，当businessType为CombineConf，该参数为必填）                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
   
 表7MatchTag 
| 参数      | 是否必选 | 参数类型             | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
|:---|:---|:---|:---|
| key     | 是    | String           | 分组条件key取值范围： - 具体标签名：标签只能包含中文、字母、数字和下划线   - event_severity：告警级别   - notification_scene：通知场景   - resource_provider：告警源   - resource_type：资源类型                                                                                                                                                                                                                                                          |
| value   | 是    | Array of strings | 分组条件value取值范围。当表达式为正则时，value的长度不能超过256 - 当分组条件key为标签时，value取值范围为：标签名对应的标签值，标签值只能包含中文、字母、数字和下划线。   - 当分组条件key为告警级别且表达式为等于时，value取值范围为：Critical，Major，Minor，Info。   - 当分组条件key为通知场景时，value取值范围为：notify_resolved：告警恢复时，notify_triggered：告警触发时   - 当分组条件key为告警源时，value取值范围为：触发告警或事件的服务名称。可以设置为AOM 、LTS、CCE等服务名称。   - 当分组条件key为资源类型时，value取值范围为：创建告警规则或自定义上报告警时选定的资源类型。可以设置为：host、container、process等类型。    |
| operate | 是    | String           | 操作符： - EQUALS ：等于   - REGEX ：正则   - EXIST ：存在                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
   
 表8CombineConfig 
| 参数                         | 是否必选 | 参数类型             | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
|:---|:---|:---|:---|
| group_by                   | 否    | Array of strings | 通知合并规则。 - resource_provider：告警源。   - event_severity：严重度。   - tag：标签，当告警分组条件里有标签条件时才支持。    目前仅支持如下通知规则组合: 1.按告警源 \["resource_provider"\] 2.按告警源+严重度 \["resource_provider","event_severity"\] 3.按告警源+所有标签 \["resource_provider","tag","event_severity","resource_type"\] |
| ai_group_by                | 否    | Array of strings | 智能通知合并规则。 数组为空时，表示不开启智能通知合并。 - event_name : 告警名称。   - alarm_info：告警信息。   - tag：所有标签，仅当分组条件里有标签时生效。                                                                                                                                                                                                                                                                     |
| group_wait                 | 是    | Integer          | 首次等待时间，取值范围为0s-10min 单位为秒，分钟需转为秒，当为1min时，需填写60s。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| group_interval             | 是    | Integer          | 变化等待时间，取值范围为5s-30min，分钟需转为秒，当为1min时，需填写60s。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| group_repeat_waiting       | 是    | Integer          | 重复等待，取值范围为0min-15day，分钟需转为秒，当为1min时，需填写60s。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| bind_notification_rule_ids | 是    | Array of strings | 绑定告警行动规则名称列表。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
   
 表9Edge 
| 参数     | 是否必选 | 参数类型   | 描述                                                                                                                                  |
|:---|:---|:---|:---|
| id     | 是    | String | 边id。命名规范为：edge-"8位随机字符串"。 随机字符串只能由字母、数字、下划线和中划线组成。 例如：edge-M3pfK202 |
| source | 是    | String | 边起始节点id。取值于nodes列表里的id。                                                                                                             |
| target | 是    | String | 边终止节点id。取值于nodes列表里的id。                                                                                                             |
| value  | 否    | String | 边value。节点之间的关系true或false。                                                                                                           |
   
 表10GroupRuleDetail 
| 参数                         | 是否必选 | 参数类型                                                         | 描述           |
|:---|:---|:---|:---|
| match                      | 是    | Array of [MatchTag] objects | 匹配条件列表。      |
| bind_notification_rule_ids | 是    | Array of strings                                             | 绑定的告警通知规则名称。 |
   
#### 响应参数
**状态码：204**
No Content 表示请求成功，但没有内容返回。
**状态码：400**
表11响应Body参数 
| 参数         | 参数类型   | 描述                                                                                                                                |
|:---|:---|:---|
| error_code | String | **参数解释：** 非法请求错误码。 **取值范围：** 不涉及。 |
| error_msg  | String | **参数解释：** 错误信息。 **取值范围：** 不涉及。    |
| error_type | String | **参数解释：** 错误类型。 **取值范围：** 不涉及。    |
| trace_id   | String | **参数解释：** 请求id。 **取值范围：** 不涉及。    |
   
**状态码：403**
表12响应Body参数 
| 参数         | 参数类型   | 描述                                                                                                                                |
|:---|:---|:---|
| error_code | String | **参数解释：** 非法请求错误码。 **取值范围：** 不涉及。 |
| error_msg  | String | **参数解释：** 错误信息。 **取值范围：** 不涉及。    |
| error_type | String | **参数解释：** 错误类型。 **取值范围：** 不涉及。    |
| trace_id   | String | **参数解释：** 请求id。 **取值范围：** 不涉及。    |
   
#### 请求示例
- 新增新版告警分组规则
  ```
  https://{Endpoint}/v2/{project_id}/alert/group-rules
  {
    "match_v3" : {
      "edges" : [ {
        "id" : "edge-gMPwWev2",
        "source" : "flow-lKSGbvq-",
        "target" : "flow-1JBeHxLU"
      }, {
        "id" : "edge-CkdHb85Z",
        "source" : "flow-1JBeHxLU",
        "target" : "flow-y7b5HNsE",
        "value" : "true"
      }, {
        "id" : "edge-BAurs1Qu",
        "source" : "flow-1JBeHxLU",
        "target" : "flow-_p6cFvLk",
        "value" : "false"
      }, {
        "id" : "edge-wsVSHYce",
        "source" : "flow-_p6cFvLk",
        "target" : "flow-xc0QfFsN",
        "value" : "true"
      }, {
        "id" : "edge-qOQTDuu9",
        "source" : "flow-_p6cFvLk",
        "target" : "flow-lvZgW9Td",
        "value" : "false"
      }, {
        "id" : "edge-Lh_ODRAX",
        "source" : "flow-lvZgW9Td",
        "target" : "flow-oUm2Cw50",
        "value" : "true"
      }, {
        "id" : "edge-SPhMFJXM",
        "source" : "flow-lvZgW9Td",
        "target" : "flow-AlJfG3vo",
        "value" : "false"
      }, {
        "id" : "edge-V50dBRsx",
        "source" : "flow-AlJfG3vo",
        "target" : "flow-cFpXzlxZ"
      } ],
      "nodes" : [ {
        "id" : "flow-lKSGbvq-",
        "type" : "Start"
      }, {
        "businessType" : "GroupCondition",
        "id" : "flow-1JBeHxLU",
        "type" : "Card",
        "value" : {
          "id" : "GroupCondition-Ive7unRq",
          "nodes" : [ {
            "id" : "GroupCondition-PsYsjYeE",
            "match" : {
              "key" : "event_severity",
              "operate" : "EQUALS",
              "value" : [ "Critical" ]
            },
            "type" : "text"
          }, {
            "id" : "GroupCondition-BG9Q_fFo",
            "nodes" : [ {
              "id" : "GroupCondition-Tb6XLa3x",
              "match" : {
                "key" : "event_severity",
                "operate" : "EQUALS",
                "value" : [ "Critical" ]
              },
              "type" : "text"
            } ],
            "type" : "bool",
            "value" : "and"
          }, {
            "id" : "GroupCondition-uLkAf9Ze",
            "match" : {
              "key" : "event_severity",
              "operate" : "EQUALS",
              "value" : [ "Critical" ]
            },
            "type" : "text"
          }, {
            "id" : "GroupCondition-_rgpgeeu",
            "nodes" : [ {
              "id" : "GroupCondition-lCOy7A4X",
              "match" : {
                "key" : "event_severity",
                "operate" : "EQUALS",
                "value" : [ "Critical" ]
              },
              "type" : "text"
            } ],
            "type" : "bool",
            "value" : "or"
          }, {
            "id" : "GroupCondition-1xrYkISG",
            "match" : {
              "key" : "resource_provider",
              "operate" : "EQUALS",
              "value" : [ "AOM" ]
            },
            "type" : "text"
          }, {
            "id" : "GroupCondition-7aQDn88G",
            "match" : {
              "key" : "1",
              "operate" : "EQUALS",
              "value" : [ "1" ]
            },
            "type" : "text"
          } ],
          "type" : "bool",
          "value" : "and"
        }
      }, {
        "businessType" : "CombineConf",
        "id" : "flow-y7b5HNsE",
        "type" : "Card",
        "value" : {
          "combineConfig" : {
            "ai_group_by" : [ "event_name", "alarm_info", "tag" ],
            "bind_notification_rule_ids" : [ "aaa" ],
            "group_interval" : 5,
            "group_repeat_waiting" : 0,
            "group_wait" : 0
          },
          "id" : "CombineConf-v1ga6Xoq"
        }
      }, {
        "businessType" : "GroupCondition",
        "id" : "flow-_p6cFvLk",
        "type" : "Card",
        "value" : {
          "id" : "GroupCondition-Jp6ukhnf",
          "nodes" : [ {
            "id" : "GroupCondition-o2wukru4",
            "match" : {
              "key" : "event_severity",
              "operate" : "EQUALS",
              "value" : [ "Critical" ]
            },
            "type" : "text"
          } ],
          "type" : "bool",
          "value" : "and"
        }
      }, {
        "businessType" : "CombineConf",
        "id" : "flow-xc0QfFsN",
        "type" : "Card",
        "value" : {
          "combineConfig" : {
            "bind_notification_rule_ids" : [ "LTQTEST-ASASDASDDSA" ],
            "group_by" : [ "resource_provider" ],
            "group_interval" : 5,
            "group_repeat_waiting" : 0,
            "group_wait" : 0
          },
          "id" : "CombineConf-zlsIhUa1"
        }
      }, {
        "businessType" : "GroupCondition",
        "id" : "flow-lvZgW9Td",
        "type" : "Card",
        "value" : {
          "id" : "GroupCondition-c-HEgnga",
          "nodes" : [ {
            "id" : "GroupCondition-6PXrURL7",
            "match" : {
              "key" : "event_severity",
              "operate" : "EQUALS",
              "value" : [ "Critical" ]
            },
            "type" : "text"
          }, {
            "id" : "GroupCondition-7i3Zw2ad",
            "match" : {
              "key" : "1",
              "operate" : "EQUALS",
              "value" : [ "1" ]
            },
            "type" : "text"
          }, {
            "id" : "GroupCondition-cQz1fQu2",
            "match" : {
              "key" : "resource_provider",
              "operate" : "EQUALS",
              "value" : [ "AOM" ]
            },
            "type" : "text"
          } ],
          "type" : "bool",
          "value" : "and"
        }
      }, {
        "businessType" : "CombineConf",
        "id" : "flow-oUm2Cw50",
        "type" : "Card",
        "value" : {
          "combineConfig" : {
            "bind_notification_rule_ids" : [ "LTQTEST-ASASDASDDSA" ],
            "group_by" : [ "resource_provider" ],
            "group_interval" : 5,
            "group_repeat_waiting" : 0,
            "group_wait" : 0
          },
          "id" : "CombineConf-exvbZXma"
        }
      }, {
        "businessType" : "CombineConf",
        "id" : "flow-AlJfG3vo",
        "type" : "Card",
        "value" : {
          "combineConfig" : {
            "bind_notification_rule_ids" : [ "aaa" ],
            "group_by" : [ "resource_provider" ],
            "group_interval" : 5,
            "group_repeat_waiting" : 0,
            "group_wait" : 0
          },
          "id" : "CombineConf-oQJVcE1q"
        }
      }, {
        "id" : "flow-cFpXzlxZ",
        "type" : "End"
      } ]
    },
    "name" : "aomtest"
  }
  ```
  
- 新增告警分组规则（开启智能合并模式开关）
  ```
  https://{Endpoint}/v2/{project_id}/alert/group-rules
  {
    "ai_group_by" : [ "event_name" ],
    "desc" : "",
    "detail" : [ {
      "bind_notification_rule_ids" : [ "testyq2" ],
      "match" : [ {
        "key" : "event_severity",
        "operate" : "EQUALS",
        "value" : [ "Critical" ]
      } ]
    } ],
    "group_by" : [ "resource_provider" ],
    "group_interval" : 5,
    "group_repeat_waiting" : 60,
    "group_wait" : 0,
    "name" : "aomtest"
  }
  ```
  
- 新增告警分组规则（关闭智能合并模式开关）
  ```
  https://{Endpoint}/v2/{project_id}/alert/group-rules
  {
    "ai_group_by" : [ ],
    "desc" : "",
    "detail" : [ {
      "bind_notification_rule_ids" : [ "testyq2" ],
      "match" : [ {
        "key" : "event_severity",
        "operate" : "EQUALS",
        "value" : [ "Critical" ]
      } ]
    } ],
    "group_by" : [ "resource_provider" ],
    "group_interval" : 5,
    "group_repeat_waiting" : 60,
    "group_wait" : 0,
    "name" : "aomtest"
  }
  ```
  
 
#### 响应示例
**状态码：400**
BadRequest 非法请求。建议直接修改该请求，不要重试该请求。
```
{
  "error_code" : "AOM.08001001",
  "error_msg" : "grouping name is invalid!",
  "error_type" : "BAD_REQUEST",
  "trace_id" : ""
}
```
**状态码：403**
Forbidden 请求被拒绝访问。返回该状态码，表明请求能够到达服务端，且服务端能够理解用户请求，但是拒绝做更多的事情，因为该请求被设置为拒绝访问，建议直接修改该请求，不要重试该请求。
```
{
  "error_code" : "AOM.0403",
  "error_msg" : "auth failed.",
  "error_type" : "AUTH_FAILED",
  "trace_id" : ""
}
```
#### 状态码
| 状态码 | 描述                                                                                            |
|:---|:---|
| 204 | No Content 表示请求成功，但没有内容返回。                                                                    |
| 400 | BadRequest 非法请求。建议直接修改该请求，不要重试该请求。                                                            |
| 403 | Forbidden 请求被拒绝访问。返回该状态码，表明请求能够到达服务端，且服务端能够理解用户请求，但是拒绝做更多的事情，因为该请求被设置为拒绝访问，建议直接修改该请求，不要重试该请求。 |
   
#### 错误码
请参见[错误码](https://support.huaweicloud.com/api-aom/ErrorCode.html)。
