This topic describes how to use the ROS Java SDK to create and manage stacks.

Background information

You can create and manage stacks by using the ROS console or API operations.

Preparations

  1. Download and install the Java SDK.
    Notice We recommend that you use JRE 1.8 or later.
  2. Add aliyun-java-sdk-core and other dependency packages to pom.xml.
    <dependencies>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ros</artifactId>
            <version>3.2.0</version>
        </dependency>
    </dependencies>
  3. Initialize the SDK.
    1. Import the following libraries:
      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. Initialize the SDK client.
      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;

Query the list of available regions

You can use the Java SDK to query the list of available regions.

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;
}

Create a stack

When creating a stack, you must specify the following parameters:
  • StackName: the name of the stack to be created. The specified name must be unique in a userspace.
  • TimeoutInMinutes: the amount of time that can elapse before the stack creation times out. If the stack is not created within the specified period of time, creation times out and fails. Unit: minutes.
  • TemplateBody: the template based on which the stack is created.
  • TemplateURL: the URL of the file that contains the template body. You must specify either the TemplateBody parameter or the TemplateURL parameter. But you cannot specify both of them.
  • Parameters: the parameters required to create the stack. Keys must be specified in the template.
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);
    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;
}

Query information about a stack

Enter the ID of a stack.

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;
}

Delete a stack

Enter the ID of a stack.

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;
}

Example

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);
        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>");
    }
}