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

Install ROS SDK for Python

  1. Download and install ROS SDK for Python.
    Note For more information, see SDK overview.
  2. Initialize ROS SDK for Python.
    1. Import the required libraries.
      import sys
      from typing import List
      from alibabacloud_ros20190910.client import Client as ROS20190910Client
      from alibabacloud_tea_openapi import models as open_api_models
      from alibabacloud_ros20190910 import models as ros20190910_models
    2. Initialize the SDK client.
      AK = '<yourAccessKeyId>'
      SECRET = '<yourAccessKeySecrect>'
      Region = '<yourRegionId>'
      client = AcsClient(AK, SECRET, Region) 

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.

    def describe_region():
        """describe regions list """
        request = DescribeRegionsRequest()
        request.set_connect_timeout(10000)
        request.set_read_timeout(10000)
        request.set_accept_format("json")
        response = client.do_action_with_exception(request)
    
        return response.decode("utf-8")
  • Create a stack

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

    To create a stack, you must configure the following parameters:

    • StackName: the name of the stack. The name must be unique within your Alibaba Cloud account.
    • TimeoutInMinutes: the timeout period for creating the stack. Unit: minutes. If a stack is not created within the specified period of time, the stack fails to be created.
    • TemplateBody: the main structure of the template.
    • Parameters: the parameters required to create the stack. You must specify both ParameterKey and ParameterValue.

    Sample code for configuring parameters:

    stack_name = "MyStack"
    timeout = 10
    template_body = """
        {
          "ROSTemplateFormatVersion": "2015-09-01",
          "Parameters": {
            "VpcName": {
              "Type": "String",
              "Description": "Vpc Name",
              "Label": "Vpc Name"
            },
            "CidrBlock": {
              "Type": "String",
              "Description": "Vpc CidrBlock",
              "Label": "Vpc CidrBlock"
             }
            },
          "Resources": {
            "Vpc": {
              "Type": "ALIYUN::ECS::VPC",
              "Properties": {
                "CidrBlock": {
                  "Ref": "CidrBlock"
                },
                "VpcName": {
                  "Ref": "VpcName"
                }
              }
            }
          }
        }
    """
    params = [
        {
            "ParameterValue": "192.168.0.0/16",
            "ParameterKey": "CidrBlock"
        },
        {
            "ParameterValue": "TestVpc",
            "ParameterKey": "VpcName"
        }
    ]
    Sample code for creating the stack:
    def create_stack():
        """create stack"""
        request = CreateStackRequest()
        request.set_accept_format("json")
        request.set_StackName(stack_name)
        request.set_TimeoutInMinutes(timeout)
        # If the length of the template body is longer than required, we recommend that you use the TemplateURL parameter. 
        # You can also add parameters to the HTTP POST request body to prevent request failures due to excessive length of URLs. 
        # request.set_method("POST")
        # request.add_body_params("TemplateBody", template_body)
        request.set_TemplateBody(template_body)
        request.set_Parameterss(params)
        response = client.do_action_with_exception(request)
    
        return response.decode("utf-8")
  • 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.

    def get_stack():
        """get descriptions of the stack"""
        request = GetStackRequest()
        request.set_accept_format("json")
        request.set_StackId(stack_id)
        response = client.do_action_with_exception(request)
    
        return response.decode("utf-8")
  • Delete a stack

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

    def delete_stack():
        """delete stack"""
        request = DeleteStackRequest()
        request.set_accept_format("json")
        request.set_StackId(stack_id)
        response = client.do_action_with_exception(request)
    
        return response.decode("utf-8")
  • Full sample code

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

    import json
    from time import sleep
    from aliyunsdkcore.client import AcsClient
    from aliyunsdkros.request.v20190910.CreateStackRequest import CreateStackRequest
    from aliyunsdkros.request.v20190910.GetStackRequest import GetStackRequest
    from aliyunsdkros.request.v20190910.DeleteStackRequest import DeleteStackRequest
    from aliyunsdkros.request.v20190910.DescribeRegionsRequest import DescribeRegionsRequest
    from aliyunsdkros.request.v20190910.ListStacksRequest import ListStacksRequest
    
    
    AK = '<yourAccessKeyId>'
    SECRET = '<yourAccessKeySecret>'
    Region = '<yourRegionId>'  # Examples: 'cn-beijing' and 'cn-hangzhou'. 
    
    client = AcsClient(AK, SECRET, Region)
    
    
    def describe_region():
        """describe regions list """
        request = DescribeRegionsRequest()
        request.set_connect_timeout(10000)
        request.set_read_timeout(10000)
        request.set_accept_format("json")
        response = client.do_action_with_exception(request)
    
        return response.decode("utf-8")
    
    
    def create_stack(stack_name, timeout, template_body, params=[]):
        """create stack"""
        request = CreateStackRequest()
        request.set_accept_format("json")
        request.set_StackName(stack_name)
        request.set_TimeoutInMinutes(timeout)
        # If the length of the template body is longer than required, we recommend that you use the TemplateURL parameter. 
        # You can also add parameters to the HTTP POST request body to prevent request failures due to excessive length of URLs. 
        # request.set_method("POST")
        # request.add_body_params("TemplateBody", template_body)
        request.set_TemplateBody(template_body)
        response = client.do_action_with_exception(request)
    
        return response.decode("utf-8")
    
    
    def get_stack(stack_id):
        """get descriptions of the stack"""
        request = GetStackRequest()
        request.set_accept_format("json")
        request.set_StackId(stack_id)
        response = client.do_action_with_exception(request)
    
        return response.decode("utf-8")
    
    
    def delete_stack(stack_id):
        """delete stack"""
        request = DeleteStackRequest()
        request.set_accept_format("json")
        request.set_StackId(stack_id)
        response = client.do_action_with_exception(request)
    
        return response.decode("utf-8")
    
    
    if __name__ == '__main__':
        test_template = """
            {
              "ROSTemplateFormatVersion": "2015-09-01",
              "Parameters": {
                "VpcName": {
                  "Type": "String",
                  "Description": "Vpc Name",
                  "Label": "Vpc Name"
                },
                "CidrBlock": {
                  "Type": "String",
                  "Description": "Vpc CidrBlock",
                  "Label": "Vpc CidrBlock"
                 }
                },
              "Resources": {
                "Vpc": {
                  "Type": "ALIYUN::ECS::VPC",
                  "Properties": {
                    "CidrBlock": {
                      "Ref": "CidrBlock"
                    },
                    "VpcName": {
                      "Ref": "VpcName"
                    }
                  }
                }
              }
            }
        """
    
        parameters = [
            {"ParameterKey": "CidrBlock", "ParameterValue": "192.168.0.0/16"},
            {"ParameterKey": "VpcName", "ParameterValue": "TestVpc"}
        ]
    
        describe_region()
        stack = create_stack('MyStack', 10, test_template, parameters)
        get_stack(json.loads(stack)["StackId"])
        sleep(3)  # Wait for the stack to be created. 
        delete_stack(json.loads(stack)["StackId"])