更新时间:2025-08-19 GMT+08:00

创建自定义规则集

功能介绍

新建自定义规则集,支持在创建的时候启用规则

调用方法

请参见如何调用API

URI

POST /v2/ruleset

请求参数

表1 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

参数解释:

用户Token。 通过调用IAM服务获取用户Token接口获取(响应消息头中X-Subject-Token的值)。

约束限制:

不涉及。

取值范围:

字符串长度不少于1,不超过10万。

默认取值:

不涉及。

表2 请求Body参数

参数

是否必选

参数类型

描述

project_id

String

参数解释:

项目的32位uuid,通过查询项目列表接口查询项目列表获取。

约束限制:

不涉及。

取值范围:

32位项目id。

默认取值:

不涉及。

template_name

String

参数解释:

新规则集名称。

约束限制:

不能含有任意中英文,数字,点,连字符-及下划线_以外的特殊字符。

取值范围:

任意中英文,数字,点,连字符-及下划线_。

默认取值:

不涉及。

language

String

参数解释:

规则集语言。

约束限制:

只能选择当前系统上支持的语言。

取值范围:

只能传一种语言。当前支持的语言有:ARKTS,C#,C++,CSS,GO,HTML,JAVA,JAVASCRIPT,KOTLIN,LUA,PHP,PYTHON,RUST,SCALA,SHELL,SQL,TYPESCRIPT。

默认取值:

不涉及。

is_default

String

参数解释:

是否基于已存在的规则集创建新规则集。

约束限制:

只能填0或1。

取值范围:

  • 0,新建空白规则集。

  • 1,基于已存在的规则集创建新规则集。

默认取值:

不涉及。

rule_ids

String

参数解释:

新启用规则ids,启用多条规则时,id用英文逗号相连。可调用获取规则列表接口获取规则id。

约束限制:

规则id为32位uuid,多个id之间用英文逗号分隔。例如:"00034fc32ee045388365d42ca4e07fe1,002c2eda6d6c11edab16fa163e0fa374"。

取值范围:

字符串长度不超过4000。

默认取值:

不涉及。

uncheck_ids

String

参数解释:

新关闭规则id,id用英文逗号相连。可调用获取规则列表接口获取规则id。

约束限制:

规则id为32位uuid,多个id之间用英文逗号分隔。例如:"00034fc32ee045388365d42ca4e07fe1,002c2eda6d6c11edab16fa163e0fa374"。

取值范围:

字符串长度不超过4000。

默认取值:

不涉及。

template_id

String

参数解释:

规则集ID。当is_default参数为1时,该参数需要传参。可调用查询规则集列表获取已存在的规则集id。

约束限制:

32位uuid。例如:"da303d80d2594faca60dbcc2e29c18aa"。

取值范围:

不涉及。

默认取值:

不涉及。

custom_attributes

Array of CustomAttributes objects

自定义规则参数项,支持修改规则阈值。

表3 CustomAttributes

参数

是否必选

参数类型

描述

attribute

String

参数解释:

配置项属性,severity:为规则告警等级。

约束限制:

不涉及。

取值范围:

severity。

默认取值:

不涉及。

rules

Array of CustomAttributesRule objects

规则详细。

表4 CustomAttributesRule

参数

是否必选

参数类型

描述

rule_id

String

参数解释:

规则ID。可调用获取规则列表接口获取。

约束限制:

不涉及。

取值范围:

32位项目id。

默认取值:

不涉及。

value

String

参数解释:

attribute的告警等级,0致命,1严重,2一般,3提示。

约束限制:

不涉及。

取值范围:

  • 0,致命。

  • 1,严重。

  • 2,一般。

  • 3,提示。

默认取值:

不涉及。

rule_config_list

Array of RuleConfig objects

规则阈值详细。

表5 RuleConfig

参数

是否必选

参数类型

描述

id

Integer

参数解释:

规则配置ID。可调用获取规则列表接口获取规则配置ID。

约束限制:

不涉及。

取值范围:

32位uuid。

默认取值:

不涉及。

rule_id

String

参数解释:

工具规则ID,即可配置阈值规则的各种阈值对应的ID,当前无便利手段获取,请联系客服咨询。

约束限制:

不涉及。

取值范围:

32位uuid。

默认取值:

不涉及。

default_value

String

参数解释:

配置参数默认阈值。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

option_value

String

参数解释:

配置参数新阈值。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

option_key

String

参数解释:

当前规则配置项key。可调用获取规则列表接口获取规则配置ID。

约束限制:

需要和规则id相匹配。

取值范围:

不涉及。

默认取值:

不涉及。

option_name

String

参数解释:

当前规则配置项key。可调用获取规则列表接口获取规则配置ID。

约束限制:

需要和规则id相匹配。

取值范围:

不涉及。

默认取值:

不涉及。

template_id

String

参数解释:

规则集id。可调用查询规则集列表获取规则集id。

约束限制:

不涉及。

取值范围:

32位uuid。

默认取值:

不涉及。

description

String

参数解释:

配置参数的描述。可不填。

约束限制:

不涉及。

取值范围:

不涉及。

默认取值:

不涉及。

响应参数

状态码:201

表6 响应Body参数

参数

参数类型

描述

template_id

String

参数解释:

规则集id。

取值范围:

32位uuid。

状态码:400

表7 响应Body参数

参数

参数类型

描述

error_code

String

错误码

error_msg

String

错误描述

状态码:401

表8 响应Body参数

参数

参数类型

描述

error_code

String

错误码

error_msg

String

错误描述

请求示例

创建自定义规则集:规则集对应的项目ID、模板ID根据实际显示,模板名称为test_cpp,语言类型为cpp

POST https://{endpoint}/v2/ruleset

{
  "project_id" : "24b97exxxxxxxxfb912625b14cxxxxxx",
  "template_id" : "da303dxxxxxxxxaca60dbcc2e2xxxxxx",
  "template_name" : "test_cpp",
  "language" : "cpp",
  "is_default" : "1",
  "rule_ids" : "8139,8138",
  "uncheck_ids" : "1101,1102"
}

响应示例

状态码:201

Request succeeded!

{
  "template_id" : "c53417a4804f45eba9c11991131c9e79"
}

状态码:400

Bad Request

{
  "error_code" : "CC.00000000",
  "error_msg" : "网络繁忙,请稍后再试"
}

状态码:401

Unauthorized

{
  "error_code" : "CC.00000003",
  "error_msg" : "认证信息过期"
}

SDK代码示例

SDK代码示例如下。

创建自定义规则集:规则集对应的项目ID、模板ID根据实际显示,模板名称为test_cpp,语言类型为cpp

 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
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.codecheck.v2.region.CodeCheckRegion;
import com.huaweicloud.sdk.codecheck.v2.*;
import com.huaweicloud.sdk.codecheck.v2.model.*;


public class CreateRulesetSolution {

    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);

        CodeCheckClient client = CodeCheckClient.newBuilder()
                .withCredential(auth)
                .withRegion(CodeCheckRegion.valueOf("<YOUR REGION>"))
                .build();
        CreateRulesetRequest request = new CreateRulesetRequest();
        Ruleset body = new Ruleset();
        body.withTemplateId("da303dxxxxxxxxaca60dbcc2e2xxxxxx");
        body.withUncheckIds("1101,1102");
        body.withRuleIds("8139,8138");
        body.withIsDefault("1");
        body.withLanguage("cpp");
        body.withTemplateName("test_cpp");
        body.withProjectId("24b97exxxxxxxxfb912625b14cxxxxxx");
        request.withBody(body);
        try {
            CreateRulesetResponse response = client.createRuleset(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());
        }
    }
}

创建自定义规则集:规则集对应的项目ID、模板ID根据实际显示,模板名称为test_cpp,语言类型为cpp

 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
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcodecheck.v2.region.codecheck_region import CodeCheckRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcodecheck.v2 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 = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]

    credentials = BasicCredentials(ak, sk)

    client = CodeCheckClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(CodeCheckRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = CreateRulesetRequest()
        request.body = Ruleset(
            template_id="da303dxxxxxxxxaca60dbcc2e2xxxxxx",
            uncheck_ids="1101,1102",
            rule_ids="8139,8138",
            is_default="1",
            language="cpp",
            template_name="test_cpp",
            project_id="24b97exxxxxxxxfb912625b14cxxxxxx"
        )
        response = client.create_ruleset(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

创建自定义规则集:规则集对应的项目ID、模板ID根据实际显示,模板名称为test_cpp,语言类型为cpp

 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
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    codecheck "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/codecheck/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/codecheck/v2/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/codecheck/v2/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 := codecheck.NewCodeCheckClient(
        codecheck.CodeCheckClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.CreateRulesetRequest{}
	templateIdRuleset:= "da303dxxxxxxxxaca60dbcc2e2xxxxxx"
	uncheckIdsRuleset:= "1101,1102"
	request.Body = &model.Ruleset{
		TemplateId: &templateIdRuleset,
		UncheckIds: &uncheckIdsRuleset,
		RuleIds: "8139,8138",
		IsDefault: "1",
		Language: "cpp",
		TemplateName: "test_cpp",
		ProjectId: "24b97exxxxxxxxfb912625b14cxxxxxx",
	}
	response, err := client.CreateRuleset(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

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

状态码

状态码

描述

201

Request succeeded!

400

Bad Request

401

Unauthorized

错误码

请参见错误码