更新时间:2024-03-15 GMT+08:00

创建自定义规则集

功能介绍

可根据需求灵活的组合规则。

调用方法

请参见如何调用API

URI

POST /v2/ruleset

请求参数

表1 请求Header参数

参数

是否必选

参数类型

描述

Content-Type

String

设置媒体类型和编码格式

X-Auth-Token

String

用户Token

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

最小长度:1

最大长度:100000

表2 请求Body参数

参数

是否必选

参数类型

描述

project_id

String

项目ID

最小长度:1

最大长度:50

template_name

String

新规则集名称

最小长度:1

最大长度:500

language

String

规则集语言

最小长度:1

最大长度:128

is_default

String

如果有基于的规则集则是1,没有基于的规则集则是0

最小长度:1

最大长度:1

rule_ids

String

新启用规则ids

最小长度:1

最大长度:4000

uncheck_ids

String

新关闭规则id

最小长度:1

最大长度:4000

template_id

String

规则集ID

最小长度:1

最大长度:50

custom_attributes

Array of CustomAttributes objects

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

表3 CustomAttributes

参数

是否必选

参数类型

描述

attribute

String

配置项属性,severity:为问题级别

rules

Array of CustomAttributesRule objects

规则详细

表4 CustomAttributesRule

参数

是否必选

参数类型

描述

rule_id

String

规则ID

value

String

attribute的问题级别,0致命,1严重,2一般,3提示

rule_config_list

Array of RuleConfig objects

规则阈值详细

表5 RuleConfig

参数

是否必选

参数类型

描述

id

Integer

规则配置ID

rule_id

Integer

规则ID

default_value

String

默认值

option_value

String

当前

option_key

String

当前规则配置项key

option_name

String

当前规则配置项名称

template_id

String

规则集id

description

String

描述

响应参数

状态码: 201

表6 响应Body参数

参数

参数类型

描述

template_id

String

规则集ID

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

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 = __import__('os').getenv("CLOUD_SDK_AK")
    sk = __import__('os').getenv("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

错误码

请参见错误码