快速入门

本节介绍如何快速使用OOS Go SDK完成常见操作,如创建模板、启动执行、查询执行等。

创建模板

以下代码用于创建模板:

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()
	/*
  阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
  强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
  本示例使用了阿里云Credentials工具托管AccessKey,来实现API访问的身份验证。
  具体配置操作(或者配置环境变量),请参见https://help.aliyun.com/document_detail/378661.html。
	*/
	// 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)
}

启动执行

以下代码用于启动执行:

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()
	/*
  阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
  强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
  本示例使用了阿里云Credentials工具托管AccessKey,来实现API访问的身份验证。
  具体配置操作(或者配置环境变量),请参见https://help.aliyun.com/document_detail/378661.html。
	*/
	// 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)
}

查询执行

以下代码用于查询执行:

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()
	/*
  阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
  强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
  本示例使用了阿里云Credentials工具托管AccessKey,来实现API访问的身份验证。
  具体配置操作(或者配置环境变量),请参见https://help.aliyun.com/document_detail/378661.html。
	*/
	// 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)
}