All Products
Search
Document Center

Cloud Control API:Use an SDK to manage resources

Last Updated:Mar 13, 2025

This topic describes how to use an SDK to call the Cloud Control API to manage resources. In this topic, the vSwitch in a Virtual Private Cloud (VPC) is used as an example and the sample code is written in Java. This tutorial walks you through the procedure to import SDK, query resource metadata, create, query, update, and delete resources, and query task status when asynchronous operation is enabled for Cloud Control API operation.

Prerequisites

  1. RAM user:

    • Create an AccessKey pair for a RAM user. An Alibaba Cloud account has all permissions on resources. If the AccessKey pair of your Alibaba Cloud account is leaked, your resources are exposed to great risks. We recommend that you use the AccessKey pair of a RAM user. For more information, see Create an AccessKey pair.

    • Grant the required permissions on VPC resources to the RAM user that you want to use. This operation instruction involves a variety of operations on the vSwitch in a VPC. You must select AliyunVPCFullAccess as system policy. You can create a custom policy based on your business requirements. For more information, see Create a custom policy.

    • The AccessKey pair of a RAM user is configured in environment variables. For more information, see Configure environment variables in Linux, macOS, and Windows.

  2. VPC

    You must create a VPC to which the vSwitch is associated. For more information, see Create and manage a VPC.

  3. Resource metadata

    Resource management is implemented based on resource metadata. You must be familiar with Resource metadata.

  4. Synchronous and asynchronous operations

    Resource creation and deletion are not performed synchronously with Cloud Control API operations. For more information, see synchronous and asynchronous operations. If you want to query the task status when asynchronous operation is enabled for Cloud Control API, see Query task status.

  5. Java

    You must use Java 8 or later.

Initialize a project

Install CloudControlAPI SDK to obtain the AccessKey in the environment variable.

  1. Import the SDK to your project. For more information, see Install CloudControlAPI SDK. You can use an SDK to integrate and use the Cloud Control API. In this example, an SDK is imported as follows:

    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>cloudcontrol20220830</artifactId>
        <version>1.1.1</version>
    </dependency>
  2. Create an ExampleDemo to implement resource management. Add the createClient method to the ExampleDemo to obtain the AccessKey pair in the environment variable and initialize the client. Sample code:

    /**
     * Initialize the client.
     * @return Client
     */
    public static Client createClient() throws Exception {
        // If the project code is leaked, the AccessKey pair may be leaked and the security of all resources in your account may be compromised. The following sample code is provided only for reference. 
        Config config = new Config()
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured. 
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured. 
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        # For more information about endpoints, see https://api.aliyun.com/product/cloudcontrol.
        config.endpoint = "cloudcontrol.aliyuncs.com";
        return new Client(config);
    }

Query the details of a resource type

You can query the details of a resource type based on product code and resource type code. The returned data is resource metadata. Subsequent resource management operations such as creation, query, listing, and deletion are implemented based on resource metadata.

  1. You can call the GetResourceType operation to obtain the details of a resource type. In this example, the vSwitch in a VPC is used. The product code of the VPC is VPC, and the resource type code of the vSwitch is vSwitch. Sample code:

    Note

    For more information about how to obtain the product code and resource code, see Where can I find the service code and resource type code?

    /**
     * Query the details of a resource type.
     * @param productCode      The product code, which is "VPC" in this example.
     * @param resourceTypeCode The resource type code, which is "vSwitch" in this example.
     * @param client           The client
     * @return resourceTypeWithOptions The response
     */
    public GetResourceTypeResponse getResourceType(String productCode, String resourceTypeCode, Client client) throws Exception {
        String requestPath = "/api/v1/providers/Aliyun/products/" + productCode + "/resourceTypes/" + resourceTypeCode;
        com.aliyun.cloudcontrol20220830.models.GetResourceTypeHeaders getResourceTypeHeaders = new com.aliyun.cloudcontrol20220830.models.GetResourceTypeHeaders()
                .setXAcsAcceptLanguage("zh_CH");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        // Write your own code to display the response of the API operation if necessary.
        GetResourceTypeResponse resourceTypeWithOptions = client.getResourceTypeWithOptions(requestPath, getResourceTypeHeaders, runtime);
        return resourceTypeWithOptions;
    }
  2. The resource metadata whose response body is vSwitch is returned. For more information about how to interpret the resource metadata, see GetResourceType - Query resource type. In this operation, the body of the response is:

    resourceTypeWithOptions.getBody()

    {
      "requestId": "DF147125-4F39-57EF-8E9C-554FE0E3AB77",
      "resourceType": {
        "product": "VPC",
        "listResponseProperties": [
          "/properties/VSwitchName",
          "/properties/ResourceGroupId",
          "/properties/CidrBlock",
          "/properties/Tags/items",
          "/properties/Description",
          "/properties/VSwitchId",
          "/properties/Status",
          "/properties/Tags/items/properties/TagValue",
          "/properties/Ipv6CidrBlock",
          "/properties/CreateTime",
          "/properties/RouteTableId",
          "/properties/VpcId",
          "/properties/Tags/items/properties/TagKey",
          "/properties/ZoneId",
          "/properties/Tags",
          "/properties/IsDefault",
          "/properties/AvailableIpAddressCount"
        ],
        "updateOnlyProperties": [
          
        ],
        "readOnlyProperties": [
          "/properties/Status",
          "/properties/CreateTime",
          "/properties/AvailableIpAddressCount"
        ],
        "createOnlyProperties": [
          "/properties/VpcIpv6CidrBlock"
        ],
        "listOnlyProperties": [
          
        ],
        "primaryIdentifier": "/properties/VSwitchId",
        "getOnlyProperties": [
          
        ],
        "required": [
          "CidrBlock",
          "VpcId",
          "ZoneId"
        ],
        "publicProperties": [
          "/properties/RouteTableId",
          "/properties/CidrBlock",
          "/properties/Description",
          "/properties/VSwitchId",
          "/properties/AvailableIpAddressCount",
          "/properties/CreateTime",
          "/properties/VSwitchName",
          "/properties/ResourceGroupId",
          "/properties/VpcId",
          "/properties/ZoneId",
          "/properties/Status",
          "/properties/RegionId",
          "/properties/IsDefault",
          "/properties/Ipv6CidrBlock",
          "/properties/Tags",
          "/properties/Tags/items",
          "/properties/Tags/items/properties/TagValue",
          "/properties/Tags/items/properties/TagKey"
        ],
        "filterProperties": [
          "/properties/VSwitchName",
          "/properties/ResourceGroupId",
          "/properties/RouteTableId",
          "/properties/VpcId",
          "/properties/VSwitchId",
          "/properties/ZoneId",
          "/properties/IsDefault"
        ],
        "getResponseProperties": [
          "/properties/ResourceGroupId",
          "/properties/CidrBlock",
          "/properties/VSwitchName",
          "/properties/Tags/items",
          "/properties/Description",
          "/properties/VSwitchId",
          "/properties/Status",
          "/properties/Tags/items/properties/TagValue",
          "/properties/Ipv6CidrBlock",
          "/properties/CreateTime",
          "/properties/RouteTableId",
          "/properties/VpcId",
          "/properties/Tags/items/properties/TagKey",
          "/properties/ZoneId",
          "/properties/Tags",
          "/properties/IsDefault",
          "/properties/AvailableIpAddressCount"
        ],
        "handlers": {
          "get": {
            "permissions": [
              "vpc:DescribeVSwitchAttributes",
              "vpc:ListTagResources"
            ]
          },
          "create": {
            "permissions": [
              "vpc:CreateVSwitch"
            ]
          },
          "update": {
            "permissions": [
              "vpc:TagResources",
              "vpc:UnTagResources",
              "vpc:ModifyVSwitchAttribute"
            ]
          },
          "list": {
            "permissions": [
              "vpc:DescribeVSwitches"
            ]
          },
          "delete": {
            "permissions": [
              "vpc:DeleteVSwitch"
            ]
          }
        },
        "deleteOnlyProperties": [
          
        ],
        "updateTypeProperties": [
          "/properties/Description",
          "/properties/VSwitchName",
          "/properties/Ipv6CidrBlock",
          "/properties/Tags",
          "/properties/Tags/items",
          "/properties/Tags/items/properties/TagValue",
          "/properties/Tags/items/properties/TagKey"
        ],
        "properties": {
          "Status": {
            "isRequired": false,
            "extMonitorInfo": false,
            "default": "",
            "deprecated": false,
            "pattern": "",
            "description": "The resource attribute field that represents the resource status",
            "readOnly": true,
            "sensitive": false,
            "title": "The resource status",
            "type": "string",
            "updateType": false
          },
          "IsDefault": {
            "isRequired": false,
            "extMonitorInfo": false,
            "default": "",
            "deprecated": false,
            "description": "Indicates whether the vSwitch is the default vSwitch in the zone",
            "readOnly": true,
            "sensitive": false,
            "title": "Indicates whether the vSwitch is the default vSwitch in the zone",
            "type": "boolean",
            "updateType": false
          },
          "RouteTableId": {
            "isRequired": false,
            "extMonitorInfo": false,
            "default": "",
            "deprecated": false,
            "pattern": "",
            "description": "The ID of the route table that is associated with the vSwitch",
            "readOnly": true,
            "sensitive": false,
            "title": "The ID of the route table that is associated with the vSwitch",
            "type": "string",
            "updateType": false
          },
          "Description": {
            "isRequired": false,
            "extMonitorInfo": false,
            "description": "The description of the vSwitch",
            "readOnly": false,
            "sensitive": false,
            "title": "The description of the vSwitch",
            "type": "string",
            "updateType": true
          },
          "ResourceGroupId": {
            "isRequired": false,
            "extMonitorInfo": false,
            "default": "",
            "deprecated": false,
            "pattern": "",
            "description": "The ID of the resource group to which the vSwitch belongs",
            "readOnly": true,
            "sensitive": false,
            "title": "The ID of the resource group to which the vSwitch belongs",
            "type": "string",
            "updateType": false
          },
          "ZoneId": {
            "isRequired": true,
            "extMonitorInfo": false,
            "description": "The resource attribute field that represents the zone",
            "readOnly": false,
            "sensitive": false,
            "title": "The ID of the zone",
            "type": "string",
            "updateType": false
          },
          "VSwitchId": {
            "isRequired": false,
            "extMonitorInfo": false,
            "description": "The ID of the vSwitch",
            "readOnly": false,
            "sensitive": false,
            "title": "The ID of the vSwitch",
            "type": "string",
            "updateType": false
          },
          "AvailableIpAddressCount": {
            "isRequired": false,
            "extMonitorInfo": false,
            "default": "",
            "deprecated": false,
            "format": "int64",
            "pattern": "",
            "description": "The number of available IP addresses in the vSwitch",
            "readOnly": true,
            "sensitive": false,
            "title": "The number of available IP addresses in the vSwitch",
            "type": "integer",
            "updateType": false
          },
          "CreateTime": {
            "isRequired": false,
            "extMonitorInfo": false,
            "default": "",
            "deprecated": false,
            "pattern": "",
            "description": "The time when the vSwitch was created",
            "readOnly": true,
            "sensitive": false,
            "title": "The time when the vSwitch was created",
            "type": "string",
            "updateType": false
          },
          "CidrBlock": {
            "isRequired": true,
            "extMonitorInfo": false,
            "description": "The CIDR block of the vSwitch",
            "readOnly": false,
            "sensitive": false,
            "title": "The CIDR block of the vSwitch",
            "type": "string",
            "updateType": false
          },
          "VpcId": {
            "isRequired": true,
            "extMonitorInfo": false,
            "description": "The ID of the VPC",
            "readOnly": false,
            "sensitive": false,
            "title": "The ID of the VPC",
            "type": "string",
            "updateType": false
          },
          "VSwitchName": {
            "isRequired": false,
            "extMonitorInfo": false,
            "description": "The name of the vSwitch",
            "readOnly": false,
            "sensitive": false,
            "title": "The name of the vSwitch",
            "type": "string",
            "updateType": true
          },
          "VpcIpv6CidrBlock": {
            "isRequired": false,
            "extMonitorInfo": false,
            "default": "",
            "deprecated": false,
            "pattern": "",
            "description": "The IPv6 CIDR block of the VPC",
            "operatePrivateType": [
              "create"
            ],
            "readOnly": false,
            "sensitive": false,
            "title": "The IPv6 CIDR block of the VPC",
            "type": "string",
            "updateType": false
          },
          "RegionId": {
            "isRequired": false,
            "extMonitorInfo": false,
            "description": "The resource attribute field that represents the region",
            "readOnly": false,
            "sensitive": false,
            "title": "The ID of the region",
            "type": "string",
            "updateType": false
          },
          "Ipv6CidrBlock": {
            "isRequired": false,
            "extMonitorInfo": false,
            "description": "The IPv6 CIDR block of the vSwitch",
            "readOnly": false,
            "sensitive": false,
            "title": "The IPv6 CIDR block of the vSwitch",
            "type": "string",
            "updateType": true
          },
          "Tags": {
            "isRequired": false,
            "extMonitorInfo": false,
            "description": "The data structure returned",
            "readOnly": false,
            "sensitive": false,
            "title": "Tag",
            "type": "array",
            "items": {
              "extMonitorInfo": false,
              "deprecated": false,
              "description": "The data structure returned",
              "sensitive": false,
              "title": "The data structure returned",
              "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": true
            },
            "updateType": true
          }
        },
        "sensitiveInfoProperties": [
          
        ],
        "info": {
          "deliveryScope": "zone",
          "chargeType": "free",
          "description": "vSwitch",
          "title": "vSwitch"
        },
        "resourceType": "VSwitch"
      }
    }

Create resources

Create a vSwitch for subsequent operations such as update, query, and delete.

  1. You can call the CreateResource operation and enter request parameters based on the resource metadata. Sample code:

    /**
     * Create resources
     * @param regionId         The region ID.
     * @param productCode      The product code, which is "VPC" in this example.
     * @param resourceTypeCode The resource type code, which is "vSwitch" in this example.
     * @param client           RAM user (client)
     * @return resourceWithOptions The response.
     */
    public CreateResourceResponse createResource(String regionId, String productCode, String resourceTypeCode, Client client) throws Exception {
        String requestPath = "/api/v1/providers/Aliyun/products/" + productCode + "/resources/" + resourceTypeCode;
        // The response parameters
        Map body = new HashMap();
        body.put("VpcId", "vpc-m5esjf3a6b380olet****"); // The ID of the VPC.
        body.put("CidrBlock", "172.16.24.0/24"); // The CIDR block of the vSwitch. 
        body.put("ZoneId", "cn-qingdao-b");  // The ID of the zone. 
        CreateResourceRequest createResourceRequest = new CreateResourceRequest()
                .setRegionId(regionId)
                .setBody(body);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        Map<String, String> headers = new HashMap<>();
        CreateResourceResponse resourceWithOptions = client.createResourceWithOptions(requestPath, createResourceRequest, headers, runtime);
        return resourceWithOptions;
    }
  2. If the call is successful, statusCode=202 is returned, which indicates that the task is an asynchronous operation. You need to further query the task status. For more information, see Query tasks. To query, update, and delete resources, you must record the resourceid. The response body is as follows:

    resourceWithOptions.getBody()

    {
      "resourceId": "vsw-m5eaburrn73mdpemo****",
      "requestId": "4F7069A4-3CA8-5A48-9D3D-40A1302CBD1B",
      "resourcePath": "VSwitch/vsw-m5eaburrn73mdpemo****",
      "taskId": "task-5057a4fbef2bcffe44d6b3590****"
    }

List resources

  1. You can call the GetResources operation to list all vSwitch resources. Sample code:

    /**
     * List resources
     * @param regionId         The region ID
     * @param productCode      The product code, which is "VPC" in this example.
     * @param resourceTypeCode The resource type code, which is "vSwitch" in this example.
     * @param client           RAM user (client)
     * @return resourceWithOptions The response.
     */
    public GetResourcesResponse getResources(String regionId, String productCode, String resourceTypeCode, Client client) throws Exception {
        String requestPath = "/api/v1/providers/aliyun/products/" + productCode + "/resources/" + resourceTypeCode;
        GetResourcesRequest getResourcesRequest = new GetResourcesRequest()
                .setRegionId(regionId);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        Map<String, String> headers = new HashMap<>();
        GetResourcesResponse resourcesWithOptions = client.getResourcesWithOptions(requestPath, getResourcesRequest, headers, runtime);
        return resourcesWithOptions;
    }
  2. If the call is successful, the following response body is returned:

    resourcesWithOptions.getBody()

    {
      "maxResults": 10,
      "nextToken": "2",
      "requestId": "1B5258F0-656F-5005-9FA7-33845FD9FE33",
      "resources": [
       ...,
        {
          "resourceId": "vsw-m5eaburrn73mdpemo****",
          "resourceAttributes": {
            "IsDefault": false,
            "Status": "Available",
            "Description": "",
            "RouteTableId": "vtb-m5epr0god70hpomnn****",
            "ResourceGroupId": "rg-acfmykd63gtpfpa",
            "ZoneId": "cn-qingdao-b",
            "AvailableIpAddressCount": 252,
            "CreateTime": "2024-10-16T05:30:31Z",
            "VSwitchId": "vsw-m5eaburrn73mdpemo****",
            "CidrBlock": "172.16.24.0/24",
            "VpcId": "vpc-m5esjf3a6b380olet****",
            "VSwitchName": "",
            "RegionId": "cn-qingdao",
            "Ipv6CidrBlock": "",
            "Tags": [
              {
                "TagKey": "acs:tag:createdby",
                "TagValue": "sub:206453422934844953:user****"
              }
            ]
          }
        }
      ],
      "totalCount": 29
    }    

Updates resources

  1. You can call the UpdateResource operation to update a specified resource. Sample code:

    Important

    Proceed with caution when you update a resource to prevent accidental operations from affecting other resources.

    /**
     * Update resources
     * @param regionId         The region ID
     * @param productCode      The product code, which is "VPC" in this example.
     * @param productCode      The product code, which is "vSwitch" in this example.
     * @param resourceId The ID of the resource. This parameter specifies the ID of the vSwitch that is created in this example.
     * @param client           RAM user (client)
     * @return resourceWithOptions The response.
     */
    public UpdateResourceResponse updateResource(String regionId, String productCode, String resourceTypeCode, String resourceId, Client client) throws Exception {
        String requestPath = "/api/v1/providers/Aliyun/products/" + productCode + "/resources/" + resourceTypeCode + "/" + resourceId;
        // The response parameters.
        Map body = new HashMap();
        body.put("Description", "Test update");
        UpdateResourceRequest updateResourceRequest = new UpdateResourceRequest()
                .setRegionId(regionId)
                .setBody(body);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        Map<String, String> headers = new HashMap<>();
        UpdateResourceResponse updateResourceResponse = client.updateResourceWithOptions(requestPath, updateResourceRequest, headers, runtime);
        return updateResourceResponse;
    }
  2. The response code statusCode=200 indicates that the request is successful. The following response body is returned: You can query the details of the resource to verify whether the resource is updated. For more information, see Query resources.

    updateResourceResponse.getBody()

    {
      "requestId": "C97193DF-FA5A-5B9B-B822-4C128CAC371E"
    }

Query resources

  1. You can call the GetResources operation to query a specified resource. Sample code:

    /**
     * Query resources
     * @param regionId         The region ID
     * @param productCode      The product code, which is "VPC" in this example.
     * @param productCode      The product code, which is "vSwitch" in this example.
     * @param resourceId The ID of the resource. This parameter specifies the ID of the vSwitch that is created in this example.
     * @param client           RAM user (client)
     * @return resourceWithOptions The response.
     */
    public GetResourcesResponse getResources(String regionId, String productCode, String resourceTypeCode, String resourceId, Client client) throws Exception {
        String requestPath = "/api/v1/providers/aliyun/products/" + productCode + "/resources/" + resourceTypeCode + "/" + resourceId;
        Map<String, String> body = new HashMap<>();
        GetResourcesRequest getResourcesRequest = new GetResourcesRequest()
                .setRegionId(regionId);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        Map<String, String> headers = new HashMap<>();
        GetResourcesResponse resourcesWithOptions = client.getResourcesWithOptions(requestPath, getResourcesRequest, headers, runtime);
        return resourcesWithOptions;
    }
  2. If the request is successful, the following body is returned:

    resourcesWithOptions.getBody()

    {
      "resource": {
        "resourceId": "vsw-m5eaburrn73mdpemo****",
        "resourceAttributes": {
          "IsDefault": false,
          "Status": "Available",
          "Description": "Test update",
          "RouteTableId": "vtb-m5epr0god70hpomnn****",
          "ResourceGroupId": "rg-acfmykd63gtpfpa",
          "ZoneId": "cn-qingdao-b",
          "AvailableIpAddressCount": 252,
          "CreateTime": "2024-10-16T05:30:31Z",
          "VSwitchId": "vsw-m5eaburrn73mdpemo****",
          "CidrBlock": "172.16.24.0/24",
          "VpcId": "vpc-m5esjf3a6b380olet****",
          "VSwitchName": "",
          "RegionId": "cn-qingdao",
          "Ipv6CidrBlock": "",
          "Tags": [
            {
              "TagKey": "acs:tag:createdby",
              "TagValue": "sub:206453422934844953:user****"
            }
          ]
        }
      },
      "requestId": "B16A5BFC-20FC-5C3A-B640-6CB014328E16"
    }

Delete resources

  1. You can call the DeleteResource operation to delete a specified resource. Sample code:

    Important

    Proceed with caution when you delete a resource to prevent accidental operations from affecting other resources.

    /**
     * Delete resources
     * @param regionId         The region ID
     * @param productCode      The product code, which is "VPC" in this example.
     * @param productCode      The product code, which is "vSwitch" in this example.
     * @param resourceId The ID of the resource. This parameter specifies the ID of the vSwitch that is created in this example.
     * @param client           RAM user (client)
     * @return deleteResourceResponse The response.
     */
    public DeleteResourceResponse deleteResource(String regionId, String productCode, String resourceTypeCode, String resourceId, Client client) throws Exception {
        String requestPath = "/api/v1/providers/Aliyun/products/" + productCode + "/resources/" + resourceTypeCode + "/" + resourceId;
        DeleteResourceRequest deleteResourceRequest = new DeleteResourceRequest()
                .setRegionId(regionId);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        Map<String, String> headers = new HashMap<>();
        DeleteResourceResponse deleteResourceResponse = client.deleteResourceWithOptions(requestPath, deleteResourceRequest, headers, runtime);
        return deleteResourceResponse;
    }
  2. If the call is successful, statusCode=202 is returned, which indicates that the task is an asynchronous operation. You need to further query the task status. For more information, see Query task status.

    deleteResourceResponse.getBody()

    {
      "requestId": "A9BC357C-7ED1-5046-ADDF-4BE17C5A7064",
      "taskId": "task-5057a4fbef2bcffe4562c2954****"
    }

Query task status

Queries the status of a specified task.

  1. You can call the GetTask operation to query the status of a specified task. The call example is to query the task in which a vSwitch is being created:

    /**
     * Query asynchronous task
     * @param taskId The ID of the task
     * @param client RAM user (client)
     * @return taskWithOptions The response.
     */
    public GetTaskResponse getTask(String taskId, Client client) throws Exception {
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        Map<String, String> headers = new HashMap<>();
        GetTaskResponse taskWithOptions = client.getTaskWithOptions(taskId, headers, runtime);
        return taskWithOptions;
    }
  2. If the request is successful, status="Succeeded" in the body is returned, indicating that the vSwitch is created.

    taskWithOptions.getBody()

    {
      "task": {
        "taskAction": "Create",
        "product": "VPC",
        "resourceId": "vsw-m5eaburrn73mdpemo****",
        "createTime": "2024-10-16T05:30:31Z",
        "regionId": "cn-qingdao",
        "resourcePath": "VSwitch/vsw-m5eaburrn73mdpemo****",
        "taskId": "task-5057a4fbef2bcffe44d6b3590****",
        "resourceType": "VSwitch",
        "status": "Succeeded"
      },
      "requestId": "C062613B-8A99-5E69-86DB-26446D1AB845"
    }

Complete sample code

The complete ExampleDemo in this tutorial is as follows:

ExampleDemo

package org.example;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.cloudcontrol20220830.Client;
import com.aliyun.cloudcontrol20220830.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

import java.util.HashMap;
import java.util.Map;

/**
 * Implement resource management.
 */
public class ExampleDemo {

    /**
     * Initialize the client.
     * @return Client
     */
    public static Client createClient() throws Exception {
        // If the project code is leaked, the AccessKey pair may be leaked and the security of all resources in your account may be compromised. The following sample code is provided only for reference. 
        Config config = new Config()
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured. 
                .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured. 
                .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        # For more information about endpoints, see https://api.aliyun.com/product/cloudcontrol.
        config.endpoint = "cloudcontrol.aliyuncs.com";
        return new Client(config);
    }

    /**
     * Queries products
     * @param client           The client
     * @return listProductsResponse The response.
     */
    public ListProductsResponse listProducts(Client client) throws Exception {
        ListProductsHeaders listProductsHeaders = new ListProductsHeaders();
        ListProductsRequest listProductsRequest = new ListProductsRequest();
        RuntimeOptions runtime = new RuntimeOptions();
        ListProductsResponse listProductsResponse = client.listProductsWithOptions("Aliyun", listProductsRequest, listProductsHeaders, runtime);
        return listProductsResponse;
    }

    /**
     * Queries resource types
     * @param productCode      The product code, which is "VPC" in this example.
     * @param client           The client.
     * @return listResourceTypesResponse The response.
     */
    public ListResourceTypesResponse listResourceTypes(String productCode, Client client) throws Exception {
        com.aliyun.cloudcontrol20220830.models.ListResourceTypesHeaders listResourceTypesHeaders = new com.aliyun.cloudcontrol20220830.models.ListResourceTypesHeaders();
        com.aliyun.cloudcontrol20220830.models.ListResourceTypesRequest listResourceTypesRequest = new com.aliyun.cloudcontrol20220830.models.ListResourceTypesRequest();
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        // Write your own code to display the response of the API operation if necessary.
        ListResourceTypesResponse listResourceTypesResponse = client.listResourceTypesWithOptions("Aliyun", productCode, listResourceTypesRequest, listResourceTypesHeaders, runtime);
        return listResourceTypesResponse;
    }

    /**
     * Create resources
     * @param regionId         The region ID.
     * @param productCode      The product code, which is "VPC" in this example.
     * @param resourceTypeCode The resource type code, which is "VPC" in this example.
     * @param client           RAM user (client)
     * @return resourceWithOptions The response.
     */
    public CreateResourceResponse createResource(String regionId, String productCode, String resourceTypeCode, Client client) throws Exception {
        String requestPath = "/api/v1/providers/Aliyun/products/" + productCode + "/resources/" + resourceTypeCode;
        // The response parameters
        Map body = new HashMap();
        body.put("Description", "Test creation");
        com.aliyun.cloudcontrol20220830.models.CreateResourceRequest createResourceRequest = new com.aliyun.cloudcontrol20220830.models.CreateResourceRequest()
                .setRegionId(regionId).setBody(body);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        CreateResourceResponse resourceWithOptions = client.createResourceWithOptions(requestPath, createResourceRequest, headers, runtime);
        return resourceWithOptions;
    }

    /**
     * Query asynchronous tasks
     * @param taskId The ID of the task
     * @param client RAM user (client)
     * @return taskWithOptions The response.
     */
    public GetTaskResponse getTask(String taskId, Client client) throws Exception {
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        GetTaskResponse taskWithOptions = client.getTaskWithOptions(taskId, headers, runtime);
        return taskWithOptions;
    }

    /**
     * List resources
     * @param regionId         The region ID
     * @param productCode      The product code, which is "VPC" in this example.
     * @param resourceTypeCode The resource type code, which is "VPC" in this example.
     * @param client           RAM user (client)
     * @return resourceWithOptions The response.
     */
    public GetResourcesResponse getResources(String regionId, String productCode, String resourceTypeCode, Client client) throws Exception {
        String requestPath = "/api/v1/providers/aliyun/products/" + productCode + "/resources/" + resourceTypeCode;
        // The response parameters.
        Map body = new HashMap();
        body.put("VpcId", "vpc-m5er4j63farlj8d53****");
        com.aliyun.cloudcontrol20220830.models.GetResourcesRequest getResourcesRequest = new com.aliyun.cloudcontrol20220830.models.GetResourcesRequest()
                .setRegionId(regionId).setFilter(body);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        GetResourcesResponse resourcesWithOptions = client.getResourcesWithOptions(requestPath, getResourcesRequest, headers, runtime);
        return resourcesWithOptions;
    }

    /**
     * Query resources
     * @param regionId         The region ID
     * @param productCode      The product code, which is "VPC" in this example.
     * @param resourceTypeCode The resource type code, which is "vSwitch" in this example.
     * @param resourceId The ID of the resource. This parameter specifies the ID of the VPC to be created in this example.
     * @param client           RAM user (client)
     * @return resourceWithOptions The response.
     */
    public GetResourcesResponse getResources(String regionId, String productCode, String resourceTypeCode, String resourceId, Client client) throws Exception {
        String requestPath = "/api/v1/providers/aliyun/products/" + productCode + "/resources/" + resourceTypeCode + "/" + resourceId;
        com.aliyun.cloudcontrol20220830.models.GetResourcesRequest getResourcesRequest = new com.aliyun.cloudcontrol20220830.models.GetResourcesRequest()
                .setRegionId(regionId);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        GetResourcesResponse resourcesWithOptions = client.getResourcesWithOptions(requestPath, getResourcesRequest, headers, runtime);
        return resourcesWithOptions;
    }

    /**
     * Update resources
     * @param regionId         The region ID
     * @param productCode      The product code, which is "VPC" in this example.
     * @param resourceTypeCode The resource type code, which is "VPC" in this example.
     * @param resourceId The ID of the resource. This parameter specifies the ID of the VPC to be created in this example.
     * @param client           RAM user (client)
     * @return resourceWithOptions The response.
     */
    public UpdateResourceResponse updateResource(String regionId, String productCode, String resourceTypeCode, String resourceId, Client client) throws Exception {
        String requestPath = "/api/v1/providers/Aliyun/products/" + productCode + "/resources/" + resourceTypeCode + "/" + resourceId;
        // The response parameters.
        Map body = new HashMap();
        body.put("Description", "Test update");
        com.aliyun.cloudcontrol20220830.models.UpdateResourceRequest updateResourceRequest = new com.aliyun.cloudcontrol20220830.models.UpdateResourceRequest()
                .setRegionId(regionId).setBody(body);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        UpdateResourceResponse updateResourceResponse = client.updateResourceWithOptions(requestPath, updateResourceRequest, headers, runtime);
        return updateResourceResponse;
    }

    /**
     * Delete resources
     * @param regionId         The region ID
     * @param productCode      The product code, which is "VPC" in this example.
     * @param resourceTypeCode The resource type code, which is "VPC" in this example.
     * @param resourceId The ID of the resource. This parameter specifies the ID of the VPC to be created in this example.
     * @param client           RAM user (client)
     * @return deleteResourceResponse The response.
     */
    public DeleteResourceResponse deleteResource(String regionId, String productCode, String resourceTypeCode, String resourceId, Client client) throws Exception {
        String requestPath = "/api/v1/providers/Aliyun/products/" + productCode + "/resources/" + resourceTypeCode + "/" + resourceId;
        com.aliyun.cloudcontrol20220830.models.DeleteResourceRequest deleteResourceRequest = new com.aliyun.cloudcontrol20220830.models.DeleteResourceRequest()
                .setRegionId(regionId);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        DeleteResourceResponse deleteResourceResponse = client.deleteResourceWithOptions(requestPath, deleteResourceRequest, headers, runtime);
        return deleteResourceResponse;
    }
}