Help Center/ Config/ SDK Reference/ Using the SDK to Create Rules
Updated on 2025-03-26 GMT+08:00

Using the SDK to Create Rules

Overview

This example shows how to use the Java SDK to create and query Config rules.

When you create a rule, you need to specify a resource scope that the rule applies to.

Prerequisites

  1. You have obtained the Huawei Cloud SDK and installed the Java SDK.
  2. You have a Huawei Cloud account and an access key ID (AK) and a secret access key. You can view or create an AK/SK pair in My Credentials > Access Keys on the Huawei Cloud console. For details, see Access Keys.
  3. Config SDK supports Java JDK 1.8 or later.

Installing the SDK

You can obtain and install the SDK using Maven. To use Maven, add dependencies to the pom.xml file. For details about SDK versions, see SDK Center.

<dependency>
    <groupId>com.huaweicloud.sdk</groupId>
    <artifactId>huaweicloud-sdk-config</artifactId>
    <version>{sdk-version}</version>
</dependency>

Example Code

public class CreatePolicyAssignmentDemo {
    public static void main(String[] args) {
        // There will be security risks if the AK and SK used for authentication is written into code. Encrypt the AK/SK and store them into the configuration file or environment variables.
        // In this example, the AK and SK are stored in environment variables. Before running this example, set environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK.
        String ak = System.getenv("HUAWEICLOUD_SDK_AK");
        String sk = System.getenv("HUAWEICLOUD_SDK_SK");
        String regionId = "<region id>";
        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);

        ICredential auth = new GlobalCredentials().withAk(ak).withSk(sk);
        ConfigClient client = ConfigClient.newBuilder().withHttpConfig(config).withCredential(auth)
            .withRegion(ConfigRegion.valueOf(regionId)).build();

        CreatePolicyAssignmentsRequest createRequest = new CreatePolicyAssignmentsRequest()
            .withBody(new PolicyAssignmentRequestBody().withPolicyAssignmentType(PolicyAssignmentRequestBody.PolicyAssignmentTypeEnum.BUILTIN)
                .withName("<Your policy_assignment_name>").withDescription("<Your policy_assignment_description>")
                .withPolicyFilter(new PolicyFilterDefinition()).withPolicyDefinitionId("<Your policy_definition_id>"));

        try {
            CreatePolicyAssignmentsResponse createResponse = client.createPolicyAssignments(createRequest);
            System.out.println(createResponse.toString());
            ShowPolicyAssignmentRequest showPolicyAssignmentRequest = new ShowPolicyAssignmentRequest()
                .withPolicyAssignmentId(createResponse.getId());
            ShowPolicyAssignmentResponse showResponse = client.showPolicyAssignment(showPolicyAssignmentRequest);
            System.out.println(showResponse.toString());
        } catch (ConnectionException | RequestTimeoutException | ServiceResponseException ex) {
            System.out.println(ex);
        }
    }
}

Response

class CreatePolicyAssignmentsResponse {
    policyAssignmentType: "policyAssignmentType"
    id: "id"
    name: "name"
    description: "description"
    policyFilter: class PolicyFilterDefinition {}
    period: "period"
    state: "state"
    created: "created"
    updated: "updated"
    policyDefinitionId: "policyDefinitionId"
    customPolicy: "customPolicy"
    parameters: {}
    createdBy: "createdBy"
}

class ShowPolicyAssignmentResponse {
    policyAssignmentType: "policyAssignmentType"
    id: "id"
    name: "name"
    description: "description"
    policyFilter: class PolicyFilterDefinition {}
    period: "period"
    state: "state"
    created: "created"
    updated: "updated"
    policyDefinitionId: "policyDefinitionId"
    customPolicy: "customPolicy"
    parameters: {}
    createdBy: "createdBy"
}	

Reference

For more details, see Resource Compliance Overview.