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

Background information

You can also use the ROS console or call API operations to create and manage stacks.

Preparations

  1. Download and install SDK for Java.
    Note 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 required 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 SDK for Java 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 you create 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 timeout period for the stack creation request. Unit: minutes. If the stack is not created within the specified period of time, creation times out and fails.
  • TemplateBody: the template based on which the stack is created.
  • TemplateURL: the URL of the file that contains the template body. You must specify one of the TemplateBody and TemplateURL parameters, but you cannot specify both of them.
  • Parameters: the parameters required to create the stack. Keys must be defined 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);
    // If the length of the template body is longer than required, we recommend that you use the TemplateURL parameter to avoid request failures due to excessive length of URLs.
    // If you want to use the TemplateBody parameter, you can inherit the CreateStackRequest class and call the protected putBodyParameter method.
    // 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;
}

Query information about a stack

Enter the ID of the target 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 the target 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);
        // If the length of the template body is longer than required, we recommend that you use the TemplateURL parameter to avoid request failures due to excessive length of URLs.
        // If you want to use the TemplateBody parameter, you can inherit the CreateStackRequest class and call the protected putBodyParameter method.
        // 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>");
    }
}