全部產品
Search
文件中心

Cloud Control API:通過SDK調用雲控制API以實現資源管理

更新時間:Feb 06, 2026

本文示範如何通過Java SDK調用雲控制API實現資源管理。以Virtual Private Cloud為例,展示SDK的整合過程,以及查詢資源中繼資料、建立、查詢、更新和刪除資源的具體操作。此外,還將介紹在雲控制API採用非同步作業處理任務時,如何查詢任務狀態。

前提條件

為確保順利操作,請完成以下前置準備:

  • 已整合SDK,具體操作請參見Java整合SDK

  • 資源管理基於資源中繼資料進行操作,因此熟悉資源中繼資料是高效使用雲控制 API 的關鍵。資源中繼資料定義了資源的屬性、狀態及其他相關資訊。具體資訊,請參見資源中繼資料

  • 雲控制API處理任務時採用非同步作業,因此您需要瞭解同步操作與非同步作業的基本概念。關於查詢任務狀態的具體資訊,請參見教程中的GetTask介面。

  • 請求路徑具體資訊,請參見請求路徑說明

調用雲控制API以實現資源管理

本文教程以Virtual Private Cloud為樣本,其中Virtual Private Cloud的產品code為VPC,資源類型code也為VPC。通過產品code和資源類型code,您可以查詢資源類型的詳細資料。後續的資源管理操作,包括建立、查詢、列舉、更新及刪除,均基於獲得的資源中繼資料進行。

介面名稱:GetResourceType

此介面旨在查詢資源類型的詳情。可以通過產品code與資源類型code查詢相應的資源類型的詳細資料,返回的資料為資源中繼資料。有關介面詳細資料,請參見擷取資源中繼資料 API文檔。在調用過程中,您需根據實際業務需求設定相應的參數及回合組態。

//  配置運行時參數
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
//        // 忽略對 SSL 憑證的驗證
//        runtime.ignoreSSL = true;
//        // 代理配置
//        runtime.httpProxy = "http://127.0.0.1:9898";
//        runtime.httpsProxy = "http://user:password@127.0.0.1:8989";
//        runtime.noProxy = "127.0.0.1,localhost";
//        // 連線逾時
//        runtime.connectTimeout = 5000;
//        // 讀逾時
//        runtime.readTimeout = 10000;
//        // 開啟自動重試機制
//        runtime.autoretry = true;
//        // 設定自動重試次數
//        runtime.maxAttempts = 3;

//        請求路徑  
String requestPath = "/api/v1/providers/Aliyun/products/" + PRODUCT_CODE + "/resourceTypes/" + RESOURCE_TYPE_CODE;
com.aliyun.cloudcontrol20220830.models.GetResourceTypeHeaders getResourceTypeHeaders = new com.aliyun.cloudcontrol20220830.models.GetResourceTypeHeaders()
    .setXAcsAcceptLanguage("zh_CH"); // 選擇返回產品的語言 zh_CH:中文 (預設)en_US:英文。
//  發起請求    
GetResourceTypeResponse getResourceTypeResponse = client.getResourceTypeWithOptions(requestPath, getResourceTypeHeaders, runtime);

完整範例程式碼

import com.aliyun.cloudcontrol20220830.Client;
import com.aliyun.cloudcontrol20220830.models.*;
import com.aliyun.tea.TeaConverter;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaPair;
import com.google.gson.Gson;

public class Sample {

    private static final String PRODUCT_CODE = "VPC"; // 產品code,此例中為"VPC"
    private static final String RESOURCE_TYPE_CODE = "VPC"; // 資源類型code,此例中為"VPC"
    public static void main(String[] args) throws Exception {
        // 初始化用戶端
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
            .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
            .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = "cloudcontrol.aliyuncs.com";
        com.aliyun.cloudcontrol20220830.Client client = new com.aliyun.cloudcontrol20220830.Client(config);

        // 運行時配置
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        // 請求路徑
        String requestPath = "/api/v1/providers/Aliyun/products/" + PRODUCT_CODE + "/resourceTypes/" + RESOURCE_TYPE_CODE;
        com.aliyun.cloudcontrol20220830.models.GetResourceTypeHeaders getResourceTypeHeaders = new com.aliyun.cloudcontrol20220830.models.GetResourceTypeHeaders()
            .setXAcsAcceptLanguage("zh_CH"); // 選擇返回產品的語言 zh_CH:中文 (預設)en_US:英文。
        try {
            // 發起請求 
            GetResourceTypeResponse getResourceTypeResponse = client.getResourceTypeWithOptions(requestPath, getResourceTypeHeaders, runtime);
            // 列印響應結果
            System.out.println(new Gson().toJson(getResourceTypeResponse.getBody()));
        } catch (TeaException error) {
            // 錯誤 message
            System.out.println(error.getMessage());
            // 診斷地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 錯誤 message
            System.out.println(error.getMessage());
            // 診斷地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

此次請求結果為VPC的資源中繼資料。有關資源中繼資料的詳細解讀,請參見查詢資源類型的詳情。響應結果如下:

{
    "requestId": "BE9E0796-DB31-5320-A446-1A2316E7CB92",
    "resourceType": {
        "createOnlyProperties": [
            "/properties/DryRun",
            "/properties/Ipv6Isp",
            "/properties/EnableIpv6"
        ],
        "deleteOnlyProperties": [

        ],
        "filterProperties": [
            "/properties/DhcpOptionsSetId",
            "/properties/ResourceGroupId",
            "/properties/VpcId",
            "/properties/VpcName",
            "/properties/IsDefault"
        ],
        "getOnlyProperties": [
            "/properties/DryRun"
        ],
        "getResponseProperties": [
            "/properties/Ipv6CidrBlocks/items/properties/Ipv6Isp",
            "/properties/RegionId",
            "/properties/VpcName",
            "/properties/ResourceType",
            "/properties/DhcpOptionsSetId",
            "/properties/CreateTime",
            "/properties/Tags/items/properties/TagKey",
            "/properties/SecondaryCidrBlocks/items",
            "/properties/Tags",
            "/properties/IsDefault",
            "/properties/UserCidrs/items",
            "/properties/ResourceGroupId",
            "/properties/CidrBlock",
            "/properties/RouterId",
            "/properties/Tags/items",
            "/properties/Description",
            "/properties/Ipv6CidrBlocks",
            "/properties/SecondaryCidrBlocks",
            "/properties/Status",
            "/properties/VSwitchIds",
            "/properties/Tags/items/properties/TagValue",
            "/properties/Ipv6CidrBlocks/items",
            "/properties/Ipv6CidrBlocks/items/properties/Ipv6CidrBlock",
            "/properties/Ipv6CidrBlock",
            "/properties/ClassicLinkEnabled",
            "/properties/UserCidrs",
            "/properties/VSwitchIds/items",
            "/properties/VpcId"
        ],
        "handlers": {
            "create": {
                "permissions": [
                    "vpc:CreateVpc"
                ]
            },
            "delete": {
                "permissions": [
                    "vpc:DeleteVpc"
                ]
            },
            "get": {
                "permissions": [
                    "vpc:ListTagResources",
                    "vpc:DescribeVpcAttribute",
                    "vpc:DescribeVpcs"
                ]
            },
            "list": {
                "permissions": [
                    "vpc:DescribeVpcs"
                ]
            },
            "update": {
                "permissions": [
                    "vpc:TagResources",
                    "vpc:AssociateVpcCidrBlock",
                    "vpc:MoveResourceGroup",
                    "vpc:ModifyVpcAttribute",
                    "vpc:UnTagResources",
                    "vpc:UnassociateVpcCidrBlock",
                    "vpc:EnableVpcClassicLink",
                    "vpc:DisableVpcClassicLink"
                ]
            }
        },
        "info": {
            "chargeType": "free",
            "deliveryScope": "region",
            "description": "一個VPC執行個體代表您建立了一個專用網路。您可以完全掌控自己的專用網路,例如選擇IP位址範圍、配置路由表和網關等,您可以在自己定義的專用網路中使用阿里雲資源如雲端服務器、雲資料庫RDS版和負載平衡等。\t",
            "title": "專用網路"
        },
        "listOnlyProperties": [
            "/properties/DryRun"
        ],
        "listResponseProperties": [
            "/properties/Ipv6CidrBlocks/items/properties/Ipv6Isp",
            "/properties/RegionId",
            "/properties/VpcName",
            "/properties/DhcpOptionsSetId",
            "/properties/CreateTime",
            "/properties/Tags/items/properties/TagKey",
            "/properties/SecondaryCidrBlocks/items",
            "/properties/Tags",
            "/properties/IsDefault",
            "/properties/UserCidrs/items",
            "/properties/ResourceGroupId",
            "/properties/CidrBlock",
            "/properties/RouterId",
            "/properties/Tags/items",
            "/properties/Description",
            "/properties/Ipv6CidrBlocks",
            "/properties/SecondaryCidrBlocks",
            "/properties/Status",
            "/properties/VSwitchIds",
            "/properties/Tags/items/properties/TagValue",
            "/properties/Ipv6CidrBlocks/items",
            "/properties/Ipv6CidrBlocks/items/properties/Ipv6CidrBlock",
            "/properties/Ipv6CidrBlock",
            "/properties/UserCidrs",
            "/properties/VSwitchIds/items",
            "/properties/VpcId"
        ],
        "primaryIdentifier": "/properties/VpcId",
        "product": "VPC",
        "properties": {
            "Status": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "pattern": "",
                "description": "VPC的狀態。",
                "readOnly": true,
                "sensitive": false,
                "title": "VPC的狀態。",
                "type": "string",
                "updateType": false
            },
            "IsDefault": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "description": "是否是預設VPC。",
                "readOnly": false,
                "sensitive": false,
                "title": "是否是預設VPC。",
                "type": "boolean",
                "updateType": false
            },
            "Ipv6Isp": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "pattern": "",
                "description": "VPC的IPv6位址區段類型。取值:\n\n- **BGP**(預設值):阿里雲BGP IPv6。\n- **ChinaMobile**:中國移動(單線)。\n- **ChinaUnicom**:中國聯通(單線)。\n- **ChinaTelecom**:中國電信(單線)。\n\n\u003e 如果是開通了單線頻寬白名單的使用者,該欄位可以設定為**ChinaTelecom**(中國電信)、**ChinaUnicom**(中國聯通)或**ChinaMobile**(中國移動)。",
                "operatePrivateType": [
                    "create"
                ],
                "readOnly": false,
                "sensitive": false,
                "title": "VPC的IPv6位址區段類型。取值:\n\n- **BGP**(預設值):阿里雲BGP IPv6。\n- **ChinaMobile**:中國移動(單線)。\n- **ChinaUnicom**:中國聯通(單線)。\n- **ChinaTelecom**:中國電信(單線)。\n\n\u003e 如果是開通了單線頻寬白名單的使用者,該欄位可以設定為**ChinaTelecom**(中國電信)、**ChinaUnicom**(中國聯通)或**ChinaMobile**(中國移動)。",
                "type": "string",
                "updateType": false
            },
            "Description": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "VPC的描述。",
                "readOnly": false,
                "sensitive": false,
                "title": "VPC的描述。",
                "type": "string",
                "updateType": true
            },
            "ResourceGroupId": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "資源群組ID。",
                "readOnly": false,
                "sensitive": false,
                "title": "資源群組ID。",
                "type": "string",
                "updateType": true
            },
            "ClassicLinkEnabled": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "description": "ClassicLink功能開啟狀態",
                "readOnly": false,
                "sensitive": false,
                "title": "ClassicLink功能開啟狀態",
                "type": "boolean",
                "updateType": true
            },
            "SecondaryCidrBlocks": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "附加網段資訊。",
                "readOnly": false,
                "sensitive": false,
                "title": "附加網段資訊。",
                "type": "array",
                "items": {
                    "description": "VPC的附加網段。",
                    "title": "VPC的附加網段。",
                    "type": "string",
                    "updateType": true
                },
                "updateType": true
            },
            "VSwitchIds": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "description": "VPC下的交換器列表。",
                "readOnly": true,
                "sensitive": false,
                "title": "VPC下的交換器列表。",
                "type": "array",
                "items": {
                    "extMonitorInfo": false,
                    "deprecated": false,
                    "pattern": "",
                    "description": "VPC下的交換器列表。",
                    "readOnly": true,
                    "sensitive": false,
                    "title": "VPC下的交換器列表。",
                    "type": "string",
                    "updateType": false
                },
                "updateType": false
            },
            "CreateTime": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "pattern": "",
                "description": "VPC的建立時間。",
                "readOnly": true,
                "sensitive": false,
                "title": "VPC的建立時間。",
                "type": "string",
                "updateType": false
            },
            "DryRun": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "description": "是否只預檢此次請求。取值:\n- **true**:發送檢查請求,不會建立VPC。檢查項包括是否填寫了必需參數、請求格式、業務限制。如果檢查不通過,則返回對應錯誤。如果檢查通過,則返回錯誤碼`DryRunOperation`。\n- **false**(預設值):發送正常請求,通過檢查後返回HTTP 2xx狀態代碼並直接建立VPC。",
                "operatePrivateType": [
                    "create",
                    "list",
                    "get"
                ],
                "readOnly": false,
                "sensitive": false,
                "title": "是否只預檢此次請求。取值:\n- **true**:發送檢查請求,不會建立VPC。檢查項包括是否填寫了必需參數、請求格式、業務限制。如果檢查不通過,則返回對應錯誤。如果檢查通過,則返回錯誤碼`DryRunOperation`。\n- **false**(預設值):發送正常請求,通過檢查後返回HTTP 2xx狀態代碼並直接建立VPC。",
                "type": "boolean",
                "updateType": false
            },
            "RouterId": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "pattern": "",
                "description": "VPC的路由器ID。",
                "readOnly": true,
                "sensitive": false,
                "title": "VPC的路由器ID。",
                "type": "string",
                "updateType": false
            },
            "CidrBlock": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "VPC的私網網段。",
                "readOnly": false,
                "sensitive": false,
                "title": "VPC的私網網段。",
                "type": "string",
                "updateType": true
            },
            "UserCidrs": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "使用者側網路的網段,如需定義多個網段請使用半形逗號隔開,最多支援3個網段。",
                "readOnly": false,
                "sensitive": false,
                "title": "使用者側網路的網段,如需定義多個網段請使用半形逗號隔開,最多支援3個網段。",
                "type": "array",
                "items": {
                    "isRequired": false,
                    "extMonitorInfo": false,
                    "deprecated": false,
                    "pattern": "",
                    "description": "使用者側網路的網段,如需定義多個網段請使用半形逗號隔開,最多支援3個網段。",
                    "sensitive": false,
                    "title": "使用者側網路的網段,如需定義多個網段請使用半形逗號隔開,最多支援3個網段。",
                    "type": "string",
                    "updateType": false
                },
                "updateType": false
            },
            "EnableIpv6": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "description": "是否開啟IPv6網段。取值:\n\n- **false**(預設值):不開啟。\n- **true**:開啟。",
                "operatePrivateType": [
                    "create"
                ],
                "readOnly": false,
                "sensitive": false,
                "title": "是否開啟IPv6網段。取值:\n\n- **false**(預設值):不開啟。\n- **true**:開啟。",
                "type": "boolean",
                "updateType": false
            },
            "DhcpOptionsSetId": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "DHCP選項集的ID。",
                "readOnly": false,
                "sensitive": false,
                "title": "DHCP選項集的ID。",
                "type": "string",
                "updateType": false
            },
            "VpcId": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "VPC的ID。",
                "readOnly": false,
                "sensitive": false,
                "title": "VPC的ID。",
                "type": "string",
                "updateType": false
            },
            "VpcName": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "VPC的名稱。",
                "readOnly": false,
                "sensitive": false,
                "title": "VPC的名稱。",
                "type": "string",
                "updateType": true
            },
            "RegionId": {
                "isRequired": true,
                "extMonitorInfo": false,
                "description": "VPC所在的地區。",
                "readOnly": false,
                "sensitive": false,
                "title": "VPC所在的地區。",
                "type": "string",
                "updateType": false
            },
            "Ipv6CidrBlocks": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "description": "VPC的IPv6網段資訊。",
                "readOnly": true,
                "sensitive": false,
                "title": "VPC的IPv6網段資訊。",
                "type": "array",
                "items": {
                    "extMonitorInfo": false,
                    "deprecated": false,
                    "description": "VPC的IPv6網段資訊。",
                    "readOnly": true,
                    "sensitive": false,
                    "title": "VPC的IPv6網段資訊。",
                    "type": "object",
                    "properties": {
                        "Ipv6Isp": {
                            "isRequired": false,
                            "extMonitorInfo": false,
                            "deprecated": false,
                            "pattern": "",
                            "description": "VPC的IPv6位址區段類型,取值:\n\n- **BGP**:阿里雲BGP IPv6。\n- **ChinaMobile**:中國移動(單線)。\n- **ChinaUnicom**:中國聯通(單線)。\n- **ChinaTelecom**:中國電信(單線)。\n\n\u003e 如果是開通了單線頻寬白名單的使用者,該欄位可以設定為**ChinaTelecom**(中國電信)、**ChinaUnicom**(中國聯通)和**ChinaMobile**(中國移動)。",
                            "readOnly": true,
                            "sensitive": false,
                            "title": "VPC的IPv6位址區段類型,取值:\n\n- **BGP**:阿里雲BGP IPv6。\n- **ChinaMobile**:中國移動(單線)。\n- **ChinaUnicom**:中國聯通(單線)。\n- **ChinaTelecom**:中國電信(單線)。\n\n\u003e 如果是開通了單線頻寬白名單的使用者,該欄位可以設定為**ChinaTelecom**(中國電信)、**ChinaUnicom**(中國聯通)和**ChinaMobile**(中國移動)。",
                            "type": "string",
                            "updateType": false
                        },
                        "Ipv6CidrBlock": {
                            "isRequired": false,
                            "extMonitorInfo": false,
                            "deprecated": false,
                            "pattern": "",
                            "description": "VPC的IPv6網段。",
                            "readOnly": true,
                            "sensitive": false,
                            "title": "VPC的IPv6網段。",
                            "type": "string",
                            "updateType": false
                        }
                    },
                    "updateType": false
                },
                "updateType": false
            },
            "Ipv6CidrBlock": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "IPv6地址",
                "readOnly": false,
                "sensitive": false,
                "title": "IPv6地址",
                "type": "string",
                "updateType": true
            },
            "Tags": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "VPC的標籤",
                "readOnly": false,
                "sensitive": false,
                "title": "VPC的標籤",
                "type": "array",
                "items": {
                    "extMonitorInfo": false,
                    "deprecated": false,
                    "description": "VPC的標籤",
                    "sensitive": false,
                    "title": "VPC的標籤",
                    "type": "object",
                    "properties": {
                        "TagKey": {
                            "isRequired": false,
                            "extMonitorInfo": false,
                            "description": "標籤的鍵",
                            "readOnly": false,
                            "sensitive": false,
                            "title": "標籤的鍵",
                            "type": "string",
                            "updateType": true
                        },
                        "TagValue": {
                            "isRequired": false,
                            "extMonitorInfo": false,
                            "description": "標籤的值",
                            "readOnly": false,
                            "sensitive": false,
                            "title": "標籤的值",
                            "type": "string",
                            "updateType": true
                        }
                    },
                    "updateType": false
                },
                "updateType": true
            }
        },
        "publicProperties": [
            "/properties/RouterId",
            "/properties/VpcId",
            "/properties/DhcpOptionsSetId",
            "/properties/ResourceGroupId",
            "/properties/UserCidrs",
            "/properties/UserCidrs/items",
            "/properties/VSwitchIds",
            "/properties/RegionId",
            "/properties/Ipv6CidrBlocks",
            "/properties/VSwitchIds/items",
            "/properties/Tags",
            "/properties/Tags/items",
            "/properties/Ipv6CidrBlocks/items",
            "/properties/CidrBlock",
            "/properties/Ipv6CidrBlocks/items/properties/Ipv6Isp",
            "/properties/Tags/items/properties/TagKey",
            "/properties/Ipv6CidrBlocks/items/properties/Ipv6CidrBlock",
            "/properties/Status",
            "/properties/Tags/items/properties/TagValue",
            "/properties/CreateTime",
            "/properties/Ipv6CidrBlock",
            "/properties/Description",
            "/properties/VpcName",
            "/properties/ClassicLinkEnabled",
            "/properties/IsDefault",
            "/properties/SecondaryCidrBlocks",
            "/properties/SecondaryCidrBlocks/items"
        ],
        "readOnlyProperties": [
            "/properties/Ipv6CidrBlocks/items/properties/Ipv6Isp",
            "/properties/CreateTime",
            "/properties/RouterId",
            "/properties/Ipv6CidrBlocks",
            "/properties/Status",
            "/properties/VSwitchIds",
            "/properties/Ipv6CidrBlocks/items",
            "/properties/Ipv6CidrBlocks/items/properties/Ipv6CidrBlock",
            "/properties/VSwitchIds/items"
        ],
        "required": [
            "RegionId"
        ],
        "resourceType": "VPC",
        "sensitiveInfoProperties": [

        ],
        "updateOnlyProperties": [

        ],
        "updateTypeProperties": [
            "/properties/ResourceGroupId",
            "/properties/Tags",
            "/properties/CidrBlock",
            "/properties/Tags/items/properties/TagKey",
            "/properties/Tags/items/properties/TagValue",
            "/properties/Ipv6CidrBlock",
            "/properties/Description",
            "/properties/VpcName",
            "/properties/ClassicLinkEnabled",
            "/properties/SecondaryCidrBlocks",
            "/properties/SecondaryCidrBlocks/items"
        ]
    }
}

介面名稱:CreateResource

此介面的目的是建立一個Virtual Private Cloud資源。有關介面詳細資料,請參見建立資源 API文檔。在調用過程中,您需根據實際業務需求設定相應的請求參數及回合組態。

// 運行時參數     
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
//        // 忽略對 SSL 憑證的驗證
//        runtime.ignoreSSL = true;
//        // 代理配置
//        runtime.httpProxy = "http://127.0.0.1:9898";
//        runtime.httpsProxy = "http://user:password@127.0.0.1:8989";
//        runtime.noProxy = "127.0.0.1,localhost";
//        // 連線逾時
//        runtime.connectTimeout = 5000;
//        // 讀逾時
//        runtime.readTimeout = 10000;
//        // 開啟自動重試機制
//        runtime.autoretry = true;
//        // 設定自動重試次數
//        runtime.maxAttempts = 3;

// headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
java.util.Map < String, String > headers = new java.util.HashMap < > ();
//        headers.put("x-acs-action", "CreateResource"); // 設定介面名稱

// 請求路徑
String requestPath = "/api/v1/providers/Aliyun/products/" + PRODUCT_CODE + "/resources/" + RESOURCE_TYPE_CODE;
java.util.Map < String, Object > body = TeaConverter.buildMap(
    new TeaPair("IsDefault", true), // 是否預設VPC
    new TeaPair("Description", "這是一個測試VPC"), // 描述
    new TeaPair("VpcName", "VPC_Test"), // VPC名稱
    new TeaPair("Tags", java.util.Arrays.asList(
        TeaConverter.buildMap(
            new TeaPair("TagKey", "key"),
            new TeaPair("TagValue", "value")
        )
    )), // VPC標籤
    new TeaPair("EnableIpv6", false), //是否開啟IPv6網段
    new TeaPair("DryRun", false) // 是否只預檢此次請求
);
// 請求參數
com.aliyun.cloudcontrol20220830.models.CreateResourceRequest createResourceRequest = new com.aliyun.cloudcontrol20220830.models.CreateResourceRequest()
    .setRegionId("cn-qingdao") // 地區ID
    .setBody(body);
// 發起請求
CreateResourceResponse createResourceResponse = client.createResourceWithOptions(requestPath, createResourceRequest, headers, runtime);

完整範例程式碼

import com.aliyun.cloudcontrol20220830.Client;
import com.aliyun.cloudcontrol20220830.models.*;
import com.aliyun.tea.TeaConverter;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaPair;
import com.google.gson.Gson;

public class Sample {

    private static final String PRODUCT_CODE = "VPC"; // 產品code,此例中為"VPC"
    private static final String RESOURCE_TYPE_CODE = "VPC"; // 資源類型code,此例中為"VPC"
    public static void main(String[] args) throws Exception {
        // 初始化用戶端
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
            .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
            .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = "cloudcontrol.aliyuncs.com";
        com.aliyun.cloudcontrol20220830.Client client = new com.aliyun.cloudcontrol20220830.Client(config);

        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        // headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
        java.util.Map < String, String > headers = new java.util.HashMap < > ();
        // 請求路徑
        String requestPath = "/api/v1/providers/Aliyun/products/" + PRODUCT_CODE + "/resources/" + RESOURCE_TYPE_CODE;
        java.util.Map < String, Object > body = TeaConverter.buildMap(
            new TeaPair("IsDefault", true), // 是否預設VPC
            new TeaPair("Description", "這是一個測試VPC"), // 描述
            new TeaPair("VpcName", "VPC_Test"), // VPC名稱
            new TeaPair("Tags", java.util.Arrays.asList(
                TeaConverter.buildMap(
                    new TeaPair("TagKey", "key"),
                    new TeaPair("TagValue", "value")
                )
            )), // VPC標籤
            new TeaPair("EnableIpv6", false), //是否開啟IPv6網段
            new TeaPair("DryRun", false) // 是否只預檢此次請求
        );
        // 配置請求參數
        com.aliyun.cloudcontrol20220830.models.CreateResourceRequest createResourceRequest = new com.aliyun.cloudcontrol20220830.models.CreateResourceRequest()
            .setRegionId("cn-qingdao") // 地區ID
            .setBody(body);
        try {

            // 發起請求
            CreateResourceResponse createResourceResponse = client.createResourceWithOptions(requestPath, createResourceRequest, headers, runtime);
            // 列印結果
            System.out.println(new Gson().toJson(createResourceResponse.getBody()));
        } catch (TeaException error) {
            // 錯誤 message
            System.out.println(error.getMessage());
            // 診斷地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 錯誤 message
            System.out.println(error.getMessage());
            // 診斷地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

此次請求返回的statusCode=202表明Virtual Private Cloud建立成功,該過程為非同步作業。為進一步查詢任務狀態,需記錄taskId,並調用GetTask介面擷取任務詳細資料。同時,記錄resourceId,用於後續步驟中的查詢資源、更新資源與刪除資源等操作。響應結果如下:

{
    "requestId": "D3AA2FD7-CA66-5A6D-B1AE-A5C8C785ADBB",
    "resourceId": "vpc-m5eml8m3XXXXXXXX",
    "resourcePath": "VPC/vpc-m5eml8m3XXXXXXXX",
    "taskId": "task-5057a4fbef2bc1a4XXXXXXXX"
}

介面名稱:GetResources

此介面的目的是擷取一個Virtual Private Cloud資來源詳細資料。有關介面詳細資料,請參見擷取資源詳情 API文檔。 在調用過程中,您需根據實際業務需求設定相應的請求參數及回合組態。

// 運行時參數
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
//        // 忽略對 SSL 憑證的驗證
//        runtime.ignoreSSL = true;
//        // 代理配置
//        runtime.httpProxy = "http://127.0.0.1:9898";
//        runtime.httpsProxy = "http://user:password@127.0.0.1:8989";
//        runtime.noProxy = "127.0.0.1,localhost";
//        // 連線逾時
//        runtime.connectTimeout = 5000;
//        // 讀逾時
//        runtime.readTimeout = 10000;
//        // 開啟自動重試機制
//        runtime.autoretry = true;
//        // 設定自動重試次數
//        runtime.maxAttempts = 3;

//  headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
java.util.Map < String, String > headers = new java.util.HashMap < > ();
//  headers.put("x-acs-action", "GetResources"); // 設定介面名稱
String resourceId = "vpc-m5eml8m3XXXXXXXX"; 
// 請求路徑
String requestPath = "/api/v1/providers/Aliyun/products/" + PRODUCT_CODE + "/resources/" + RESOURCE_TYPE_CODE + "/" + resourceId;
com.aliyun.cloudcontrol20220830.models.GetResourcesRequest getResourcesRequest = new com.aliyun.cloudcontrol20220830.models.GetResourcesRequest()
    .setRegionId("cn-qingdao"); // 地區ID
// 發起請求
GetResourcesResponse getResourcesResponse = client.getResourcesWithOptions(requestPath, getResourcesRequest, headers, runtime);

完整範例程式碼

import com.aliyun.cloudcontrol20220830.Client;
import com.aliyun.cloudcontrol20220830.models.*;
import com.aliyun.tea.TeaConverter;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaPair;
import com.google.gson.Gson;

public class Sample {

    private static final String PRODUCT_CODE = "VPC"; // 產品code,此例中為"VPC"
    private static final String RESOURCE_TYPE_CODE = "VPC"; // 資源類型code,此例中為"VPC"
    public static void main(String[] args) throws Exception {
        //      初始化用戶端
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
            .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
            .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = "cloudcontrol.aliyuncs.com"; // 服務地址
        com.aliyun.cloudcontrol20220830.Client client = new com.aliyun.cloudcontrol20220830.Client(config);

        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        //        // 忽略對 SSL 憑證的驗證
        //        runtime.ignoreSSL = true;
        //        // 代理配置
        //        runtime.httpProxy = "http://127.0.0.1:9898";
        //        runtime.httpsProxy = "http://user:password@127.0.0.1:8989";
        //        runtime.noProxy = "127.0.0.1,localhost";
        //        // 連線逾時
        //        runtime.connectTimeout = 5000;
        //        // 讀逾時
        //        runtime.readTimeout = 10000;
        //        // 開啟自動重試機制
        //        runtime.autoretry = true;
        //        // 設定自動重試次數
        //        runtime.maxAttempts = 3;
        //     headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
        java.util.Map < String, String > headers = new java.util.HashMap < > ();

        String resourceId = "vrt-m5eyitbd6XXXXXXXX";
        String requestPath = "/api/v1/providers/Aliyun/products/" + PRODUCT_CODE + "/resources/" + RESOURCE_TYPE_CODE + "/" + resourceId;
        // 請求參數
        com.aliyun.cloudcontrol20220830.models.GetResourcesRequest getResourcesRequest = new com.aliyun.cloudcontrol20220830.models.GetResourcesRequest()
            .setRegionId("cn-qingdao");
        try {
            // 發起請求
            GetResourcesResponse getResourcesResponse = client.getResourcesWithOptions(requestPath, getResourcesRequest, headers, runtime);
            // 列印響應
            System.out.println(new Gson().toJson(getResourcesResponse.getBody()));
        } catch (TeaException error) {
            // 錯誤 message
            System.out.println(error.getMessage());
            // 診斷地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 錯誤 message
            System.out.println(error.getMessage());
            // 診斷地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

此次請求返回結果為Virtual Private Cloud資源的詳細資料。響應結果如下:

{
    "requestId": "475BC512-7686-51DD-BAFA-EDA116EADE4E",
    "resource": {
        "resourceAttributes": {
            "IsDefault": false,
            "Status": "Available",
            "Description": "這是一個測試VPC",
            "ResourceGroupId": "rg-acfmykXXXXXXXX",
            "SecondaryCidrBlocks": [

            ],
            "VSwitchIds": [

            ],
            "CreateTime": "2025-04-24T07:19:22Z",
            "CidrBlock": "172.XX.X.X/XX",
            "RouterId": "vrt-m5eyitbd6XXXXXXXX",
            "UserCidrs": [

            ],
            "VpcId": "vpc-m5eml8m3XXXXXXXX",
            "VpcName": "VPC_Test",
            "RegionId": "cn-qingdao",
            "Ipv6CidrBlock": "",
            "Ipv6CidrBlocks": [
                {
                    "Ipv6CidrBlock": ""
                }
            ],
            "Tags": [
                {
                    "TagKey": "acs:tag:XXXXXXXX",
                    "TagValue": "sub:207343XXXXXXXX:XX"
                }
            ]
        },
        "resourceId": "vpc-m5eml8m3XXXXXXXX"
    }
}

介面名稱:GetResources

此介面的目的是列舉所有Virtual Private Cloud資源資訊。有關介面詳細資料,請參見 列舉資源 API文檔。 在調用過程中,您需根據實際業務需求設定相應的請求參數及回合組態。

// 運行時配置
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
//        // 忽略對 SSL 憑證的驗證
//        runtime.ignoreSSL = true;
//        // 代理配置
//        runtime.httpProxy = "http://127.0.0.1:9898";
//        runtime.httpsProxy = "http://user:password@127.0.0.1:8989";
//        runtime.noProxy = "127.0.0.1,localhost";
//        // 連線逾時
//        runtime.connectTimeout = 5000;
//        // 讀逾時
//        runtime.readTimeout = 10000;
//        // 開啟自動重試機制
//        runtime.autoretry = true;
//        // 設定自動重試次數
//        runtime.maxAttempts = 3;

//          headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
java.util.Map < String, String > headers = new java.util.HashMap < > ();
//        headers.put("x-acs-action", "GetResource"); // 設定介面名稱
//        請求路徑
String requestPath = "/api/v1/providers/Aliyun/products/" + PRODUCT_CODE + "/resources/" + RESOURCE_TYPE_CODE;
// 過濾條件(可選)
// java.util.Map < String, Object > filter = TeaConverter.buildMap(
//    new TeaPair("IsDefault", true), // 是否是預設VPC
//    new TeaPair("ResourceGroupId", "<YOUR_RESOURCEGROUPID>"), // 資源群組ID
//    new TeaPair("DhcpOptionsSetId", "<YOUR_DHCPOPTIONSSETID>"), // DHCP選項集的ID
//    new TeaPair("VpcId", "<YOUR_VPCID>"), // VPC的ID
//    new TeaPair("VpcName", "<YOUR_VPCNAME>") // VPC的名稱
// );
com.aliyun.cloudcontrol20220830.models.GetResourcesRequest getResourcesRequest = new com.aliyun.cloudcontrol20220830.models.GetResourcesRequest()
//    .setFilter(filter)
    .setRegionId("cn-qingdao");
// 發起請求
GetResourcesResponse getResourcesResponse = client.getResourcesWithOptions(requestPath, getResourcesRequest, headers, runtime);

完整範例程式碼

import com.aliyun.cloudcontrol20220830.Client;
import com.aliyun.cloudcontrol20220830.models.*;
import com.aliyun.tea.TeaConverter;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaPair;
import com.google.gson.Gson;

public class Sample {

    private static final String PRODUCT_CODE = "VPC"; // 產品code,此例中為"VPC"
    private static final String RESOURCE_TYPE_CODE = "VPC"; // 資源類型code,此例中為"VPC"
    public static void main(String[] args) throws Exception {
        //      初始化用戶端
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
            .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
            .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = "cloudcontrol.aliyuncs.com";
        com.aliyun.cloudcontrol20220830.Client client = new com.aliyun.cloudcontrol20220830.Client(config);

        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        //        headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
        java.util.Map < String, String > headers = new java.util.HashMap < > ();
        //        headers.put("x-acs-action", "GetResource"); // 設定介面名稱
        //       請求路徑
        String requestPath = "/api/v1/providers/Aliyun/products/" + PRODUCT_CODE + "/resources/" + RESOURCE_TYPE_CODE;
        // 過濾條件(可選)
        // java.util.Map < String, Object > filter = TeaConverter.buildMap(
        //    new TeaPair("IsDefault", true), // 是否是預設VPC
        //    new TeaPair("ResourceGroupId", "<YOUR_RESOURCEGROUPID>"), // 資源群組ID
        //    new TeaPair("DhcpOptionsSetId", "<YOUR_DHCPOPTIONSSETID>"), // DHCP選項集的ID
        //    new TeaPair("VpcId", "<YOUR_VPCID>"), // VPC的ID
        //    new TeaPair("VpcName", "<YOUR_VPCNAME>") // VPC的名稱
        // );
        com.aliyun.cloudcontrol20220830.models.GetResourcesRequest getResourcesRequest = new com.aliyun.cloudcontrol20220830.models.GetResourcesRequest()
            //            .setFilter(filter)
            .setRegionId("cn-qingdao");

        try {
            GetResourcesResponse getResourcesResponse = client.getResourcesWithOptions(requestPath, getResourcesRequest, headers, runtime);
            System.out.println(new Gson().toJson(getResourcesResponse.getBody()));
        } catch (TeaException error) {
            // 錯誤 message
            System.out.println(error.getMessage());
            // 診斷地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 錯誤 message
            System.out.println(error.getMessage());
            // 診斷地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

此次請求的返回結果包含所有資源。如果存在過濾條件,將返回該地區下所有合格資源的詳細資料;如果不存在過濾條件,則將返回該地區下所有資源的詳細資料。響應結果如下:

{
    "maxResults": 50,
    "requestId": "8E4D2B97-F0BE-5595-B9FD-782B79BF53F2",
    "resources": [
        {
            "resourceAttributes": {
                "IsDefault": false,
                "Status": "Available",
                "Description": "這是一個測試VPC",
                "ResourceGroupId": "rg-acfmykdXXXXXXXX",
                "SecondaryCidrBlocks": [

                ],
                "VSwitchIds": [

                ],
                "CreateTime": "2025-04-24T07:19:22Z",
                "CidrBlock": "172.XX.X.X/XX",
                "RouterId": "vrt-m5eyitbd6XXXXXXXX",
                "UserCidrs": [

                ],
                "desiredResourceState": "{\"VpcId\":\"vpc-m5eml8m3XXXXXXXX\",\"VpcName\":\"VPC_Test\"}",
                "VpcId": "vpc-m5eml8m3XXXXXXXX",
                "VpcName": "VPC_Test",
                "RegionId": "cn-qingdao",
                "Ipv6CidrBlock": "",
                "Tags": [
                    {
                        "TagKey": "acs:tag:XXXXXXXX",
                        "TagValue": "sub:20734333XXXXXXXX:yc"
                    }
                ]
            },
            "resourceId": "vpc-m5eml8m3XXXXXXXX"
        }
    ],
    "totalCount": 1
}

介面名稱:UpdateResource

此介面的目的是更新Virtual Private Cloud資源資訊。有關介面詳細資料,請參見更新資源 API文檔。在調用過程中,您需根據實際業務需求設定相應的請求參數及回合組態。

// 運行時配置 
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
//        // 忽略對 SSL 憑證的驗證
//        runtime.ignoreSSL = true;
//        // 代理配置
//        runtime.httpProxy = "http://127.0.0.1:9898";
//        runtime.httpsProxy = "http://user:password@127.0.0.1:8989";
//        runtime.noProxy = "127.0.0.1,localhost";
//        // 連線逾時
//        runtime.connectTimeout = 5000;
//        // 讀逾時
//        runtime.readTimeout = 10000;
//        // 開啟自動重試機制
//        runtime.autoretry = true;
//        // 設定自動重試次數
//        runtime.maxAttempts = 3;

//          headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
java.util.Map < String, String > headers = new java.util.HashMap < > ();
//        headers.put("x-acs-action", "UpdateResource");

// 資源ID
String resourceId = "vpc-m5eml8mXXXXXXXX";
// 請求路徑
String requestPath = "/api/v1/providers/Aliyun/products/VPC/resources/VPC/" + resourceId;
java.util.Map < String, Object > body = TeaConverter.buildMap(
    new TeaPair("Description", "更新測試VPC描述"), // 資源描述
    new TeaPair("VpcName", "UpdateVpc"), // 資源名稱
    new TeaPair("Tags", java.util.Arrays.asList(
        TeaConverter.buildMap(
            new TeaPair("TagKey", "key1"),
            new TeaPair("TagValue", "value1")
        )
    )) // 資源標籤
);
// 請求參數
com.aliyun.cloudcontrol20220830.models.UpdateResourceRequest updateResourceRequest = new com.aliyun.cloudcontrol20220830.models.UpdateResourceRequest()
    .setRegionId("cn-qingdao")
    .setBody(body);
// 發起請求
UpdateResourceResponse updateResourceResponse = client.updateResourceWithOptions(requestPath, updateResourceRequest, headers, runtime);

完整範例程式碼

import com.aliyun.cloudcontrol20220830.Client;
import com.aliyun.cloudcontrol20220830.models.*;
import com.aliyun.tea.TeaConverter;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaPair;
import com.google.gson.Gson;

public class Sample {

    private static final String PRODUCT_CODE = "VPC"; // 產品code,此例中為"VPC"
    private static final String RESOURCE_TYPE_CODE = "VPC"; // 資源類型code,此例中為"VPC"
    public static void main(String[] args) throws Exception {
        //      初始化用戶端
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
            .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
            .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = "cloudcontrol.aliyuncs.com";
        com.aliyun.cloudcontrol20220830.Client client = new com.aliyun.cloudcontrol20220830.Client(config);

        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        //          headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
        java.util.Map < String, String > headers = new java.util.HashMap < > ();

        // 資源ID
        String resourceId = "vpc-m5eml8m3XXXXXXXX";
        // 請求路徑
        String requestPath = "/api/v1/providers/Aliyun/products/" + PRODUCT_CODE + "/resources/" + RESOURCE_TYPE_CODE +"/" + resourceId;
        java.util.Map < String, Object > body = TeaConverter.buildMap(
            new TeaPair("Description", "更新測試VPC描述"), // 更新資源描述
            new TeaPair("VpcName", "UpdateVpc"), // 更新資源名稱
            new TeaPair("Tags", java.util.Arrays.asList(
                TeaConverter.buildMap(
                    new TeaPair("TagKey", "key1"),
                    new TeaPair("TagValue", "value1")
                )
            )) // 更新資源標籤
        );
        com.aliyun.cloudcontrol20220830.models.UpdateResourceRequest updateResourceRequest = new com.aliyun.cloudcontrol20220830.models.UpdateResourceRequest()
            .setRegionId("cn-qingdao") // 地區
            .setBody(body);
        try {
            // 發起請求 
            UpdateResourceResponse updateResourceResponse = client.updateResourceWithOptions(requestPath, updateResourceRequest, headers, runtime);
            System.out.println(new Gson().toJson(updateResourceResponse.getBody()) + new Gson().toJson(updateResourceResponse.statusCode));
        } catch (TeaException error) {
            // 錯誤 message
            System.out.println(error.getMessage());
            // 診斷地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 錯誤 message
            System.out.println(error.getMessage());
            // 診斷地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

此次請求返回statusCode=200,表明請求成功。為驗證更新結果,請調用GetResources介面查看資源詳情。響應結果如下:

{
    "requestId": "3A6086A7-FD3E-5C16-8B96-A236A10B600F"
}

介面名稱:DeleteResource

此介面目的是用於刪除指定的Virtual Private Cloud資源。有關介面詳細資料,請參見資源刪除 API文檔。在調用過程中,您需根據實際業務需求設定相應的請求參數及回合組態。

重要

刪除操作為無法復原操作,務必謹慎處理。在執行刪除VPC操作之前,建議首先在測試環境中對刪除流程進行全面驗證,確保無誤後再進行生產資源的操作。

// 配置運行時參數
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
//        // 忽略對 SSL 憑證的驗證
//        runtime.ignoreSSL = true;
//        // 代理配置
//        runtime.httpProxy = "http://127.0.0.1:9898";
//        runtime.httpsProxy = "http://user:password@127.0.0.1:8989";
//        runtime.noProxy = "127.0.0.1,localhost";
//        // 連線逾時
//        runtime.connectTimeout = 5000;
//        // 讀逾時
//        runtime.readTimeout = 10000;
//        // 開啟自動重試機制
//        runtime.autoretry = true;
//        // 設定自動重試次數
//        runtime.maxAttempts = 3;

//          headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
java.util.Map < String, String > headers = new java.util.HashMap < > ();
//        headers.put("x-acs-action", "DeleteResource"); // 設定介面名稱

// 資源ID
String resourceId = "vpc-m5ei5xmtXXXXXXXX";
// 請求路徑
String requestPath = "/api/v1/providers/Aliyun/products/" + PRODUCT_CODE + "/resources/" + RESOURCE_TYPE_CODE + "/" + resourceId;
com.aliyun.cloudcontrol20220830.models.DeleteResourceRequest deleteResourceRequest = new com.aliyun.cloudcontrol20220830.models.DeleteResourceRequest()
    .setRegionId("cn-qingdao");
// 發起請求
DeleteResourceResponse deleteResourceResponse = client.deleteResourceWithOptions(requestPath, deleteResourceRequest, headers, runtime);

完整程式碼範例

import com.aliyun.cloudcontrol20220830.Client;
import com.aliyun.cloudcontrol20220830.models.*;
import com.aliyun.tea.TeaConverter;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaPair;
import com.google.gson.Gson;

public class Sample {

    private static final String PRODUCT_CODE = "VPC"; // 產品code,此例中為"VPC"
    private static final String RESOURCE_TYPE_CODE = "VPC"; // 資源類型code,此例中為"VPC"
    public static void main(String[] args) throws Exception {
        //      初始化用戶端
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
            .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
            .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = "cloudcontrol.aliyuncs.com";
        com.aliyun.cloudcontrol20220830.Client client = new com.aliyun.cloudcontrol20220830.Client(config);

        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        //          headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
        java.util.Map < String, String > headers = new java.util.HashMap < > ();
        // 資源ID
        String resourceId = "vpc-m5ei5XXXXXXXX";
        // 請求路徑
        String requestPath = "/api/v1/providers/Aliyun/products/" + PRODUCT_CODE + "/resources/" + RESOURCE_TYPE_CODE + "/" + resourceId;
        // 請求參數
        com.aliyun.cloudcontrol20220830.models.DeleteResourceRequest deleteResourceRequest = new com.aliyun.cloudcontrol20220830.models.DeleteResourceRequest()
            .setRegionId("cn-qingdao");
        try {
            // 發起請求
            DeleteResourceResponse deleteResourceResponse = client.deleteResourceWithOptions(requestPath, deleteResourceRequest, headers, runtime);
            // 列印結果
            System.out.println(new Gson().toJson(deleteResourceResponse.getBody()));
        } catch (TeaException error) {
            // 錯誤 message
            System.out.println(error.getMessage());
            // 診斷地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 錯誤 message
            System.out.println(error.getMessage());
            // 診斷地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

此次請求返回statusCode = 202表明Virtual Private Cloud刪除成功,該過程為非同步作業。為進一步查詢任務狀態,需記錄taskId,並調用GetTask介面擷取任務詳細資料。響應結果為:

{
    "requestId": "699C832C-5854-5F22-966D-7D9A6F8AFE85",
    "taskId": "task-5057a4fbef2XXXXXXXX"
}

介面名稱:GetTask

此介面目的是用於查詢任務詳情。有關介面詳細資料,請參見查詢任務 API文檔。在調用過程中,您需根據實際業務需求設定相應的請求參數及回合組態。

// 運行時配置
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
//        // 忽略對 SSL 憑證的驗證
//        runtime.ignoreSSL = true;
//        // 代理配置
//        runtime.httpProxy = "http://127.0.0.1:9898";
//        runtime.httpsProxy = "http://user:password@127.0.0.1:8989";
//        runtime.noProxy = "127.0.0.1,localhost";
//        // 連線逾時
//        runtime.connectTimeout = 5000;
//        // 讀逾時
//        runtime.readTimeout = 10000;
//        // 開啟自動重試機制
//        runtime.autoretry = true;
//        // 設定自動重試次數
//        runtime.maxAttempts = 3;

//          headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
java.util.Map < String, String > headers = new java.util.HashMap < > ();
//        headers.put("x-acs-action", "GetTask"); // 設定介面名稱
// 任務ID
String taskId = "task-5057a4fbXXXXXXXX";
// 發起請求
GetTaskResponse getTaskResponse = client.getTaskWithOptions(taskId, headers, runtime);

完整程式碼範例

import com.aliyun.cloudcontrol20220830.Client;
import com.aliyun.cloudcontrol20220830.models.*;
import com.aliyun.tea.TeaConverter;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaPair;
import com.google.gson.Gson;

public class Sample {

    private static final String PRODUCT_CODE = "VPC"; // 產品code,此例中為"VPC"
    private static final String RESOURCE_TYPE_CODE = "VPC"; // 資源類型code,此例中為"VPC"
    public static void main(String[] args) throws Exception {
        //      初始化用戶端
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
            .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
            .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = "cloudcontrol.aliyuncs.com";
        com.aliyun.cloudcontrol20220830.Client client = new com.aliyun.cloudcontrol20220830.Client(config);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        //          headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
        java.util.Map < String, String > headers = new java.util.HashMap < > ();
        // 任務ID
        String taskId = "task-5057a4fXXXXXXXX";
        try {
            // 發起請求
            GetTaskResponse getTaskResponse = client.getTaskWithOptions(taskId, headers, runtime);
            // 列印結果
            System.out.println(new Gson().toJson(getTaskResponse.getBody()));
        } catch (TeaException error) {
            // 錯誤 message
            System.out.println(error.getMessage());
            // 診斷地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 錯誤 message
            System.out.println(error.getMessage());
            // 診斷地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

此次請求返回status為Succeeded說明請求成功,響應結果如下:

{
    "requestId": "EC7940A9-52A4-5B9D-97BF-98BE2B75A727",
    "task": {
        "createTime": "2025-04-24T07:19:22Z",
        "product": "VPC",
        "regionId": "cn-qingdao",
        "resourceId": "vpc-m5eml8XXXXXXXX",
        "resourcePath": "VPC/vpc-m5eml8m3XXXXXXXX",
        "resourceType": "VPC",
        "status": "Succeeded",
        "taskAction": "Create",
        "taskId": "task-5057a4fbefXXXXXXXX"
    }
}

常見問題

報錯提示 Cannot invoke "com.aliyun.credentials.Client.getCredential()" because "this._credential" is null。

問題現象:代碼運行時報錯,報錯資訊中包含以上資訊時,表示AK未正確地設定阿里雲的憑證

解決方案:

  1. 執行以下命令,檢查您的環境變數中是否配置有ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET:

    Linux/macOS

    echo $ALIBABA_CLOUD_ACCESS_KEY_ID
    echo $ALIBABA_CLOUD_ACCESS_KEY_SECRET

    Windows

    echo %ALIBABA_CLOUD_ACCESS_KEY_ID%
    echo %ALIBABA_CLOUD_ACCESS_KEY_SECRET%

    若返回正確的AccessKey,則說明配置成功。如果返回為空白或錯誤,請嘗試重新設定,具體操作請參見在Linux、macOS和Windows系統配置環境變數

  2. 檢查代碼中AK相關內容是否存在錯誤。

    常見的錯誤樣本:

    Config config = new Config()
             .setAccessKeyId(System.getenv("yourAccessKeyID"))   
             .setAccessKeySecret(System.getenv("yourAccessKeySecret")); 

    正確樣本:

    Config config = new Config()
            .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
            .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
    說明

    System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")和System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),表示的是從環境變數中擷取ALIBABA_CLOUD_ACCESS_KEY_ID及ALIBABA_CLOUD_ACCESS_KEY_SECRET的值。

    重要

    切勿直接線上上代碼中明文寫入 AccessKey,該寫法存在安全隱患。