Help Center/ Cloud Firewall/ Best Practices/ Purchasing and Querying CFW via API
Updated on 2024-10-10 GMT+08:00

Purchasing and Querying CFW via API

Application Scenarios

For professionals, using APIs is more efficient than using the console. CFW provides APIs for diverse functions. For details, see APIs.

You can use APIs to quickly purchase and query standard edition firewall instances.

Prerequisites

The current account has the BSS Administrator and CFW FullAccess permissions.

Purchasing and Querying a Standard Edition Firewall

  1. Log in to the management console.
  2. Choose Tools > API Explorer in the upper right corner.
  3. In the navigation pane on the left, click All Products and choose Security & Compliance > Cloud Firewall.
  4. Buy a standard firewall. Select the Create Firewall API, set the key parameters as follows, and set other parameters as required.

    • Region: Select the region where the cloud asset is located.
    • project_id: project ID, which is automatically obtained.
    • flavor: Enter flavor information.
      • version: firewall edition. In this example, select Standard. For details about the differences between editions, see Editions.
    • charge_info: Enter the billing mode.
      • charge_mode: Enter the billing mode information. In this example, the billing mode is yearly/monthly. Set this parameter to prePaid.
      • is_auto_renew: Whether to automatically renew the subscription. In this example, the subscription period is one month. Select false.
      • is_auto_pay: Whether automatic payment is enabled. In this example, select true.

  5. Query a purchased firewall. Select ListFirewallList API, set the key parameters as follows, and set other parameters as required.

    • Region: Select the region where the firewall is located.
    • project_id: project ID, which is automatically obtained.

    • key_word: Enter a keyword, for example, a firewall name.
    • limit: Set the number of records displayed on each page. In this example, set it to 1.
    • offset: Set the start position of the returned record. Set it to 0.

Code Example

Prepare basic authentication information.
import com.huaweicloud.sdk.cfw.v1.CfwClient;
import com.huaweicloud.sdk.cfw.v1.model.*;
import com.huaweicloud.sdk.cfw.v1.region.CfwRegion;
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 java.util.ArrayList;
import java.util.List;

public class CreateFirewallSolution {

    public static void main(String[] args) {
        String ak = "xxxx";
        String sk = "xxxx";

        BasicCredentials auth = new BasicCredentials().withAk(ak).withSk(sk);

        CfwClient client = CfwClient.newBuilder()
            .withCredential(auth)
            .withRegion(CfwRegion.valueOf("xxxx"))
            .build();

        //Request body for creating a firewall.
        CreateFirewallRequest request = new CreateFirewallRequest();
        CreateFirewallReq body = new CreateFirewallReq();
        body.setName("cfwtest");
        body.setEnterpriseProjectId("0");
        CreateFirewallReqTags createFirewallReqTags = new CreateFirewallReqTags();
        createFirewallReqTags.setKey("TagKey");
        createFirewallReqTags.setValue("TagValue");
        List<CreateFirewallReqTags> createFirewallReqTagsList = new ArrayList<>();
        createFirewallReqTagsList.add(createFirewallReqTags);
        body.setTags(createFirewallReqTagsList);
        CreateFirewallReqFlavor flavor = new CreateFirewallReqFlavor();
        flavor.setVersion(CreateFirewallReqFlavor.VersionEnum.STANDARD);
        body.setFlavor(flavor);
        CreateFirewallReqChargeInfo createFirewallReqChargeInfo = new CreateFirewallReqChargeInfo();
        createFirewallReqChargeInfo.setChargeMode("prePaid");
        createFirewallReqChargeInfo.setPeriodType("month");
        createFirewallReqChargeInfo.setPeriodNum(1);
        createFirewallReqChargeInfo.setIsAutoPay(true);
        createFirewallReqChargeInfo.setIsAutoRenew(true);
        body.setChargeInfo(createFirewallReqChargeInfo);
        request.setBody(body);

        //Request body for querying a firewall.
        ListFirewallListRequest listFirewallListRequest = new ListFirewallListRequest();
        QueryFireWallInstanceDto queryFireWallInstanceDto = new QueryFireWallInstanceDto();
        queryFireWallInstanceDto.setOffset(0);
        queryFireWallInstanceDto.setLimit(1);
        queryFireWallInstanceDto.setKeyWord("cfwtest");
        listFirewallListRequest.setBody(queryFireWallInstanceDto);
        try {
            //Create a firewall.
            CreateFirewallResponse createFirewallResponse = client.createFirewall(request);
            System.out.println(createFirewallResponse.toString());

            ////Query the firewall list.
            ListFirewallListResponse listFirewallListResponse = client.listFirewallList(listFirewallListRequest);
            System.out.println(listFirewallListResponse.toString());
        } catch (ConnectionException e) {
            System.out.println(e.getMessage());
        } catch (RequestTimeoutException e) {
            System.out.println(e.getMessage());
        } catch (ServiceResponseException e) {
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}