すべてのプロダクト
Search
ドキュメントセンター

Cloud Control API:Cloud Control API と Java SDK を使用したリソース管理

最終更新日:Apr 06, 2026

このチュートリアルでは、Alibaba Cloud SDK for Java を使用して Cloud Control API を呼び出し、リソース管理の完全なライフサイクル (作成、読み取り、更新、削除 (CRUD)) を実行する方法を説明します。このチュートリアルでは、Virtual Private Cloud (VPC) を例として、Cloud Control API が使用する非同期タスクの処理方法について説明します。

前提条件

  • Java SDK がインストールされ、設定済みであること。

  • リソースのプロパティと構造を定義するリソースメタデータの概念を理解していること。詳細については、「リソースメタデータ」をご参照ください。

  • Cloud Control API が頻繁に非同期操作を使用することを理解していること。詳細については、「同期操作と非同期操作」をご参照ください。

ステップ 1:リソースタイプのメタデータの取得

リソースを管理する前に、まずそのメタデータを取得して構造を理解する必要があります。これは、`GetResourceType` 操作を呼び出すことで行われます。次の例では、VPC のメタデータを取得します。

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"; // プロダクトコード。この例では "VPC"
    private static final String RESOURCE_TYPE_CODE = "VPC"; // リソースタイプコード。この例では "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("en-US"); // 応答の言語を選択します。zh-CN:中国語 (デフォルト)、en-US:英語。
        try {
            // リクエストを送信します。
            GetResourceTypeResponse getResourceTypeResponse = client.getResourceTypeWithOptions(requestPath, getResourceTypeHeaders, runtime);
            // 応答結果を出力します。
            System.out.println(new Gson().toJson(getResourceTypeResponse.getBody()));
        } catch (TeaException error) {
            // エラーメッセージ。
            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);
            // エラーメッセージ。
            System.out.println(error.getMessage());
            // 診断アドレス。
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

応答には、VPC リソースの完全なメタデータが含まれます。これには、プロパティ、作成に必須のパラメーター (createOnlyProperties)、読み取り専用プロパティ (readOnlyProperties)、および各操作 (handlers) に必要な権限が含まれます。この情報は、後続のステップで使用します。

応答例:

{
    "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": "A VPC instance represents a virtual private cloud that you created. You have full control over your VPC, such as specifying the IP address range, configuring route tables, and setting up gateways. You can use Alibaba Cloud resources such as Elastic Compute Service (ECS) instances, ApsaraDB RDS instances, and Server Load Balancer (SLB) instances within your VPC.",
            "title": "Virtual Private Cloud"
        },
        "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": "The status of the VPC.",
                "readOnly": true,
                "sensitive": false,
                "title": "The status of the VPC.",
                "type": "string",
                "updateType": false
            },
            "IsDefault": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "description": "Indicates whether it is the default VPC.",
                "readOnly": false,
                "sensitive": false,
                "title": "Indicates whether it is the default VPC.",
                "type": "boolean",
                "updateType": false
            },
            "Ipv6Isp": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "pattern": "",
                "description": "The IPv6 CIDR block type for the VPC. Valid values:\n\n- **BGP** (default): Alibaba Cloud BGP IPv6.\n- **ChinaMobile**: China Mobile (single line).\n- **ChinaUnicom**: China Unicom (single line).\n- **ChinaTelecom**: China Telecom (single line).\n\n\u003e This field can be set to **ChinaTelecom**, **ChinaUnicom**, or **ChinaMobile** if you are on the single-line bandwidth whitelist.",
                "operatePrivateType": [
                    "create"
                ],
                "readOnly": false,
                "sensitive": false,
                "title": "The IPv6 CIDR block type for the VPC. Valid values:\n\n- **BGP** (default): Alibaba Cloud BGP IPv6.\n- **ChinaMobile**: China Mobile (single line).\n- **ChinaUnicom**: China Unicom (single line).\n- **ChinaTelecom**: China Telecom (single line).\n\n\u003e This field can be set to **ChinaTelecom**, **ChinaUnicom**, or **ChinaMobile** if you are on the single-line bandwidth whitelist.",
                "type": "string",
                "updateType": false
            },
            "Description": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "The description of the VPC.",
                "readOnly": false,
                "sensitive": false,
                "title": "The description of the VPC.",
                "type": "string",
                "updateType": true
            },
            "ResourceGroupId": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "The ID of the resource group.",
                "readOnly": false,
                "sensitive": false,
                "title": "The ID of the resource group.",
                "type": "string",
                "updateType": true
            },
            "ClassicLinkEnabled": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "description": "The status of the ClassicLink feature.",
                "readOnly": false,
                "sensitive": false,
                "title": "The status of the ClassicLink feature.",
                "type": "boolean",
                "updateType": true
            },
            "SecondaryCidrBlocks": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "The secondary CIDR block information.",
                "readOnly": false,
                "sensitive": false,
                "title": "The secondary CIDR block information.",
                "type": "array",
                "items": {
                    "description": "The secondary CIDR block of the VPC.",
                    "title": "The secondary CIDR block of the VPC.",
                    "type": "string",
                    "updateType": true
                },
                "updateType": true
            },
            "VSwitchIds": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "description": "The list of VSwitches in the VPC.",
                "readOnly": true,
                "sensitive": false,
                "title": "The list of VSwitches in the VPC.",
                "type": "array",
                "items": {
                    "extMonitorInfo": false,
                    "deprecated": false,
                    "pattern": "",
                    "description": "The list of VSwitches in the VPC.",
                    "readOnly": true,
                    "sensitive": false,
                    "title": "The list of VSwitches in the VPC.",
                    "type": "string",
                    "updateType": false
                },
                "updateType": false
            },
            "CreateTime": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "pattern": "",
                "description": "The time when the VPC was created.",
                "readOnly": true,
                "sensitive": false,
                "title": "The time when the VPC was created.",
                "type": "string",
                "updateType": false
            },
            "DryRun": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "description": "Indicates whether to perform a dry run. Valid values:\n- **true**: Sends a check request without creating the VPC. The system checks for required parameters, request format, and service limits. If the check fails, an error is returned. If the check passes, the `DryRunOperation` error code is returned.\n- **false** (default): Sends a normal request, and if the check passes, returns a 2xx HTTP status code and creates the VPC.",
                "operatePrivateType": [
                    "create",
                    "list",
                    "get"
                ],
                "readOnly": false,
                "sensitive": false,
                "title": "Indicates whether to perform a dry run. Valid values:\n- **true**: Sends a check request without creating the VPC. The system checks for required parameters, request format, and service limits. If the check fails, an error is returned. If the check passes, the `DryRunOperation` error code is returned.\n- **false** (default): Sends a normal request, and if the check passes, returns a 2xx HTTP status code and creates the VPC.",
                "type": "boolean",
                "updateType": false
            },
            "RouterId": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "pattern": "",
                "description": "The router ID of the VPC.",
                "readOnly": true,
                "sensitive": false,
                "title": "The router ID of the VPC.",
                "type": "string",
                "updateType": false
            },
            "CidrBlock": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "The private CIDR block of the VPC.",
                "readOnly": false,
                "sensitive": false,
                "title": "The private CIDR block of the VPC.",
                "type": "string",
                "updateType": true
            },
            "UserCidrs": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "The CIDR block of the user-side network. To specify multiple CIDR blocks, separate them with commas. Up to 3 CIDR blocks are supported.",
                "readOnly": false,
                "sensitive": false,
                "title": "The CIDR block of the user-side network. To specify multiple CIDR blocks, separate them with commas. Up to 3 CIDR blocks are supported.",
                "type": "array",
                "items": {
                    "isRequired": false,
                    "extMonitorInfo": false,
                    "deprecated": false,
                    "pattern": "",
                    "description": "The CIDR block of the user-side network. To specify multiple CIDR blocks, separate them with commas. Up to 3 CIDR blocks are supported.",
                    "sensitive": false,
                    "title": "The CIDR block of the user-side network. To specify multiple CIDR blocks, separate them with commas. Up to 3 CIDR blocks are supported.",
                    "type": "string",
                    "updateType": false
                },
                "updateType": false
            },
            "EnableIpv6": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "description": "Indicates whether to enable the IPv6 CIDR block. Valid values:\n\n- **false** (default): No.\n- **true**: Yes.",
                "operatePrivateType": [
                    "create"
                ],
                "readOnly": false,
                "sensitive": false,
                "title": "Indicates whether to enable the IPv6 CIDR block. Valid values:\n\n- **false** (default): No.\n- **true**: Yes.",
                "type": "boolean",
                "updateType": false
            },
            "DhcpOptionsSetId": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "The ID of the DHCP options set.",
                "readOnly": false,
                "sensitive": false,
                "title": "The ID of the DHCP options set.",
                "type": "string",
                "updateType": false
            },
            "VpcId": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "The ID of the VPC.",
                "readOnly": false,
                "sensitive": false,
                "title": "The ID of the VPC.",
                "type": "string",
                "updateType": false
            },
            "VpcName": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "The name of the VPC.",
                "readOnly": false,
                "sensitive": false,
                "title": "The name of the VPC.",
                "type": "string",
                "updateType": true
            },
            "RegionId": {
                "isRequired": true,
                "extMonitorInfo": false,
                "description": "The region where the VPC is located.",
                "readOnly": false,
                "sensitive": false,
                "title": "The region where the VPC is located.",
                "type": "string",
                "updateType": false
            },
            "Ipv6CidrBlocks": {
                "isRequired": false,
                "extMonitorInfo": false,
                "deprecated": false,
                "description": "The IPv6 CIDR block information of the VPC.",
                "readOnly": true,
                "sensitive": false,
                "title": "The IPv6 CIDR block information of the VPC.",
                "type": "array",
                "items": {
                    "extMonitorInfo": false,
                    "deprecated": false,
                    "description": "The IPv6 CIDR block information of the VPC.",
                    "readOnly": true,
                    "sensitive": false,
                    "title": "The IPv6 CIDR block information of the VPC.",
                    "type": "object",
                    "properties": {
                        "Ipv6Isp": {
                            "isRequired": false,
                            "extMonitorInfo": false,
                            "deprecated": false,
                            "pattern": "",
                            "description": "The IPv6 CIDR block type of the VPC. Valid values:\n\n- **BGP**: Alibaba Cloud BGP IPv6.\n- **ChinaMobile**: China Mobile (single line).\n- **ChinaUnicom**: China Unicom (single line).\n- **ChinaTelecom**: China Telecom (single line).\n\n\u003e This field can be set to **ChinaTelecom**, **ChinaUnicom**, and **ChinaMobile** if you are on the single-line bandwidth whitelist.",
                            "readOnly": true,
                            "sensitive": false,
                            "title": "The IPv6 CIDR block type of the VPC. Valid values:\n\n- **BGP**: Alibaba Cloud BGP IPv6.\n- **ChinaMobile**: China Mobile (single line).\n- **ChinaUnicom**: China Unicom (single line).\n- **ChinaTelecom**: China Telecom (single line).\n\n\u003e This field can be set to **ChinaTelecom**, **ChinaUnicom**, and **ChinaMobile** if you are on the single-line bandwidth whitelist.",
                            "type": "string",
                            "updateType": false
                        },
                        "Ipv6CidrBlock": {
                            "isRequired": false,
                            "extMonitorInfo": false,
                            "deprecated": false,
                            "pattern": "",
                            "description": "The IPv6 CIDR block of the VPC.",
                            "readOnly": true,
                            "sensitive": false,
                            "title": "The IPv6 CIDR block of the VPC.",
                            "type": "string",
                            "updateType": false
                        }
                    },
                    "updateType": false
                },
                "updateType": false
            },
            "Ipv6CidrBlock": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "The IPv6 address.",
                "readOnly": false,
                "sensitive": false,
                "title": "The IPv6 address.",
                "type": "string",
                "updateType": true
            },
            "Tags": {
                "isRequired": false,
                "extMonitorInfo": false,
                "description": "The tags of the VPC.",
                "readOnly": false,
                "sensitive": false,
                "title": "The tags of the VPC.",
                "type": "array",
                "items": {
                    "extMonitorInfo": false,
                    "deprecated": false,
                    "description": "The tags of the VPC.",
                    "sensitive": false,
                    "title": "The tags of the VPC.",
                    "type": "object",
                    "properties": {
                        "TagKey": {
                            "isRequired": false,
                            "extMonitorInfo": false,
                            "description": "The key of the tag.",
                            "readOnly": false,
                            "sensitive": false,
                            "title": "The key of the tag.",
                            "type": "string",
                            "updateType": true
                        },
                        "TagValue": {
                            "isRequired": false,
                            "extMonitorInfo": false,
                            "description": "The value of the tag.",
                            "readOnly": false,
                            "sensitive": false,
                            "title": "The value of the tag.",
                            "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"
        ]
    }
}

ステップ 2:リソースの作成

`CreateResource` 操作を使用して、新しいリソースを作成します。リクエストボディには、リソースメタデータで定義されたプロパティを含める必要があります。この操作は非同期であり、進行状況を追跡するために使用する taskId を返します。

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"; // プロダクトコード。この例では "VPC"。
    private static final String RESOURCE_TYPE_CODE = "VPC"; // リソースタイプコード。この例では "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", "This is a test 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 CIDR ブロックを有効にするかどうかを指定します。
            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) {
            // エラーメッセージ。
            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);
            // エラーメッセージ。
            System.out.println(error.getMessage());
            // 診断アドレス。
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

ステップ 3:非同期タスクのステータスの確認

CreateResource や DeleteResource のような非同期操作を開始した後、返された taskId を使用して `GetTask` 操作を呼び出し、そのステータスを確認します。

応答例:

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

ステップ 4:リソースの読み取り、更新、削除

リソースが作成されると、その resourceId を使用して詳細の読み取り、プロパティの更新、または削除ができます。

単一リソースの読み取り (`GetResources`)

特定のリソースの現在の状態を取得するには、その resourceId をリクエストパスに追加します。

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"; // プロダクトコード。この例では "VPC"。
    private static final String RESOURCE_TYPE_CODE = "VPC"; // リソースタイプコード。この例では "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) {
            // エラーメッセージ。
            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);
            // エラーメッセージ。
            System.out.println(error.getMessage());
            // 診断アドレス。
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

すべてのリソースの一覧表示 (`GetResources`)

リージョン内の特定のタイプのリソースをすべて一覧表示するには、ベースリソースパスにリクエストを送信します。フィルターを追加することもできます。

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"; // プロダクトコード。この例では "VPC"。
    private static final String RESOURCE_TYPE_CODE = "VPC"; // リソースタイプコード。この例では "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"); // API オペレーション名を設定します。
        //       リクエストパス。
        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) {
            // エラーメッセージ。
            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);
            // エラーメッセージ。
            System.out.println(error.getMessage());
            // 診断アドレス。
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

リソースの更新 (`UpdateResource`)

パスに resourceId を、リクエストボディに変更するプロパティを指定します。

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"; // プロダクトコード。この例では "VPC"。
    private static final String RESOURCE_TYPE_CODE = "VPC"; // リソースタイプコード。この例では "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", "Updated test VPC description"), // リソースの説明を更新します。
            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) {
            // エラーメッセージ。
            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);
            // エラーメッセージ。
            System.out.println(error.getMessage());
            // 診断アドレス。
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

リソースの削除 (`DeleteResource`)

リクエストパスに resourceId を指定します。作成と同様に、これは追跡用の taskId を返す非同期操作です。

重要

削除は不可逆的な操作です。注意して実行してください。

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"; // プロダクトコード。この例では "VPC"。
    private static final String RESOURCE_TYPE_CODE = "VPC"; // リソースタイプコード。この例では "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) {
            // エラーメッセージ。
            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);
            // エラーメッセージ。
            System.out.println(error.getMessage());
            // 診断アドレス。
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

よくある質問

エラー:Cannot invoke "com.aliyun.credentials.Client.getCredential()" because "this._credential" is null

このエラーは、SDK が Alibaba Cloud の認証情報 (AccessKey ペア) を見つけられなかったことを示します。

解決策:

  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 ID が出力されます。出力が空の場合、変数が正しく設定されていないため、再設定する必要があります。

    詳細については、「Linux、macOS、Windows で環境変数を設定する」をご参照ください。

  2. 初期化コードを確認します。クライアントの初期化コードが環境変数を正しく読み取っているか、または有効な認証情報プロバイダーで設定されていることを確認してください。

    重要

    ソースコードに AccessKey ペアを直接ハードコーディングしないでください。これは重大なセキュリティリスクです。