本文為您介紹資源編排服務ROS(Resource Orchestration Service)的Java SDK安裝方法和示例代碼。以查詢可用地域列表、創建資源棧、查詢資源棧、刪除資源棧等操作為例,幫助您快速掌握資源編排接口的使用方法。
安裝Java SDK
下載及安裝Java SDK。
說明Java SDK的下載和使用說明,請參見SDK概覽。
建議您使用JRE 1.8及以上版本。
在pom.xml中,添加依賴包。
<dependencies> <dependency> <groupId>com.aliyun</groupId> <artifactId>ros20190910</artifactId> <version>2.2.20</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>tea-openapi</artifactId> <version>0.2.8</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>tea-console</artifactId> <version>0.0.1</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>tea-util</artifactId> <version>0.2.16</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>tea</artifactId> <version>1.1.14</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>darabonba-env</artifactId> <version>0.1.1</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>credentials-java</artifactId> <version>0.2.11</version> </dependency> </dependencies>
初始化SDK。
導入相關的軟件包。
import com.aliyun.tea.*; import com.aliyun.ros20190910.*; import com.aliyun.ros20190910.models.*; import com.aliyun.teaopenapi.*; import com.aliyun.teaopenapi.models.*; import com.aliyun.darabonba.env.*; import com.aliyun.credentials.*; import java.util.*;
初始化SDK客戶端對象。
public static com.aliyun.ros20190910.Client createClient() throws Exception { // 使用默認憑證初始化Credentials Client com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client(); Config config = new Config() // 配置云產品服務接入地址(endpoint) .setEndpoint("ros.aliyuncs.com") // 使用Credential配置憑證 .setCredential(credentialClient); return new com.aliyun.ros20190910.Client(config); }
示例
查詢可用地域列表示例
通過調用DescribeRegions接口查詢可用地域列表。關于DescribeRegions接口的詳情,請參見DescribeRegions。
public static void describeRegions(Client client) throws Exception { DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest(); // 復制代碼運行后,請自行打印API的返回值。 DescribeRegionsResponse resp = client.describeRegions(describeRegionsRequest); }
創建資源棧示例
通過調用CreateStack接口創建資源棧。關于CreateStack接口的詳情,請參見CreateStack。
在本示例中,指定了以下參數:
regionId:資源棧所屬的地域ID。
stackName:資源棧名稱。同一阿里云賬號中資源棧名稱不能重復。
timeoutInMinutes:創建資源棧的超時時間,單位為分鐘。如果在指定時間內不能完成創建,則資源棧創建失敗。
templateBody:模板的主體結構。
parameters:創建的資源棧所需要的參數。需要同時指定ParameterKey和ParameterValue。
參數設置的實例代碼如下所示。
String stackName = "MyStack"; Long timeoutInMinutes = 40L; String templateBody = "{\n" + " \"ROSTemplateFormatVersion\": \"2015-09-01\",\n" + " \"Parameters\": {\n" + " \"VpcName\": {\n" + " \"Type\": \"String\",\n" + " \"Description\": \"Vpc Name\",\n" + " \"Label\": \"Vpc Name\"\n" + " },\n" + " \"CidrBlock\": {\n" + " \"Type\": \"String\",\n" + " \"Description\": \"Vpc CidrBlock\",\n" + " \"Label\": \"Vpc CidrBlock\"\n" + " }\n" + " },\n" + " \"Resources\": {\n" + " \"Vpc\": {\n" + " \"Type\": \"ALIYUN::ECS::VPC\",\n" + " \"Properties\": {\n" + " \"CidrBlock\": {\n" + " \"Ref\": \"CidrBlock\"\n" + " },\n" + " \"VpcName\": {\n" + " \"Ref\": \"VpcName\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}"; CreateStackRequest.CreateStackRequestParameters parameters0 = new CreateStackRequest.CreateStackRequestParameters() .setParameterKey("VpcName") .setParameterValue("TestVpc"); CreateStackRequest.CreateStackRequestParameters parameters1 = new CreateStackRequest.CreateStackRequestParameters() .setParameterKey("CidrBlock") .setParameterValue("192.168.0.0/16"); List parameters = java.util.Arrays.asList(parameters0, parameters1);
參數設置的示例代碼如下所示。
public static void createStack(Client client, String regionId, String stackName, String templateBody, Long timeoutInMinutes, List parameters) throws Exception { CreateStackRequest createStackRequest = new CreateStackRequest() .setTemplateBody(templateBody) .setStackName(stackName) .setRegionId(regionId) .setTimeoutInMinutes(timeoutInMinutes) .setParameters(parameters); // 復制代碼運行后,請自行打印API的返回值。 CreateStackResponse resp = client.createStack(createStackRequest); com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp))); }
查詢資源棧示例
通過調用GetStack接口查詢資源棧信息。關于GetStack接口的詳情,請參見GetStack。
在本示例中,指定了以下參數:
stackId:資源棧ID。
regionId:資源棧所屬的地域ID。
參數設置的示例代碼如下:
public static void getStack(Client client, String stackId, String regionId) throws Exception {
GetStackRequest getStackRequest = new GetStackRequest()
.setStackId(stackId)
.setRegionId(regionId);
// 復制代碼運行后,請自行打印API的返回值。
GetStackResponse resp = client.getStack(getStackRequest);
com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
}
刪除資源棧示例
通過調用DeleteStack接口刪除資源棧。關于DeleteStack接口的詳情,請參見DeleteStack。
在本示例中,指定了以下參數:
regionId:資源棧所屬的地域ID。
stackId:資源棧ID。
參數設置的示例代碼如下所示。
public static void deleteStack(Client client, String stackId, String regionId) throws Exception {
DeleteStackRequest deleteStackRequest = new DeleteStackRequest()
.setStackId(stackId)
.setRegionId(regionId);
// 復制代碼運行后,請自行打印API的返回值。
DeleteStackResponse resp = client.deleteStack(deleteStackRequest);
com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
}
完整示例
以下為查詢可用地域列表、創建資源棧、查詢資源棧和刪除資源棧的完整操作示例。
說明調用接口前,需配置環境變量,通過環境變量讀取訪問憑證。更多信息,請參見配置訪問憑證。
AccessKey ID和AccessKey Secret的環境變量名:
ALIBABA_CLOUD_ACCESS_KEY_ID
、ALIBABA_CLOUD_ACCESS_KEY_SECRET
。package com.aliyun.sample; import com.aliyun.ros20190910.Client; import com.aliyun.tea.*; import com.aliyun.ros20190910.*; import com.aliyun.ros20190910.models.*; import com.aliyun.teaopenapi.*; import com.aliyun.teaopenapi.models.*; import com.aliyun.darabonba.env.*; import java.util.*; public class Sample { /** * 初始化賬號Client * @return Client * @throws Exception */ private static String REGIONID = "Region Id"; public static com.aliyun.ros20190910.Client createClient() throws Exception { // 使用默認憑證初始化Credentials Client。 com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client(); Config config = new Config() // 配置云產品服務接入地址(endpoint) .setEndpoint("ros.aliyuncs.com") // 使用Credential配置憑證 .setCredential(credentialClient); return new com.aliyun.ros20190910.Client(config); } public static void describeRegions(Client client) throws Exception { DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest(); // 復制代碼運行后,請自行打印API的返回值。 DescribeRegionsResponse resp = client.describeRegions(describeRegionsRequest); } public static void getStack(Client client, String stackId, String regionId, Boolean wait) throws Exception { GetStackRequest getStackRequest = new GetStackRequest() .setStackId(stackId) .setRegionId(regionId); // 復制代碼運行后,請自行打印API的返回值。 GetStackResponse response = client.getStack(getStackRequest); com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response))); GetStackResponseBody body = response.body; com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response))); if (wait) { if (com.aliyun.teautil.Common.equalString(body.status, "CREATE_IN_PROGRESS")) { com.aliyun.teaconsole.Client.log("正在創建中......"); com.aliyun.teautil.Common.sleep(30000); Sample.getStack(client, stackId, regionId, wait); } else if (com.aliyun.teautil.Common.equalString(body.status, "CREATE_FAILED")) { com.aliyun.teaconsole.Client.log("創建失敗, 信息:" + body.statusReason + ", requestId:" + body.requestId + ""); } else if (com.aliyun.teautil.Common.equalString(body.status, "CREATE_COMPLETE")) { com.aliyun.teaconsole.Client.log("創建成功"); com.aliyun.teaconsole.Client.log("ID:" + body.stackId + ""); } } else { com.aliyun.teaconsole.Client.log("ID:" + body.stackId + ""); com.aliyun.teaconsole.Client.log("狀態:" + body.status + ""); } } public static void deleteStack(Client client, String stackId, String regionId) throws Exception { DeleteStackRequest deleteStackRequest = new DeleteStackRequest() .setStackId(stackId) .setRegionId(regionId); // 復制代碼運行后,請自行打印API的返回值。 DeleteStackResponse resp = client.deleteStack(deleteStackRequest); com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp))); } public static String createStack(Client client, String regionId, String stackName, String templateBody, Long timeoutInMinutes, java.util.List parameters) throws Exception { CreateStackRequest createStackRequest = new CreateStackRequest() .setTemplateBody(templateBody) .setStackName(stackName) .setRegionId(regionId) .setTimeoutInMinutes(timeoutInMinutes) .setParameters(parameters); // 復制代碼運行后,請自行打印API的返回值。 CreateStackResponse resp = client.createStack(createStackRequest); com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp))); return resp.body.stackId; } public static void main(String[] args_) throws Exception { String stackName = "MyStack"; String parameter =" {\"ParameterKey\": \"VpcName\", \"ParameterValue\": \"test_vpc\"}"; Long timeoutInMinutes = 40L; String templateBody = "{\n" + " \"ROSTemplateFormatVersion\": \"2015-09-01\",\n" + " \"Parameters\": {\n" + " \"VpcName\": {\n" + " \"Type\": \"String\",\n" + " \"Description\": \"Vpc Name\",\n" + " \"Label\": \"Vpc Name\"\n" + " },\n" + " \"CidrBlock\": {\n" + " \"Type\": \"String\",\n" + " \"Description\": \"Vpc CidrBlock\",\n" + " \"Label\": \"Vpc CidrBlock\"\n" + " }\n" + " },\n" + " \"Resources\": {\n" + " \"Vpc\": {\n" + " \"Type\": \"ALIYUN::ECS::VPC\",\n" + " \"Properties\": {\n" + " \"CidrBlock\": {\n" + " \"Ref\": \"CidrBlock\"\n" + " },\n" + " \"VpcName\": {\n" + " \"Ref\": \"VpcName\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}"; com.aliyun.ros20190910.Client client = Sample.createClient(); Sample.describeRegions(client); CreateStackRequest.CreateStackRequestParameters parameters0 = new CreateStackRequest.CreateStackRequestParameters() .setParameterKey("VpcName") .setParameterValue("TestVpc"); CreateStackRequest.CreateStackRequestParameters parameters1 = new CreateStackRequest.CreateStackRequestParameters() .setParameterKey("CidrBlock") .setParameterValue("192.168.0.0/16"); List parameters = java.util.Arrays.asList(parameters0, parameters1); String stackId = Sample.createStack(client, REGIONID, stackName, templateBody, timeoutInMinutes, parameters); Sample.getStack(client, stackId, REGIONID, true); Sample.deleteStack(client, stackId, REGIONID); } }