全部产品
Search
文档中心

云控制API:通过SDK调用云控制API以实现资源管理

更新时间:Feb 05, 2026

本文演示如何通过Java SDK调用云控制API实现资源管理。以专有网络VPC为例,展示SDK的集成过程,以及查询资源元数据、创建、查询、更新和删除资源的具体操作。此外,还将介绍在云控制API采用异步操作处理任务时,如何查询任务状态。

前提条件

为确保顺利操作,请完成以下前置准备:

  • 已集成SDK,具体操作请参见Java集成SDK

  • 资源管理基于资源元数据进行操作,因此熟悉资源元数据是高效使用云控制 API 的关键。资源元数据定义了资源的属性、状态及其他相关信息。具体信息,请参见资源元数据

  • 云控制API处理任务时采用异步操作,因此您需要了解同步操作与异步操作的基本概念。关于查询任务状态的具体信息,请参见教程中的GetTask接口。

  • 请求路径具体信息,请参见请求路径说明

调用云控制API以实现资源管理

本文教程以专有网络VPC为示例,其中专有网络VPC的产品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

此接口的目的是创建一个专有网络VPC资源。有关接口详细信息,请参见创建资源 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表明专有网络VPC创建成功,该过程为异步操作。为进一步查询任务状态,需记录taskId,并调用GetTask接口获取任务详细信息。同时,记录resourceId,用于后续步骤中的查询资源、更新资源与删除资源等操作。响应结果如下:

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

接口名称:GetResources

此接口的目的是获取一个专有网络VPC资源详细信息。有关接口详细信息,请参见获取资源详情 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);
        }
    }
}

此次请求返回结果为专有网络VPC资源的详细信息。响应结果如下:

{
    "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

此接口的目的是列举所有专有网络VPC资源信息。有关接口详细信息,请参见 列举资源 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

此接口的目的是更新专有网络VPC资源信息。有关接口详细信息,请参见更新资源 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

此接口目的是用于删除指定的专有网络VPC资源。有关接口详细信息,请参见资源删除 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表明专有网络VPC删除成功,该过程为异步操作。为进一步查询任务状态,需记录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,该写法存在安全隐患。