本文演示如何通过Java SDK调用云控制API实现资源管理。以专有网络VPC为例,展示SDK的集成过程,以及查询资源元数据、创建、查询、更新和删除资源的具体操作。此外,还将介绍在云控制API采用异步操作处理任务时,如何查询任务状态。
前提条件
为确保顺利操作,请完成以下前置准备:
调用云控制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);接口名称: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);接口名称: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);接口名称: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);接口名称: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);接口名称: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);接口名称: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);