全部产品
云市场

快速入门

更新时间:2019-06-19 20:16:49

快速入门

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

创建模板

以下代码用于创建模板:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/services/oos"
  5. )
  6. var client, err = oos.NewClientWithAccessKey("cn-hangzhou", "<AccessKeyId>", "<AccessKeySecret>")
  7. var content = `
  8. {
  9. "FormatVersion": "OOS-2019-06-01",
  10. "Description": "Descirbe instances of given status",
  11. "Parameters": {
  12. "Status": {
  13. "Type": "String",
  14. "Description": "(Required) The status of the Ecs instance."
  15. }
  16. },
  17. "Tasks": [
  18. {
  19. "Properties": {
  20. "Parameters": { "Status": "{{ Status }}" },
  21. "API": "DescribeInstances",
  22. "Service": "ECS"
  23. },
  24. "Name": "describeInstances",
  25. "Action": "ACS::ExecuteAPI"
  26. }
  27. ]
  28. }`
  29. func CreateTemplate(name string, content string) (response *oos.CreateTemplateResponse, err error) {
  30. request := oos.CreateCreateTemplateRequest()
  31. request.TemplateName = name
  32. request.Content = content
  33. response, err = client.CreateTemplate(request)
  34. return
  35. }
  36. func main() {
  37. response, err := CreateTemplate("MyTemplate", content)
  38. fmt.Println(response)
  39. fmt.Println(err)
  40. }

启动执行

以下代码用于启动执行:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/services/oos"
  5. )
  6. var client, err = oos.NewClientWithAccessKey("cn-hangzhou", "<AccessKeyId>", "<AccessKeySecret>")
  7. func StartExecution(templateName string, parameters string) (response *oos.StartExecutionResponse, err error) {
  8. request := oos.CreateStartExecutionRequest()
  9. request.TemplateName = templateName
  10. request.Parameters = parameters
  11. response, err = client.StartExecution(request)
  12. return
  13. }
  14. func main() {
  15. response, err := StartExecution("MyTemplate", "{\"Status\": \"Running\"}")
  16. fmt.Println(response)
  17. fmt.Println(err)
  18. }

查询执行

以下代码用于查询执行:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/services/oos"
  5. )
  6. var client, err = oos.NewClientWithAccessKey("cn-hangzhou", "<AccessKeyId>", "<AccessKeySecret>")
  7. func ListExecutions(executionId string) (response *oos.ListExecutionsResponse, err error) {
  8. request := oos.CreateListExecutionsRequest()
  9. request.ExecutionId = executionId
  10. response, err = client.ListExecutions(request)
  11. return
  12. }
  13. func main() {
  14. response, err := ListExecutions("<ExecutionId>")
  15. fmt.Println(response)
  16. fmt.Println(err)
  17. }