本文为您介绍如何使用资源编排服务(ROS)的Java SDK来创建和管理资源栈。

背景信息

您除了可以在ROS控制台创建资源栈,还可以使用API代码来创建和管理资源栈。

准备工作

  1. 下载及安装Java SDK。
    说明 建议您使用JRE 1.8及以上版本。
  2. pom.xml中,添加aliyun-java-sdk-core及其它依赖包。
    <dependencies>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ros</artifactId>
            <version>3.2.0</version>
        </dependency>
    </dependencies>
  3. 初始化SDK。
    1. 导入相关的包。
      import java.util.ArrayList;
      import java.util.List;
      import com.aliyuncs.DefaultAcsClient;
      import com.aliyuncs.exceptions.ClientException;
      import com.aliyuncs.http.FormatType;
      import com.aliyuncs.http.HttpResponse;
      import com.aliyuncs.profile.DefaultProfile;
      import com.aliyuncs.ros.model.v20190910.CreateStackRequest;
      import com.aliyuncs.ros.model.v20190910.DeleteStackRequest;
      import com.aliyuncs.ros.model.v20190910.DescribeRegionsRequest;
      import com.aliyuncs.ros.model.v20190910.GetStackRequest;
    2. 初始化SDK客户端对象。
      private static String ACCESSKEYID = "<yourAccessKeyId>";
      private static String SECRET = "<yourAccessKeySecrect>";
      private static String REGIONID = "<yourRegionId>";
      private static DefaultAcsClient client = new DefaultAcsClient(DefaultProfile.getProfile(REGIONID, ACCESSKEYID, SECRET));
      private static int connectTimeout = 10000;
      private static int readTimeout = 10000;
      private static Long timeoutInMinutes = 30L;

查询可用地域列表

您可以使用Java SDK查询可用地域列表。

public String describeRegion() {
    DescribeRegionsRequest request = new DescribeRegionsRequest();
    request.setConnectTimeout(connectTimeout);
    request.setReadTimeout(readTimeout);
    request.setAcceptFormat(FormatType.JSON);
    HttpResponse response = null;
    String responseContent = null;
    try {
        response = client.doAction(request);
        responseContent = response.getHttpContentString();
    } catch (ClientException e) {
        e.printStackTrace();
    }
    return responseContent;
}

创建资源栈

创建资源栈时,您必须指定以下参数:
  • StackName:将要创建的资源栈的名称。每个用户空间下的资源栈名称不能重复。
  • TimeoutInMinutes:资源栈创建时长,单位为分钟。如果在指定时间内不能完成创建,则超时失败。
  • TemplateBody:创建的资源栈使用的模板内容。
  • TemplateURL:模板主体的文件的位置。必须指定TemplateBodyTemplateURL,但不能同时指定两者。
  • Parameters:创建的资源栈所需的参数。需要在模板中定义Key。
String stackName = "MyStack";
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" +
        "}";
List<CreateStackRequest.Parameters> parameterss = new ArrayList<CreateStackRequest.Parameters>();
CreateStackRequest.Parameters cidrBlock = new CreateStackRequest.Parameters();
cidrBlock.setParameterKey("CidrBlock");
cidrBlock.setParameterValue("192.168.0.0/16");
CreateStackRequest.Parameters vpcName = new CreateStackRequest.Parameters();
vpcName.setParameterKey("VpcName");
vpcName.setParameterValue("TestVpc");
parameterss.add(cidrBlock);
parameterss.add(vpcName);
public String createStack(String stackName, String templateBody, List<CreateStackRequest.Parameters> parameterss) {
    CreateStackRequest request = new CreateStackRequest();
    request.setConnectTimeout(connectTimeout);
    request.setReadTimeout(readTimeout);
    request.setTimeoutInMinutes(timeoutInMinutes);
    request.setStackName(stackName);
    // 若模板较大,建议使用TemplateURL参数,避免URL过长,调用失败。
    // 您也可以继承CreateStackRequest类,通过调用putBodyParameter(protected方法)自行实现setTemplateBody方法。
    // request.putBodyParameter("TemplateBody", templateBody)
    request.setTemplateBody(templateBody);
    request.setParameterss(parameterss);
    HttpResponse response = null;
    String responseContent = null;
    try {
        response = client.doAction(request);
        responseContent = response.getHttpContentString();
    } catch (ClientException e) {
        e.printStackTrace();
    }
    return responseContent;
}

查询资源栈

输入目标资源栈的ID。

public String getStack(String stackId) {
    GetStackRequest request = new GetStackRequest();
    request.setConnectTimeout(connectTimeout);
    request.setReadTimeout(readTimeout);
    request.setStackId(stackId);
    request.setAcceptFormat(FormatType.JSON);
    HttpResponse response = null;
    String responseContent = null;
    try {
        response = client.doAction(request);
        responseContent = response.getHttpContentString();
    } catch (ClientException e) {
        e.printStackTrace();
    }
    return responseContent;
}

删除资源栈

输入目标资源栈的ID。

public String deleteStack(String stackId) {
    DeleteStackRequest request = new DeleteStackRequest();
    request.setConnectTimeout(connectTimeout);
    request.setReadTimeout(readTimeout);
    request.setStackId(stackId);
    request.setAcceptFormat(FormatType.JSON);
    HttpResponse response = null;
    String responseContent = null;
    try {
        response = client.doAction(request);
        responseContent = response.getHttpContentString();
    } catch (ClientException e) {
        e.printStackTrace();
    }
    return responseContent;
}

操作样例

package com;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.ros.model.v20190910.CreateStackRequest;
import com.aliyuncs.ros.model.v20190910.DeleteStackRequest;
import com.aliyuncs.ros.model.v20190910.DescribeRegionsRequest;
import com.aliyuncs.ros.model.v20190910.GetStackRequest;

import java.util.ArrayList;
import java.util.List;

public class Stack {
    private static String ACCESSKEYID = "<yourAccessKeyId>";
    private static String SECRET = "<yourAccessKeySecrect>";
    private static String REGIONID = "<yourRegionId>";
    private static DefaultAcsClient client = new DefaultAcsClient(DefaultProfile.getProfile(REGIONID, ACCESSKEYID, SECRET));
    private static int connectTimeout = 10000;
    private static int readTimeout = 10000;
    private static Long timeoutInMinutes = 30L;

    public String describeRegion() {
        DescribeRegionsRequest request = new DescribeRegionsRequest();
        request.setConnectTimeout(connectTimeout);
        request.setReadTimeout(readTimeout);
        request.setAcceptFormat(FormatType.JSON);
        HttpResponse response = null;
        String responseContent = null;
        try {
            response = client.doAction(request);
            responseContent = response.getHttpContentString();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return responseContent;
    }

    public String createStack(String stackName, String templateBody, List<CreateStackRequest.Parameters> parameterss) {
        CreateStackRequest request = new CreateStackRequest();
        request.setConnectTimeout(connectTimeout);
        request.setReadTimeout(readTimeout);
        request.setTimeoutInMinutes(timeoutInMinutes);
        request.setStackName(stackName);
        // 若模板较大,建议使用TemplateURL参数,避免URL过长,调用失败。
        // 您也可以继承CreateStackRequest类,通过调用putBodyParameter(protected方法)自行实现setTemplateBody方法。
        // request.putBodyParameter("TemplateBody", templateBody)
        request.setTemplateBody(templateBody);
        request.setParameterss(parameterss);

        HttpResponse response = null;
        String responseContent = null;
        try {
            response = client.doAction(request);
            responseContent = response.getHttpContentString();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return responseContent;
    }

    public String getStack(String stackId) {
        GetStackRequest request = new GetStackRequest();
        request.setConnectTimeout(connectTimeout);
        request.setReadTimeout(readTimeout);
        request.setStackId(stackId);
        request.setAcceptFormat(FormatType.JSON);
        HttpResponse response = null;
        String responseContent = null;
        try {
            response = client.doAction(request);
            responseContent = response.getHttpContentString();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return responseContent;
    }

    public String deleteStack(String stackId) {
        DeleteStackRequest request = new DeleteStackRequest();
        request.setConnectTimeout(connectTimeout);
        request.setReadTimeout(readTimeout);
        request.setStackId(stackId);
        request.setAcceptFormat(FormatType.JSON);
        HttpResponse response = null;
        String responseContent = null;
        try {
            response = client.doAction(request);
            responseContent = response.getHttpContentString();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return responseContent;
    }

    public static void main(String[] args) throws ClassNotFoundException {
        StackDemo stack = new StackDemo();
        String stackName = "MyStack";
        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" +
                "}";
        List<CreateStackRequest.Parameters> parameterss = new ArrayList<CreateStackRequest.Parameters>();
        CreateStackRequest.Parameters cidrBlock = new CreateStackRequest.Parameters();
        cidrBlock.setParameterKey("CidrBlock");
        cidrBlock.setParameterValue("192.168.0.0/16");
        CreateStackRequest.Parameters vpcName = new CreateStackRequest.Parameters();
        vpcName.setParameterKey("VpcName");
        vpcName.setParameterValue("TestVpc");
        parameterss.add(cidrBlock);
        parameterss.add(vpcName);
        stack.createStack(stackName, templateBody, parameterss);
        stack.describeRegion();
        stack.getStack("<yourStackId>");
        stack.deleteStack("<yourStackId>");
    }
}