This topic describes how to use CloudOps Orchestration Service (OOS) SDK for Go to perform common operations, such as creating templates, starting executions, and querying execution results.
Create a template
The following sample code shows how to create a template:
package main
import (
"fmt"
"os"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
oos "github.com/aliyun/alibaba-cloud-sdk-go/services/oos"
)
func main() {
config := sdk.NewConfig()
/*
The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M.
We recommend that you do not hard-code the AccessKey ID and AccessKey secret into your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account is compromised.
In this example, Alibaba Cloud Credentials is used to manage the AccessKey pair to authenticate API access.
*/
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
credential := credentials.NewAccessKeyCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
/* use STS Token
credential := credentials.NewStsTokenCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), os.Getenv("ALIBABA_CLOUD_SECURITY_TOKEN"))
*/
client, err := oos.NewClientWithOptions("cn-hangzhou", config, credential)
if err != nil {
panic(err)
}
request := oos.CreateCreateTemplateRequest()
request.Scheme = "https"
request.TemplateName = "MyTemplate"
request.Content = "{\n \\\"FormatVersion\\\": \\\"OOS-2019-06-01\\\",\n \\\"Description\\\": \\\"Describe instances of given status\\\",\n \\\"Parameters\\\": {\n \\\"Status\\\": {\n \\\"Type\\\": \\\"String\\\",\n \\\"Description\\\": \\\"(Required) The status of the Ecs instance.\\\"\n }\n },\n \\\"Tasks\\\": [\n {\n \\\"Properties\\\": {\n \\\"Parameters\\\": { \\\"Status\\\": \\\"{{ Status }}\\\" },\n \\\"API\\\": \\\"DescribeInstances\\\",\n \\\"Service\\\": \\\"ECS\\\"\n },\n \\\"Name\\\": \\\"describeInstances\\\",\n \\\"Action\\\": \\\"ACS::ExecuteAPI\\\"\n }\n ]\n}"
response, err := client.CreateTemplate(request)
if err != nil {
fmt.Print(err.Error())
}
fmt.Printf("response is %#v\n", response)
}
Start an execution
The following sample code shows how to start an execution:
package main
import (
"fmt"
"os"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
oos "github.com/aliyun/alibaba-cloud-sdk-go/services/oos"
)
func main() {
config := sdk.NewConfig()
/*
The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M.
We recommend that you do not hard-code the AccessKey ID and AccessKey secret into your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account is compromised.
In this example, Alibaba Cloud Credentials is used to manage the AccessKey pair to authenticate API access.
*/
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
credential := credentials.NewAccessKeyCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
/* use STS Token
credential := credentials.NewStsTokenCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), os.Getenv("ALIBABA_CLOUD_SECURITY_TOKEN"))
*/
client, err := oos.NewClientWithOptions("cn-hangzhou", config, credential)
if err != nil {
panic(err)
}
request := oos.CreateStartExecutionRequest()
request.Scheme = "https"
request.TemplateName = "MyTemplate"
response, err := client.StartExecution(request)
if err != nil {
fmt.Print(err.Error())
}
fmt.Printf("response is %#v\n", response)
}Query the execution result
The following sample code shows how to query the result of an execution:
package main
import (
"fmt"
"os"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
oos "github.com/aliyun/alibaba-cloud-sdk-go/services/oos"
)
func main() {
config := sdk.NewConfig()
/*
The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M.
We recommend that you do not hard-code the AccessKey ID and AccessKey secret into your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account is compromised.
In this example, Alibaba Cloud Credentials is used to manage the AccessKey pair to authenticate API access.
*/
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
credential := credentials.NewAccessKeyCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
/* use STS Token
credential := credentials.NewStsTokenCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), os.Getenv("ALIBABA_CLOUD_SECURITY_TOKEN"))
*/
client, err := oos.NewClientWithOptions("cn-hangzhou", config, credential)
if err != nil {
panic(err)
}
request := oos.CreateListExecutionsRequest()
request.Scheme = "https"
request.ExecutionId = "<ExecutionId>"
response, err := client.ListExecutions(request)
if err != nil {
fmt.Print(err.Error())
}
fmt.Printf("response is %#v\n", response)
}