This topic describes how to install Resource Orchestration Service (ROS) SDK for Java. This topic also provides sample code that you can reuse where applicable.

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 the following dependencies to the pom.xml file:
    <dependencies>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>tea-openapi</artifactId>
            <version>0.0.19</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>ros20190910</artifactId>
            <version>1.0.1</version>
        </dependency>
    </dependencies>
  3. Initialize ROS SDK for Java.
    1. Import the required libraries.
      import com.aliyun.tea.*;
      import com.aliyun.ros20190910.*;
      import com.aliyun.ros20190910.models.*;
      import com.aliyun.teaopenapi.*;
      import com.aliyun.teaopenapi.models.*;
    2. Initialize the SDK client.
      public static com.aliyun.ros20190910.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
              Config config = new Config()
                      // Your AccessKey ID.
                      .setAccessKeyId(accessKeyId)
                      // Your AccessKey secret.
                      .setAccessKeySecret(accessKeySecret);
              // The endpoint.
              config.endpoint = "ros.aliyuncs.com";
              return new com.aliyun.ros20190910.Client(config);
          }

Examples

  • Query the list of available regions

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

    public static void describeRegions(String[] args_) throws Exception {
            java.util.List<String> args = java.util.Arrays.asList(args_);
            com.aliyun.ros20190910.Client client = Sample.createClient("accessKeyId", "accessKeySecret");
            DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
            DescribeRegionsResponse resp = client.describeRegions(describeRegionsRequest);
        }
  • Create a stack
    You can call the CreateStack operation to create a stack. For more information about the CreateStack operation, see CreateStack.
    public static void createStack(String stackName, String regionId) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);    
        CreateStackRequest.CreateStackRequestParameters parameters0 = new CreateStackRequest.CreateStackRequestParameters()            
            .setParameterKey("VpcName")            
            .setParameterValue("TestVpc");    
        CreateStackRequest.CreateStackRequestParameters parameters1 = new CreateStackRequest.CreateStackRequestParameters()            
            .setParameterKey("CidrBlock")            
            .setParameterValue("192.168.0.0/16");    
        CreateStackRequest createStackRequest = new CreateStackRequest()            
            .setTemplateBody("{\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" +                    
                "}")            
            .setStackName("MyStack")            
            .setRegionId("cn-hangzhou")            
            .setTimeoutInMinutes(40L)            
            .setParameters(java.util.Arrays.asList(                    
                parameters0,                    
                parameters1            
            ));    
            // Print the return value of the API operation after the sample code is run.     
            client.createStack(createStackRequest);
    }
  • Query information about a stack

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

    public static void getStack(String stackId, String regionId) throws Exception {        
        GetStackRequest getStackRequest = new GetStackRequest()            
            .setStackId(stackId)            
            .setRegionId(regionId);    
            // Print the return value of the API operation after the sample code is run.     
        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 about the DeleteStack operation, see DeleteStack.

    public static void deleteStack(String stackId, String regionId) throws Exception {    
        DeleteStackRequest deleteStackRequest = new DeleteStackRequest()            
            .setStackId(stackId)            
            .setRegionId(regionId);    
            // Print the return value of the API operation after the sample code is run.     
        DeleteStackResponse resp = client.deleteStack(deleteStackRequest);    
        com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
    }
  • Sample code

    The following sample code shows how to query the list of available regions and how to create, query, and delete a stack:

    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.*;
    
    public class Sample {
    
        /**
         * Use your AccessKey ID and AcecssKey secret to initialize the client.
         *
         * @param accessKeyId
         * @param accessKeySecret
         * @return Client
         * @throws Exception
         */
        private static String ACCESSKEYID = "<yourAccessKeyId>";
        private static String SECRET = "<yourAccessKeySecrect>";
        private static String REGIONID = "<yourRegionId>";
    
        public static com.aliyun.ros20190910.Client createClient() throws Exception {
            Config config = new Config()
                    // Your AccessKey ID.
                    .setAccessKeyId(ACCESSKEYID)
                    // Your AccessKey secret.
                    .setAccessKeySecret(SECRET);
            // The endpoint.
            config.endpoint = "ros.aliyuncs.com";
            return new com.aliyun.ros20190910.Client(config);
        }
    
        public static void describeRegions(Client client) throws Exception {
            DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
            // Print the return value of the API operation after the sample code is run. 
            DescribeRegionsResponse resp = client.describeRegions(describeRegionsRequest);
        }
    
        public static void getStack(Client client, String stackId, String regionId) throws Exception {
            GetStackRequest getStackRequest = new GetStackRequest()
                    .setStackId(stackId)
                    .setRegionId(regionId);
            // Print the return value of the API operation after the sample code is run. 
            GetStackResponse resp = client.getStack(getStackRequest);
            com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
        }
    
        public static void deleteStack(Client client, String stackId, String regionId) throws Exception {
            DeleteStackRequest deleteStackRequest = new DeleteStackRequest()
                    .setStackId(stackId)
                    .setRegionId(regionId);
            // Print the return value of the API operation after the sample code is run. 
            DeleteStackResponse resp = client.deleteStack(deleteStackRequest);
            com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
        }
    
        public static void main(String[] args_) throws Exception {
            java.util.List<String> args = java.util.Arrays.asList(args_);
            com.aliyun.ros20190910.Client client = Sample.createClient();
            CreateStackRequest.CreateStackRequestParameters parameters0 = new CreateStackRequest.CreateStackRequestParameters()
                    .setParameterKey("VpcName")
                    .setParameterValue("TestVpc");
            CreateStackRequest.CreateStackRequestParameters parameters1 = new CreateStackRequest.CreateStackRequestParameters()
                    .setParameterKey("CidrBlock")
                    .setParameterValue("192.168.0.0/16");
            CreateStackRequest createStackRequest = new CreateStackRequest()
                    .setTemplateBody("{\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" +
                            "}")
                    .setStackName("stackName")
                    .setRegionId("cn-hangzhou")
                    .setTimeoutInMinutes(40L)
                    .setParameters(java.util.Arrays.asList(
                            parameters0,
                            parameters1
                    ));
            // Print the return value of the API operation after the sample code is run. 
            client.createStack(createStackRequest);
            Sample.describeRegions(client);
            Sample.getStack(client, "yourStackId", REGIONID);
            Sample.deleteStack(client, "yourStackId", REGIONID);
        }
    }