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
Download and install Go.
NoteFor more information about how to download and install Go, see Downloads on the Go official website.
We recommend that you use Go 1.13 or later.
Verify whether Go is installed.
Verify the version of Go.
Run the following command:
go version
Expected output:
go version go1.20.2
Verify the settings of the GOPATH parameter.
Run the following command:
go env GOPATH
Expected output:
/root/go
Install ROS SDK for Go
Download and install ROS SDK for Go.
NoteFor more information about how to download and use ROS SDK for Go, see SDK overview.
Initialize ROS SDK for Go.
Import the required software packages.
import ( "os" ros20190910 "github.com/alibabacloud-go/ros-20190910/v4/client" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" console "github.com/alibabacloud-go/tea-console/client" util "github.com/alibabacloud-go/tea-utils/v2/service" "github.com/alibabacloud-go/tea/tea" )
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 domain name that you want to access. config.Endpoint = tea.String("ros.aliyuncs.com") _result = &ros20190910.Client{} _result, _err = ros20190910.NewClient(config) return _result, _err }
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.
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{} // After you copy and run the sample code, obtain the return values of the API operation. _, _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, see CreateStack.
In this example, the following parameters are specified:
regionId: the region ID of the stack.
stackName: the name of the stack. 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 body of the template.
parameters: the parameters that are required to create the stack. You must specify both ParameterKey and ParameterValue.
The following sample code provides an example on how to specify the parameters:
// The region ID of the stack. regionId := tea.String("cn-shenzhen") // The name of the stack. stackName := tea.String("MyStack") // The timeout period for creating the stack. timeoutInMinutes := tea.Int64(40) // Create the parameter template. parameters0 := &ros20190910.CreateStackRequestParameters{ ParameterKey: tea.String("VpcName"), ParameterValue: tea.String("test_vpc"), } parameters1 := &ros20190910.CreateStackRequestParameters{ ParameterKey: tea.String("CidrBlock"), ParameterValue: tea.String("192.168.0.0/16"), } parameters := []*ros20190910.CreateStackRequestParameters{parameters0, parameters1} // Create the stack template. templateBody := tea.String(` { "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" } } } } }`) // Wait until the stack is created. wait := tea.Bool(true)
The following sample code provides an example on how to create a stack:
func CreateStackSample (client *ros20190910.Client, regionId *string, stackName *string, templateBody *string, timeoutInMinutes *int64, parameters []*string) (_result *string, _err error) { request := &ros20190910.CreateStackRequest{ RegionId: regionId, StackName: stackName, TemplateBody: templateBody, TimeoutInMinutes: timeoutInMinutes, Parameters: parameters, } 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, see GetStack.
In this example, the following parameters are specified:
regionId: the region ID of the stack.
stackId: the ID of the stack.
The following sample code provides an example on how to query the information about a stack:
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, see DeleteStack.
In this example, the following parameters are specified:
regionId: the region ID of the stack.
stackId: the ID of the stack.
The following sample code provides an example on how to delete a stack:
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
}
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:
package main import ( "os" console "github.com/alibabacloud-go/tea-console/client" ros20190910 "github.com/alibabacloud-go/ros-20190910/v4/client" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" util "github.com/alibabacloud-go/tea-utils/v2/service" "github.com/alibabacloud-go/tea/tea" ) /** * Use your AccessKey ID and AcecssKey secret to initialize the client. * @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{} // Your AccessKey ID. config.AccessKeyId = accessKeyId // Your AccessKey secret. config.AccessKeySecret = accessKeySecret // The ID of the region. config.RegionId = regionId _result = &ros20190910.Client{} _result, _err = ros20190910.NewClient(config) return _result, _err } /** * Create a stack. * @param client * @param request * @throws Exception */ func CreateStackSample(client *ros20190910.Client, regionId *string, stackName *string, templateBody *string, timeoutInMinutes *int64, parameters []*ros20190910.CreateStackRequestParameters ) (_result *string, _err error) { request := &ros20190910.CreateStackRequest{ RegionId: regionId, StackName: stackName, TemplateBody: templateBody, TimeoutInMinutes: timeoutInMinutes, Parameters: parameters, } response, _err := client.CreateStack(request) if _err != nil { return _result, _err } console.Log(tea.String("Creating and Stack ID:" + tea.StringValue(response.Body.StackId))) _result = response.Body.StackId return _result, _err } /** * Query the information about a stack. * @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 console.Log(util.ToJSONString(tea.ToMap(response))) if tea.BoolValue(wait) { if tea.BoolValue(util.EqualString(body.Status, tea.String("CREATE_IN_PROGRESS"))) { console.Log(tea.String("Creating......")) _err = util.Sleep(tea.Int(10000)) 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"))) { console.Log(tea.String("Creation Failed and Error Message:" + tea.StringValue(body.StatusReason) + ", requestId:" + tea.StringValue(body.RequestId))) } else if tea.BoolValue(util.EqualString(body.Status, tea.String("CREATE_COMPLETE"))) { console.Log(tea.String("Created")) console.Log(tea.String("ID:" + tea.StringValue(body.StackId))) } } else { console.Log(tea.String("ID:" + tea.StringValue(body.StackId))) console.Log(tea.String("State:" + tea.StringValue(body.Status))) console.Log(util.ToJSONString(tea.ToMap(response))) } return _err } func DeleteStackSample(client *ros20190910.Client, regionId *string, stackId *string) (_err error) { request := &ros20190910.DeleteStackRequest{ RegionId: regionId, StackId: stackId, } response, _err := client.DeleteStack(request) if _err != nil { return _err } console.Log(util.ToJSONString(tea.ToMap(response))) return _err } func _main(args []*string) (_err error) { // access key id accessKeyiId := tea.String(`you access key id`) // access key secret accessKeySecret := tea.String(`you access key secret`) // The region ID. regionId := tea.String("cn-shenzhen") // The name of the stack. stackName := tea.String("MyStack") // The timeout period for creating the stack. timeoutInMinutes := tea.Int64(40) // Create the parameter template. parameters0 := &ros20190910.CreateStackRequestParameters{ ParameterKey: tea.String("VpcName"), ParameterValue: tea.String("test_vpc"), } parameters1 := &ros20190910.CreateStackRequestParameters{ ParameterKey: tea.String("CidrBlock"), ParameterValue: tea.String("192.168.0.0/16"), } parameters := []*ros20190910.CreateStackRequestParameters{parameters0, parameters1} // Create the stack template. templateBody := tea.String(` { "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" } } } } }`) // Wait until the stack is created. wait := tea.Bool(true) client, _err := Initialization(accessKeyiId, accessKeySecret, regionId) if _err != nil { return _err } console.Log(tea.String("--------------------Create a stack.--------------------")) stackId, _err := CreateStackSample(client, regionId, stackName, templateBody, timeoutInMinutes, parameters) if _err != nil { return _err } _err = util.Sleep(tea.Int(1000)) if _err != nil { return _err } console.Log(tea.String("--------------------Stack" + tea.StringValue(stackName) + "Information--------------------")) _err = GetStackSample(client, regionId, stackId, wait) if _err != nil { return _err } console.Log(tea.String("--------------------Delete a stack.--------------------")) _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) } }