Quick start
This topic describes how to use the CloudOps Orchestration Service (OOS) SDK for Go to perform common operations, such as creating a template, executing a template, and checking the execution result.
Create a template
Run the following code to create a template:
package main
import (
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/oos"
)
var client, err = oos.NewClientWithAccessKey("cn-hangzhou", "<AccessKeyId>", "<AccessKeySecret>")
var content = `
{
"FormatVersion": "OOS-2019-06-01",
"Description": "Descirbe instances of given status",
"Parameters": {
"Status": {
"Type": "String",
"Description": "(Required) The status of the Ecs instance."
}
},
"Tasks": [
{
"Properties": {
"Parameters": { "Status": "{{ Status }}" },
"API": "DescribeInstances",
"Service": "ECS"
},
"Name": "describeInstances",
"Action": "ACS::ExecuteAPI"
}
]
}`
func CreateTemplate(name string, content string) (response *oos.CreateTemplateResponse, err error) {
request := oos.CreateCreateTemplateRequest()
request.TemplateName = name
request.Content = content
response, err = client.CreateTemplate(request)
return
}
func main() {
response, err := CreateTemplate("MyTemplate", content)
fmt.Println(response)
fmt.Println(err)
}
Execute a template
Run the following code to execute a template:
package main
import (
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/oos"
)
var client, err = oos.NewClientWithAccessKey("cn-hangzhou", "<AccessKeyId>", "<AccessKeySecret>")
func StartExecution(templateName string, parameters string) (response *oos.StartExecutionResponse, err error) {
request := oos.CreateStartExecutionRequest()
request.TemplateName = templateName
request.Parameters = parameters
response, err = client.StartExecution(request)
return
}
func main() {
response, err := StartExecution("MyTemplate", "{\"Status\": \"Running\"}")
fmt.Println(response)
fmt.Println(err)
}
Check the execution result
Run the following code to check the execution result:
package main
import (
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/oos"
)
var client, err = oos.NewClientWithAccessKey("cn-hangzhou", "<AccessKeyId>", "<AccessKeySecret>")
func ListExecutions(executionId string) (response *oos.ListExecutionsResponse, err error) {
request := oos.CreateListExecutionsRequest()
request.ExecutionId = executionId
response, err = client.ListExecutions(request)
return
}
func main() {
response, err := ListExecutions("<ExecutionId>")
fmt.Println(response)
fmt.Println(err)
}