本文示範如何通過Java SDK調用雲控制API實現資源管理。以Virtual Private Cloud為例,展示SDK的整合過程,以及查詢資源中繼資料、建立、查詢、更新和刪除資源的具體操作。此外,還將介紹在雲控制API採用非同步作業處理任務時,如何查詢任務狀態。
前提條件
為確保順利操作,請完成以下前置準備:
已整合SDK,具體操作請參見Java整合SDK。
資源管理基於資源中繼資料進行操作,因此熟悉資源中繼資料是高效使用雲控制 API 的關鍵。資源中繼資料定義了資源的屬性、狀態及其他相關資訊。具體資訊,請參見資源中繼資料。
雲控制API處理任務時採用非同步作業,因此您需要瞭解同步操作與非同步作業的基本概念。關於查詢任務狀態的具體資訊,請參見教程中的GetTask介面。
請求路徑具體資訊,請參見請求路徑說明。
調用雲控制API以實現資源管理
本文教程以Virtual Private Cloud為樣本,其中Virtual Private Cloud的產品code為VPC,資源類型code也為VPC。通過產品code和資源類型code,您可以查詢資源類型的詳細資料。後續的資源管理操作,包括建立、查詢、列舉、更新及刪除,均基於獲得的資源中繼資料進行。
介面名稱:GetResourceType
此介面旨在查詢資源類型的詳情。可以通過產品code與資源類型code查詢相應的資源類型的詳細資料,返回的資料為資源中繼資料。有關介面詳細資料,請參見擷取資源中繼資料 API文檔。在調用過程中,您需根據實際業務需求設定相應的參數及回合組態。
// 配置運行時參數
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
// // 忽略對 SSL 憑證的驗證
// runtime.ignoreSSL = true;
// // 代理配置
// runtime.httpProxy = "http://127.0.0.1:9898";
// runtime.httpsProxy = "http://user:password@127.0.0.1:8989";
// runtime.noProxy = "127.0.0.1,localhost";
// // 連線逾時
// runtime.connectTimeout = 5000;
// // 讀逾時
// runtime.readTimeout = 10000;
// // 開啟自動重試機制
// runtime.autoretry = true;
// // 設定自動重試次數
// runtime.maxAttempts = 3;
// 請求路徑
String requestPath = "/api/v1/providers/Aliyun/products/" + PRODUCT_CODE + "/resourceTypes/" + RESOURCE_TYPE_CODE;
com.aliyun.cloudcontrol20220830.models.GetResourceTypeHeaders getResourceTypeHeaders = new com.aliyun.cloudcontrol20220830.models.GetResourceTypeHeaders()
.setXAcsAcceptLanguage("zh_CH"); // 選擇返回產品的語言 zh_CH:中文 (預設)en_US:英文。
// 發起請求
GetResourceTypeResponse getResourceTypeResponse = client.getResourceTypeWithOptions(requestPath, getResourceTypeHeaders, runtime);介面名稱:CreateResource
此介面的目的是建立一個Virtual Private Cloud資源。有關介面詳細資料,請參見建立資源 API文檔。在調用過程中,您需根據實際業務需求設定相應的請求參數及回合組態。
// 運行時參數
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
// // 忽略對 SSL 憑證的驗證
// runtime.ignoreSSL = true;
// // 代理配置
// runtime.httpProxy = "http://127.0.0.1:9898";
// runtime.httpsProxy = "http://user:password@127.0.0.1:8989";
// runtime.noProxy = "127.0.0.1,localhost";
// // 連線逾時
// runtime.connectTimeout = 5000;
// // 讀逾時
// runtime.readTimeout = 10000;
// // 開啟自動重試機制
// runtime.autoretry = true;
// // 設定自動重試次數
// runtime.maxAttempts = 3;
// headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
java.util.Map < String, String > headers = new java.util.HashMap < > ();
// headers.put("x-acs-action", "CreateResource"); // 設定介面名稱
// 請求路徑
String requestPath = "/api/v1/providers/Aliyun/products/" + PRODUCT_CODE + "/resources/" + RESOURCE_TYPE_CODE;
java.util.Map < String, Object > body = TeaConverter.buildMap(
new TeaPair("IsDefault", true), // 是否預設VPC
new TeaPair("Description", "這是一個測試VPC"), // 描述
new TeaPair("VpcName", "VPC_Test"), // VPC名稱
new TeaPair("Tags", java.util.Arrays.asList(
TeaConverter.buildMap(
new TeaPair("TagKey", "key"),
new TeaPair("TagValue", "value")
)
)), // VPC標籤
new TeaPair("EnableIpv6", false), //是否開啟IPv6網段
new TeaPair("DryRun", false) // 是否只預檢此次請求
);
// 請求參數
com.aliyun.cloudcontrol20220830.models.CreateResourceRequest createResourceRequest = new com.aliyun.cloudcontrol20220830.models.CreateResourceRequest()
.setRegionId("cn-qingdao") // 地區ID
.setBody(body);
// 發起請求
CreateResourceResponse createResourceResponse = client.createResourceWithOptions(requestPath, createResourceRequest, headers, runtime);介面名稱:GetResources
此介面的目的是擷取一個Virtual Private Cloud資來源詳細資料。有關介面詳細資料,請參見擷取資源詳情 API文檔。 在調用過程中,您需根據實際業務需求設定相應的請求參數及回合組態。
// 運行時參數
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
// // 忽略對 SSL 憑證的驗證
// runtime.ignoreSSL = true;
// // 代理配置
// runtime.httpProxy = "http://127.0.0.1:9898";
// runtime.httpsProxy = "http://user:password@127.0.0.1:8989";
// runtime.noProxy = "127.0.0.1,localhost";
// // 連線逾時
// runtime.connectTimeout = 5000;
// // 讀逾時
// runtime.readTimeout = 10000;
// // 開啟自動重試機制
// runtime.autoretry = true;
// // 設定自動重試次數
// runtime.maxAttempts = 3;
// headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
java.util.Map < String, String > headers = new java.util.HashMap < > ();
// headers.put("x-acs-action", "GetResources"); // 設定介面名稱
String resourceId = "vpc-m5eml8m3XXXXXXXX";
// 請求路徑
String requestPath = "/api/v1/providers/Aliyun/products/" + PRODUCT_CODE + "/resources/" + RESOURCE_TYPE_CODE + "/" + resourceId;
com.aliyun.cloudcontrol20220830.models.GetResourcesRequest getResourcesRequest = new com.aliyun.cloudcontrol20220830.models.GetResourcesRequest()
.setRegionId("cn-qingdao"); // 地區ID
// 發起請求
GetResourcesResponse getResourcesResponse = client.getResourcesWithOptions(requestPath, getResourcesRequest, headers, runtime);介面名稱:GetResources
此介面的目的是列舉所有Virtual Private Cloud資源資訊。有關介面詳細資料,請參見 列舉資源 API文檔。 在調用過程中,您需根據實際業務需求設定相應的請求參數及回合組態。
// 運行時配置
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
// // 忽略對 SSL 憑證的驗證
// runtime.ignoreSSL = true;
// // 代理配置
// runtime.httpProxy = "http://127.0.0.1:9898";
// runtime.httpsProxy = "http://user:password@127.0.0.1:8989";
// runtime.noProxy = "127.0.0.1,localhost";
// // 連線逾時
// runtime.connectTimeout = 5000;
// // 讀逾時
// runtime.readTimeout = 10000;
// // 開啟自動重試機制
// runtime.autoretry = true;
// // 設定自動重試次數
// runtime.maxAttempts = 3;
// headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
java.util.Map < String, String > headers = new java.util.HashMap < > ();
// headers.put("x-acs-action", "GetResource"); // 設定介面名稱
// 請求路徑
String requestPath = "/api/v1/providers/Aliyun/products/" + PRODUCT_CODE + "/resources/" + RESOURCE_TYPE_CODE;
// 過濾條件(可選)
// java.util.Map < String, Object > filter = TeaConverter.buildMap(
// new TeaPair("IsDefault", true), // 是否是預設VPC
// new TeaPair("ResourceGroupId", "<YOUR_RESOURCEGROUPID>"), // 資源群組ID
// new TeaPair("DhcpOptionsSetId", "<YOUR_DHCPOPTIONSSETID>"), // DHCP選項集的ID
// new TeaPair("VpcId", "<YOUR_VPCID>"), // VPC的ID
// new TeaPair("VpcName", "<YOUR_VPCNAME>") // VPC的名稱
// );
com.aliyun.cloudcontrol20220830.models.GetResourcesRequest getResourcesRequest = new com.aliyun.cloudcontrol20220830.models.GetResourcesRequest()
// .setFilter(filter)
.setRegionId("cn-qingdao");
// 發起請求
GetResourcesResponse getResourcesResponse = client.getResourcesWithOptions(requestPath, getResourcesRequest, headers, runtime);介面名稱:UpdateResource
此介面的目的是更新Virtual Private Cloud資源資訊。有關介面詳細資料,請參見更新資源 API文檔。在調用過程中,您需根據實際業務需求設定相應的請求參數及回合組態。
// 運行時配置
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
// // 忽略對 SSL 憑證的驗證
// runtime.ignoreSSL = true;
// // 代理配置
// runtime.httpProxy = "http://127.0.0.1:9898";
// runtime.httpsProxy = "http://user:password@127.0.0.1:8989";
// runtime.noProxy = "127.0.0.1,localhost";
// // 連線逾時
// runtime.connectTimeout = 5000;
// // 讀逾時
// runtime.readTimeout = 10000;
// // 開啟自動重試機制
// runtime.autoretry = true;
// // 設定自動重試次數
// runtime.maxAttempts = 3;
// headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
java.util.Map < String, String > headers = new java.util.HashMap < > ();
// headers.put("x-acs-action", "UpdateResource");
// 資源ID
String resourceId = "vpc-m5eml8mXXXXXXXX";
// 請求路徑
String requestPath = "/api/v1/providers/Aliyun/products/VPC/resources/VPC/" + resourceId;
java.util.Map < String, Object > body = TeaConverter.buildMap(
new TeaPair("Description", "更新測試VPC描述"), // 資源描述
new TeaPair("VpcName", "UpdateVpc"), // 資源名稱
new TeaPair("Tags", java.util.Arrays.asList(
TeaConverter.buildMap(
new TeaPair("TagKey", "key1"),
new TeaPair("TagValue", "value1")
)
)) // 資源標籤
);
// 請求參數
com.aliyun.cloudcontrol20220830.models.UpdateResourceRequest updateResourceRequest = new com.aliyun.cloudcontrol20220830.models.UpdateResourceRequest()
.setRegionId("cn-qingdao")
.setBody(body);
// 發起請求
UpdateResourceResponse updateResourceResponse = client.updateResourceWithOptions(requestPath, updateResourceRequest, headers, runtime);介面名稱:DeleteResource
此介面目的是用於刪除指定的Virtual Private Cloud資源。有關介面詳細資料,請參見資源刪除 API文檔。在調用過程中,您需根據實際業務需求設定相應的請求參數及回合組態。
刪除操作為無法復原操作,務必謹慎處理。在執行刪除VPC操作之前,建議首先在測試環境中對刪除流程進行全面驗證,確保無誤後再進行生產資源的操作。
// 配置運行時參數
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
// // 忽略對 SSL 憑證的驗證
// runtime.ignoreSSL = true;
// // 代理配置
// runtime.httpProxy = "http://127.0.0.1:9898";
// runtime.httpsProxy = "http://user:password@127.0.0.1:8989";
// runtime.noProxy = "127.0.0.1,localhost";
// // 連線逾時
// runtime.connectTimeout = 5000;
// // 讀逾時
// runtime.readTimeout = 10000;
// // 開啟自動重試機制
// runtime.autoretry = true;
// // 設定自動重試次數
// runtime.maxAttempts = 3;
// headers 參數,可自訂要求標頭,覆蓋預設值或添加額外的資訊。
java.util.Map < String, String > headers = new java.util.HashMap < > ();
// headers.put("x-acs-action", "DeleteResource"); // 設定介面名稱
// 資源ID
String resourceId = "vpc-m5ei5xmtXXXXXXXX";
// 請求路徑
String requestPath = "/api/v1/providers/Aliyun/products/" + PRODUCT_CODE + "/resources/" + RESOURCE_TYPE_CODE + "/" + resourceId;
com.aliyun.cloudcontrol20220830.models.DeleteResourceRequest deleteResourceRequest = new com.aliyun.cloudcontrol20220830.models.DeleteResourceRequest()
.setRegionId("cn-qingdao");
// 發起請求
DeleteResourceResponse deleteResourceResponse = client.deleteResourceWithOptions(requestPath, deleteResourceRequest, headers, runtime);介面名稱: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);