This topic describes how to install Resource Orchestration Service (ROS) SDK for Go. This topic also provides sample code that you can run to use ROS SDK for Go.

Install Go

  1. Download and install Go.
    Note
    • For more information about how to download and install Go, see Downloads on the Go official website.
    • We recommend that you use Go 1.10.1 or later.
  2. Verify whether Go is installed.
    • Verify the version of Go.
      Run the following command:
      go version
      The following sample code shows the expected output:
      go version go1.10.1
    • Verify the settings of the GOPATH parameter.
      Run the following command:
      go env GOPATH
      The following sample code shows the expected output:
      /root/gos

Install ROS SDK for Go

  1. Download and install ROS SDK for Go.
    Note For more information about how to download and use ROS SDK for Go, see SDK overview.
  2. Initialize ROS SDK for Go.
    1. Import the required software packages.
      import (
        "os"
        ros20190910  "github.com/alibabacloud-go/ros-20190910/v3/client"
        openapi  "github.com/alibabacloud-go/darabonba-openapi/client"
        util  "github.com/alibabacloud-go/tea-utils/service"
        "github.com/alibabacloud-go/tea/tea"
      )
    2. Initialize the SDK client.
      func CreateClient (accessKeyId *string, accessKeySecret *string) (_result *ros20190910.Client, _err error) {
        config := &openapi.Config{
          // Your AccessKey ID.
          AccessKeyId: accessKeyId,
          // Your AccessKey secret.
          AccessKeySecret: accessKeySecret,
        }
        // The endpoint.
        config.Endpoint = tea.String("ros.aliyuncs.com")
        _result = &ros20190910.Client{}
        _result, _err = ros20190910.NewClient(config)
        return _result, _err
      }

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.

    func _main (args []*string) (_err error) {
      client, _err := CreateClient(tea.String("accessKeyId"), tea.String("accessKeySecret"))
      if _err != nil {
        return _err
      }
    
      describeRegionsRequest := &ros20190910.DescribeRegionsRequest{}
      runtime := &util.RuntimeOptions{}
      // Obtain the return values of the DescribeRegions operation if you want to copy and run the sample code.
      _, _err = client.DescribeRegionsWithOptions(describeRegionsRequest, runtime)
      if _err != nil {
        return _err
      }
      return _err
    }
  • Create a stack

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

    In this example, the following parameters are specified:
    • StackName: the name of the stack. The name must be unique within an Alibaba Cloud account.
    • TemplateBody: the body of the template.
    • Parameter: the parameters required to create the stack. You must specify both ParameterKey and ParameterValue.
    func CreateStackSample (client *ros20190910.Client, regionId *string, stackName *string, templateBody *string, parameter []*string) (_result *string, _err error) {
      request := &ros20190910.CreateStackRequest{
        RegionId: regionId,
        StackName: stackName,
        TemplateBody: templateBody,
        Parameters: []*ros20190910.CreateStackRequestParameters{parameter},
      }
      response, _err := client.CreateStack(request)
      if _err != nil {
        return _result, _err
      }
      _result = response.Body.StackId
      return _result , _err
    }
  • Query the information about a stack

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

    func GetStackSample (client *ros20190910.Client, regionId *string, stackId *string) (_result *string, _err error) {
      request := &ros20190910.GetStackRequest{
        RegionId: regionId,
        stackId: stackId,
      }
      response, _err := client.GetStack(request)
      if _err != nil {
        return _result, _err
      }
      _result = response.Body.StackId
      return _result , _err
    }
  • Delete a stack

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

    func DeleteStackSample (client *ros20190910.Client, regionId *string, stackId *string) (_err error) {
      request := &ros20190910.DeleteStackRequest{
        RegionId: regionId,
        stackId: stackId,
      }
      _, _err := client.DeleteStack(request)
      if _err != nil {
        return  _err
      }
      _result = response.Body.StackId
      return  _err
    }
  • Sample code

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

    package main
    
    import (
        "fmt"
        "os"
        openapi "github.com/alibabacloud-go/darabonba-openapi/client"
        ros20190910 "github.com/alibabacloud-go/ros-20190910/v3/client"
        console "github.com/alibabacloud-go/tea-console/client"
        util "github.com/alibabacloud-go/tea-utils/service"
        "github.com/alibabacloud-go/tea/tea"
    )
    
    /**
      
     * @param accessKeyId
     * @param accessKeySecret
     * @param regionId
     * @return Client
     * @throws Exception
     */
    func Initialization(accessKeyId *string, accessKeySecret *string, regionId *string) (_result *ros20190910.Client, _err error) {
        config := &openapi.Config{}
         
        config.AccessKeyId = accessKeyId
         
        config.AccessKeySecret = accessKeySecret
         
        config.RegionId = regionId
        _result = &ros20190910.Client{}
        _result, _err = ros20190910.NewClient(config)
        return _result, _err
    }
    
    /**
      
     * @param client
     * @param request
     * @throws Exception
     */
    func CreateStackSample(client *ros20190910.Client, regionId *string, stackName *string, templateBody *string, parameter *string) (_result *string, _err error) {
        paramMap := util.AssertAsMap(util.ParseJSON(parameter))
        param := &ros20190910.CreateStackRequestParameters{
            ParameterKey:   util.AssertAsString(paramMap["ParameterKey"]),
            ParameterValue: util.AssertAsString(paramMap["ParameterValue"]),
        }
        request := &ros20190910.CreateStackRequest{
            RegionId:     regionId,
            StackName:    stackName,
            TemplateBody: templateBody,
            Parameters:   []*ros20190910.CreateStackRequestParameters{param},
        }
        response, _err := client.CreateStack(request)
        if _err != nil {
            return _result, _err
        }
         
         
        _result = response.Body.StackId
        return _result, _err
    }
    
    /**
      
     * @param client
     * @param stackId
     * @throws Exception
     */
    func GetStackSample(client *ros20190910.Client, regionId *string, stackId *string, wait *bool) (_err error) {
        request := &ros20190910.GetStackRequest{
            RegionId: regionId,
            StackId:  stackId,
        }
        response, _err := client.GetStack(request)
        if _err != nil {
            return _err
        }
    
        body := response.Body
        fmt.Print(util.ToJSONString(tea.ToMap(response)))
        if tea.BoolValue(wait) {
            if tea.BoolValue(util.EqualString(body.Status, tea.String("CREATE_IN_PROGRESS"))) {
                 
                _err = util.Sleep(tea.Int(30000))
                if _err != nil {
                    return _err
                }
                _err = GetStackSample(client, regionId, stackId, wait)
                if _err != nil {
                    return _err
                }
            } else if tea.BoolValue(util.EqualString(body.Status, tea.String("CREATE_FAILED"))) {
                 
            } else if tea.BoolValue(util.EqualString(body.Status, tea.String("CREATE_COMPLETE"))) {
                 
                fmt.Print(tea.String("ID:" + tea.StringValue(body.StackId)))
            }
    
        } else {
            fmt.Print(tea.String("ID:" + tea.StringValue(body.StackId)))
             
        }
    
        return _err
    }
    
    func DeleteStackSample(client *ros20190910.Client, regionId *string, stackId *string) (_err error) {
        request := &ros20190910.DeleteStackRequest{
            RegionId: regionId,
            StackId:  stackId,
        }
        _, _err = client.DeleteStack(request)
        if _err != nil {
            return _err
        }
        return _err
    }
    
    func _main(args []*string) (_err error) {
      // access key id
      accessKeyiId := tea.String(`You access key id`)
       
        accessKeySecret := tea.String(`you access key secret`)
         
        regionId := tea.String(`cn-hangzhou`)
         
        stackName := tea.String(`MyTestStack`)
         
        templateBody := tea.String(`
          {
        "ROSTemplateFormatVersion": "2015-09-01",
        "Parameters": {
          "CidrBlock": {
            "Type": "String",
            "Description": "Vpc CidrBlock",
            "Label": "Vpc CidrBlock"
          }
        },
        "Resources": {
          "Vpc": {
            "Type": "ALIYUN::ECS::VPC",
            "Properties": {
              "CidrBlock": {
                "Ref": "CidrBlock"
              },
              "VpcName": {
                "Fn::Join": [
                  "-",
                  [
                    "StackId",
                    {
                      "Ref": "ALIYUN::StackId"
                    }
                  ]
                ]
              }
            }
          }
        }
      }`)
         
        parameter := tea.String(`{"ParameterKey": "CidrBlock", "ParameterValue": "192.168.XX.XX/16"}`)
    
         
        wait := tea.Bool(false)
        client, _err := Initialization(accessKeyiId, accessKeySecret, regionId)
        if _err != nil {
            return _err
        }
    
         
        stackId, _err := CreateStackSample(client, regionId, stackName, templateBody, parameter)
        if _err != nil {
            return _err
        }
    
        _err = util.Sleep(tea.Int(1000))
        if _err != nil {
            return _err
        }
         
        _err = GetStackSample(client, regionId, stackId, wait)
        if _err != nil {
            return _err
        }
        return _err
    
         
        _err = DeleteStackSample(client, regionId, stackId)
        if _err != nil {
            return _err
        }
        return _err
    }
    
    func main() {
        err := _main(tea.StringSlice(os.Args[1:]))
        if err != nil {
            panic(err)
        }
    }