All Products
Search
Document Center

Resource Orchestration Service:Use ROS SDK for Java

Last Updated:Jan 05, 2024

This topic describes how to install Resource Orchestration Service (ROS) SDK for Java. This topic also provides sample code that you can run to use ROS SDK for Java. The sample code provides examples on how to query a list of available regions, and create, query, and delete a stack by using specific ROS API operations. This way, you can quickly familiarize yourself with the method to use the ROS API operations.

Install ROS SDK for Java

  1. Download and install ROS SDK for Java.

    Note
    • For more information about how to download and use ROS SDK for Java, see SDK overview.

    • We recommend that you use JRE 1.8 or later.

  2. Add dependencies to the pom.xml file.

    <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>
  3. Initialize ROS SDK for Java.

    1. Import the required software packages.

      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.*;
    2. Initialize the SDK client.

      public static com.aliyun.ros20190910.Client createClient() throws Exception {
              // Use the default credential to initialize the credential client.
              com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
              Config config = new Config()
                      // Specify the endpoint of ROS.
                      .setEndpoint("ros.aliyuncs.com")
                      // Configure credentials.
                      .setCredential(credentialClient);
              return new com.aliyun.ros20190910.Client(config);
          }

Examples

  • Query a list of available regions

    You can call the DescribeRegions operation to query a list of available regions. For more information, see DescribeRegions.

    public static void describeRegions(Client client) throws Exception {
            DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
            // After you copy and run the sample code, obtain the return values of the API operation. 
            DescribeRegionsResponse resp = client.describeRegions(describeRegionsRequest);
        }
  • Create a stack

    You can call the CreateStack operation to create a stack. For more information, see CreateStack.

    In this example, the following parameters are specified:

    • regionId: the region ID of the stack.

    • stackName: the stack name. The name must be unique within an Alibaba Cloud account.

    • timeoutInMinutes: the timeout period for creating the stack. Unit: minutes. If a stack is not created within the specified timeout period, the stack fails to be created.

    • templateBody: the template body.

    • parameters: the parameters that are required to create the stack. You must specify ParameterKey and ParameterValue.

    The following sample code provides an example on how to specify the parameters:

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

    The following sample code provides an example on how to create a stack:

    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);
         // After you copy and run the sample code, obtain the return values of the API operation. 
         CreateStackResponse resp = client.createStack(createStackRequest);
         com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
         return resp.body.stackId;
    }
  • Query the information about a stack

    You can call the GetStack operation to query the information about a stack. For more information, see GetStack.

    In this example, the following parameters are specified:

    • stackId: the stack ID.

    • regionId: the region ID of the stack.

    The following sample code provides an example on how to query the information about a stack:

public static void getStack(Client client, String stackId, String regionId) throws Exception {        
    GetStackRequest getStackRequest = new GetStackRequest()            
        .setStackId(stackId)            
        .setRegionId(regionId);    
        // After you copy and run the sample code, obtain the return values of the API operation.     
    GetStackResponse resp = client.getStack(getStackRequest);    
    com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
}
  • Delete a stack

    You can call the DeleteStack operation to delete a stack. For more information, see DeleteStack.

    In this example, the following parameters are specified:

    • regionId: the region ID of the stack.

    • stackId: the stack ID.

  • The following sample code provides an example on how to delete a stack:

public static void deleteStack(Client client, String stackId, String regionId) throws Exception {    
    DeleteStackRequest deleteStackRequest = new DeleteStackRequest()
                .setStackId(stackId)
                .setRegionId(regionId);
    // After you copy and run the sample code, obtain the return values of the API operation. 
    DeleteStackResponse resp = client.deleteStack(deleteStackRequest);
    com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
}
  • Complete sample code

    The following sample code provides an example on how to query a list of available regions and how to create, query, and delete a stack:

    Note

    Before you call the operation, you must configure environment variables and obtain access credentials from the environment variables. For more information, see Configure access credentials.

    The environment variables of the AccessKey ID and the AccessKey secret are ALIBABA_CLOUD_ACCESS_KEY_ID and 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 {
    
        /**
         * Initialize the client.
         * @return Client
         * @throws Exception
         */
        private static String REGIONID = "Region Id";
    
        public static com.aliyun.ros20190910.Client createClient() throws Exception {
            // Use the default credential to initialize the credential client. 
            com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
            Config config = new Config()
                    // Specify the endpoint of ROS.
                    .setEndpoint("ros.aliyuncs.com")
                    // Configure credentials.
                    .setCredential(credentialClient);
            return new com.aliyun.ros20190910.Client(config);
        }
    
        public static void describeRegions(Client client) throws Exception {
            DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
            // After you copy and run the sample code, obtain the return values of the API operation. 
            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);
            // After you copy and run the sample code, obtain the return values of the API operation. 
            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("Creating......");
                    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("Creation failed. Information:" + body.statusReason + ", requestId:" + body.requestId + "");
                } else if (com.aliyun.teautil.Common.equalString(body.status, "CREATE_COMPLETE")) {
                    com.aliyun.teaconsole.Client.log("Created");
                    com.aliyun.teaconsole.Client.log("ID:" + body.stackId + "");
                }
    
            } else {
                com.aliyun.teaconsole.Client.log("ID:" + body.stackId + "");
                com.aliyun.teaconsole.Client.log("Status:" + body.status + "");
            }
        }
    
    
        public static void deleteStack(Client client, String stackId, String regionId) throws Exception {
            DeleteStackRequest deleteStackRequest = new DeleteStackRequest()
                    .setStackId(stackId)
                    .setRegionId(regionId);
            // After you copy and run the sample code, obtain the return values of the API operation. 
            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);
            // After you copy and run the sample code, obtain the return values of the API operation. 
            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);
        }
    }