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. 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 Go
Download and install Go.
NoteFor more information about how to download and install Go, see All releases 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 configuration 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" "github.com/aliyun/credentials-go/credentials" )
Initialize the SDK client.
func Initialization(regionId *string) (_result *ros20190910.Client, _err error) { // Initialize credentials. credential, _err := credentials.NewCredential(nil) if _err != nil { panic(_err) } config := &openapi.Config{ // Configure credentials. Credential: credential, // Specify the endpoint of ROS. Endpoint: tea.String("ros.aliyuncs.com"), } // Specify the region ID. config.RegionId = regionId _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{} // Obtain the return values of the API operation after you 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, 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 the 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 both ParameterKey and ParameterValue.
The following sample code provides an example on how to specify the parameters:
// Specify the region ID of the stack. regionId := tea.String("cn-shenzhen") // Specify the stack name. stackName := tea.String("MyStack") // Specify 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 stack ID.
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 stack ID.
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 create, query, and delete a stack:
NoteBefore you call the operations, 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 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" "github.com/aliyun/credentials-go/credentials" ) /** * Initialize the client. * @param regionId * @return Client * @throws Exception */ func Initialization(regionId *string) (_result *ros20190910.Client, _err error) { // Initialize credentials. credential, _err := credentials.NewCredential(nil) if _err != nil { panic(_err) } config := &openapi.Config{ // Configure credentials. Credential: credential, // Specify the endpoint of ROS. Endpoint: tea.String("ros.aliyuncs.com"), } // Specify the region ID. config.RegionId = regionId _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. 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. Information:" + 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("Status:" + 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) { // Specify the region ID. regionId := tea.String("cn-shenzhen") // Specify the stack name. stackName := tea.String("MyStack") // Specify 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(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) } }