更新时间:2024-10-09 GMT+08:00
分享

查询CA配额

查询CA配额总数和已使用的CA配额数。

相关参数详情请参见查询CA配额参数说明

import com.huaweicloud.sdk.ccm.v1.CcmClient;
import com.huaweicloud.sdk.ccm.v1.model.Resources;
import com.huaweicloud.sdk.ccm.v1.model.ShowCertificateAuthorityQuotaRequest;
import com.huaweicloud.sdk.ccm.v1.model.ShowCertificateAuthorityQuotaResponse;
import com.huaweicloud.sdk.core.auth.GlobalCredentials;

import java.util.List;

/**
 * 查询CA证书配额
 */
public class ShowCertificateAuthorityQuotaExample {
    /**
     * 基础认证信息:
     * - ACCESS_KEY: 华为云账号Access Key
     * - SECRET_ACCESS_KEY: 华为云账号Secret Access Key
     * - DOMAIN_ID: 华为云账号ID
     * - CCM_ENDPOINT: 华为云CCM服务(PCA属于CCM下的微服务)访问终端地址
     * 认证使用的ak和sk硬编到代码中或明文存储存在较大安全风险,建议在配置文件或环境变量中密文存放,使用时解密,确保安全;
     * 本示例ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
     */
    private static final String ACCESS_KEY = System.getenv("HUAWEICLOUD_SDK_AK");
    private static final String SECRET_ACCESS_KEY =  System.getenv("HUAWEICLOUD_SDK_SK");
    private static final String DOMAIN_ID = "<DomainID>";
    private static final String CCM_ENDPOINT = "<CcmEndpoint>";

    public static void main(String[] args) {
        // 1.准备访问华为云的认证信息,PCA为全局服务
        final GlobalCredentials auth = new GlobalCredentials()
                .withAk(ACCESS_KEY)
                .withSk(SECRET_ACCESS_KEY)
                .withDomainId(DOMAIN_ID);

        // 2.初始化SDK,传入认证信息及CCM服务的访问终端地址
        final CcmClient ccmClient = CcmClient.newBuilder()
                .withCredential(auth)
                .withEndpoint(CCM_ENDPOINT).build();

        // 3、构造请求体
        ShowCertificateAuthorityQuotaRequest request = new ShowCertificateAuthorityQuotaRequest();

        // 4、开始发起请求
        ShowCertificateAuthorityQuotaResponse response;
        try {
            response = ccmClient.showCertificateAuthorityQuota(request);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }

        // 5、获取配额使用情况
        List<Resources> quotas = response.getQuotas().getResources();
        // 配额总量
        int caQuota = quotas.get(0).getQuota();
        // 已使用的配额
        int used = quotas.get(0).getUsed();

        System.out.println(response);
    }

}

相关文档