This topic describes how to install and use the CloudFlow SDK for Go. It provides examples in Go on how to call CloudFlow API operations to create a workflow, query workflow information, and asynchronously start a workflow execution. This topic also provides complete integration steps.
Prerequisites
An AccessKey pair is required to call Alibaba Cloud OpenAPI operations. Make sure that you have created an AccessKey pair. For more information, see Create an AccessKey pair. To prevent credential leaks, we recommend that you store your credentials in environment variables. For more information about security best practices, see Best practices for using access credentials to call Alibaba Cloud OpenAPI operations.
Environment requirements
Go 1.10.x or later is required.
Step 1: Import the SDK
Alibaba Cloud SDK supports generic and specialized calls to OpenAPI, and the SDK that you need to import varies based on the call method. For more information, see Generic and specialized calls.
Specialized calls
You can visit OpenAPI Explorer, find the service that you want to use, and view the supported languages and installation methods. Then, import the SDK of the service to your project. In this example, CloudFlow SDK for Go is imported. Perform the following steps:
Go to CloudFlow SDK.
In the All Languages section, select the language that you want to use.
Select an Installation Method and copy the code to your project.
Add the dependency to your project.
The installation process for the CloudFlow (FNF) product for Go is as follows:
go get github.com/alibabacloud-go/fnf-20190315/v2Generic calls
Generic calls do not depend on the SDK of any service and rely only on the core package github.com/alibabacloud-go/darabonba-openapi/v2/client. The installation method for Go is as follows. For the latest version, see darabonba-openapi.
go get github.com/alibabacloud-go/darabonba-openapi/v2/clientStep 2: Initialize a request client
Specify the correct endpoint for CloudFlow based on the region. For more information about endpoints, see Supported regions.
The following section provides sample code on how to initiate a specialized call. For information about how to initiate generic calls, see Generic calls and specialized calls.
Use an AccessKey pair
The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using the AccessKey pair to perform operations is a high-risk operation. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. We recommend that you do not save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources that belong to your account may be compromised.
In this example, the AccessKey pair is saved in ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to implement identity authentication.
For more information about how to configure authentication information, see Manage access credentials.
The method that is used to configure environment variables varies based on the operating system. For more information, see Configure environment variables in Linux, macOS, and Windows.
import (
"encoding/json"
"strings"
"fmt"
"os"
fnf20190315 "github.com/alibabacloud-go/fnf-20190315/v2/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"
)
func CreateClient () (_result *fnf20190315.Client, _err error) {
config := &openapi.Config{
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured in the code runtime environment.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured in the code runtime environment.
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
config.Endpoint = tea.String("cn-hangzhou.fnf.aliyuncs.com")
_result = &fnf20190315.Client{}
_result, _err = fnf20190315.NewClient(config)
return _result, _err
}
func _main (args []*string) (_err error) {
client, _err := CreateClient()
if _err != nil {
return _err
}
}
Step 3: Use the initialized client to call CloudFlow API operations
After you initialize the client, you can use it to call the CloudFlow API.
API operation: CreateFlow
Call the CreateFlow operation to create a workflow. When you call the CreateFlow operation, create a request and configure parameters and runtime settings based on your business requirements. You can also customize runtime settings to meet specific requirements.
// Create a request.
createFlowRequest := &fnf20190315.CreateFlowRequest{
// Specify a name for the workflow that you want to create.
Name: tea.String("your_flow_name"),
// Define the workflow in conformance with the Flow Definition Language (FDL) syntax standard. To ensure forward compatibility, the system supports the old and new versions of workflow definition specifications.
Definition: tea.String(`The old version:
\"
type: flow
version: v1
name: my_flow_name
steps:
- type: pass
name: mypass
\"
The new version:
\"
Type: StateMachine
SpecVersion: v1
Name: my_flow_name
StartAt: my_state
States:
- Type: Pass
Name: my_state
End: true
\"`),
// Specify a description for the workflow.
Description: tea.String("your test flow"),
// Specify the type of the workflow.
Type: tea.String("FDL"),
}
// Configure runtime settings.
runtime := &util.RuntimeOptions{}The following sample code provides a complete example on how to create a workflow by using the AccessKey pair to call the CreateFlow operation:
// This file is auto-generated, don't edit it. Thanks.
package main
import (
"encoding/json"
"strings"
"fmt"
"os"
fnf20190315 "github.com/alibabacloud-go/fnf-20190315/v2/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"
)
func CreateClient () (_result *fnf20190315.Client, _err error) {
config := &openapi.Config{
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured in the code runtime environment.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured in the code runtime environment.
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
}
config.Endpoint = tea.String("cn-hangzhou.fnf.aliyuncs.com")
_result = &fnf20190315.Client{}
_result, _err = fnf20190315.NewClient(config)
return _result, _err
}
func _main (args []*string) (_err error) {
client, _err := CreateClient()
if _err != nil {
return _err
}
// Create a request.
createFlowRequest := &fnf20190315.CreateFlowRequest{
// Specify a name for the workflow that you want to create.
Name: tea.String("your_flow_name"),
// Define the workflow against the Flow Definition Language (FDL) syntax standard. To ensure forward compatibility, the system supports the old and new versions of workflow definition specifications.
Definition: tea.String(`The old version:
\"
type: flow
version: v1
name: my_flow_name
steps:
- type: pass
name: mypass
\"
The new version:
\"
Type: StateMachine
SpecVersion: v1
Name: my_flow_name
StartAt: my_state
States:
- Type: Pass
Name: my_state
End: true
\"`),
// Specify a description for the workflow.
Description: tea.String("your test flow"),
// Configure the type of the workflow.
Type: tea.String("FDL"),
}
// Configure runtime settings.
runtime := &util.RuntimeOptions{}
tryErr := func()(_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// If you copy and run the sample code, write your own code to display the response of the API operation.
_, _err = client.CreateFlowWithOptions(createFlowRequest, runtime)
if _err != nil {
return _err
}
return nil
}()
if tryErr != nil {
var error = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
error = _t
} else {
error.Message = tea.String(tryErr.Error())
}
// Handle exceptions with caution based on your actual business scenario and do not ignore exceptions in your project. The error messages displayed in this example are for reference only.
// Display error messages.
fmt.Println(tea.StringValue(error.Message))
// Provide the URL for troubleshooting.
var data interface{}
d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
d.Decode(&data)
if m, ok := data.(map[string]interface{}); ok {
recommend, _ := m["Recommend"]
fmt.Println(recommend)
}
_, _err = util.AssertAsString(error.Message)
if _err != nil {
return _err
}
}
return _err
}
func main() {
err := _main(tea.StringSlice(os.Args[1:]))
if err != nil {
panic(err)
}
}API operation: DescribeFlow
Call the DescribeFlow operation to query information about a workflow. When you call the DescribeFlow operation, create a request and configure parameters and runtime settings based on your business requirements. You can also customize runtime settings to meet specific requirements.
// Create a request and specify the request parameters.
describeFlowRequest := &fnf20190315.DescribeFlowRequest{
// Specify the name of the workflow that you want to query.
Name: tea.String("your_flow_name"),
}
// Configure runtime settings.
runtime := &util.RuntimeOptions{}The following sample code provides a complete example on how to query information about a workflow by using the AccessKey pair to call the DescribeFlow operation:
package main
import (
"encoding/json"
"strings"
"fmt"
"os"
fnf20190315 "github.com/alibabacloud-go/fnf-20190315/v2/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"
)
func CreateClient () (_result *fnf20190315.Client, _err error) {
config := &openapi.Config{
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured in the code runtime environment.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured in the code runtime environment.
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
}
config.Endpoint = tea.String("cn-hangzhou.fnf.aliyuncs.com")
_result = &fnf20190315.Client{}
_result, _err = fnf20190315.NewClient(config)
return _result, _err
}
func _main (args []*string) (_err error) {
client, _err := CreateClient()
if _err != nil {
return _err
}
// Create a request and specify the request parameters.
describeFlowRequest := &fnf20190315.DescribeFlowRequest{
// Specify the name of the workflow that you want to query.
Name: tea.String("your_flow_name"),
}
// Configure runtime settings.
runtime := &util.RuntimeOptions{}
tryErr := func()(_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// If you copy and run the sample code, write your own code to display the response of the API operation.
_, _err = client.DescribeFlowWithOptions(describeFlowRequest, runtime)
if _err != nil {
return _err
}
return nil
}()
if tryErr != nil {
var error = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
error = _t
} else {
error.Message = tea.String(tryErr.Error())
}
// Handle exceptions with caution based on your actual business scenario and do not ignore exceptions in your project. The error messages displayed in this example are for reference only.
// Display error messages.
fmt.Println(tea.StringValue(error.Message))
// Provide the URL for troubleshooting.
var data interface{}
d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
d.Decode(&data)
if m, ok := data.(map[string]interface{}); ok {
recommend, _ := m["Recommend"]
fmt.Println(recommend)
}
_, _err = util.AssertAsString(error.Message)
if _err != nil {
return _err
}
}
return _err
}
func main() {
err := _main(tea.StringSlice(os.Args[1:]))
if err != nil {
panic(err)
}
}API operation: StartExecution
Call the operation to start the execution of a workflow. The StartExecution operation is an asynchronous operation. When you call the StartExecution operation, create a request and configure parameters and runtime settings based on your business requirements. You can also customize runtime settings to meet specific requirements.
// Create a request and specify the request parameters.
startExecutionRequest := &fnf20190315.StartExecutionRequest{
// Specify the name of the workflow that you want to execute.
FlowName: tea.String("your_flow_name"),
// Specify a name for the execution.
ExecutionName: tea.String("your_exec_name"),
// Specify the input of the execution.
Input: tea.String("{\"key\":\"value\"}"),
// Call back TaskToken-related tasks after the workflow execution ends. Example value: 12.
CallbackFnFTaskToken: tea.String("12"),
}
// Configure runtime settings.
runtime := &util.RuntimeOptions{}The following sample code provides a complete example on how to start the execution of a workflow by using the AccessKey pair to call the StartExecution operation:
package main
import (
"encoding/json"
"strings"
"fmt"
"os"
fnf20190315 "github.com/alibabacloud-go/fnf-20190315/v2/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"
)
func CreateClient () (_result *fnf20190315.Client, _err error) {
config := &openapi.Config{
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured in the code runtime environment.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured in the code runtime environment.
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
}
config.Endpoint = tea.String("cn-hangzhou.fnf.aliyuncs.com")
_result = &fnf20190315.Client{}
_result, _err = fnf20190315.NewClient(config)
return _result, _err
}
func _main (args []*string) (_err error) {
client, _err := CreateClient()
if _err != nil {
return _err
}
// Create a request and specify the request parameters.
startExecutionRequest := &fnf20190315.StartExecutionRequest{
// Specify the name of the workflow that you want to execute.
FlowName: tea.String("your_flow_name"),
// Specify a name for the execution.
ExecutionName: tea.String("your_exec_name"),
// Specify the input of the execution.
Input: tea.String("{\"key\":\"value\"}"),
// Call back TaskToken-related tasks after the workflow execution ends. Example value: 12.
CallbackFnFTaskToken: tea.String("12"),
}
// Configure runtime settings.
runtime := &util.RuntimeOptions{}
tryErr := func()(_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// If you copy and run the sample code, write your own code to display the response of the API operation.
_, _err = client.StartExecutionWithOptions(startExecutionRequest, runtime)
if _err != nil {
return _err
}
return nil
}()
if tryErr != nil {
var error = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
error = _t
} else {
error.Message = tea.String(tryErr.Error())
}
// Handle exceptions with caution based on your actual business scenario and do not ignore exceptions in your project. The error messages displayed in this example are for reference only.
// Display error messages.
fmt.Println(tea.StringValue(error.Message))
// Provide the URL for troubleshooting.
var data interface{}
d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
d.Decode(&data)
if m, ok := data.(map[string]interface{}); ok {
recommend, _ := m["Recommend"]
fmt.Println(recommend)
}
_, _err = util.AssertAsString(error.Message)
if _err != nil {
return _err
}
}
return _err
}
func main() {
err := _main(tea.StringSlice(os.Args[1:]))
if err != nil {
panic(err)
}
}API operation: StartSyncExecution
Call the StartSyncExecution operation to synchronously start the execution of a workflow. When you call the operation, create a request and configure parameters and runtime settings based on your business requirements. You can also customize runtime settings to meet specific requirements.
This operation supports only Express workflows.
// Create a request and specify the request parameters.
startSyncExecutionRequest := &fnf20190315.StartSyncExecutionRequest{
// Specify the name of the workflow that you want to execute.
FlowName: tea.String("your_flow_name"),
// Specify a name for the execution.
ExecutionName: tea.String("your_exec_name"),
// Specify the input of the execution.
Input: tea.String("{\"key\":\"value\"}"),
}
// Configure runtime settings.
runtime := &util.RuntimeOptions{}The following sample code provides a complete example on how to synchronously start the execution of a workflow by using the AccessKey pair:
package main
import (
"encoding/json"
"strings"
"fmt"
"os"
fnf20190315 "github.com/alibabacloud-go/fnf-20190315/v2/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"
)
func CreateClient () (_result *fnf20190315.Client, _err error) {
config := &openapi.Config{
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured in the code runtime environment.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured in the code runtime environment.
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
}
config.Endpoint = tea.String("cn-hangzhou.fnf.aliyuncs.com")
_result = &fnf20190315.Client{}
_result, _err = fnf20190315.NewClient(config)
return _result, _err
}
func _main (args []*string) (_err error) {
client, _err := CreateClient()
if _err != nil {
return _err
}
// Create a request and specify the request parameters.
startSyncExecutionRequest := &fnf20190315.StartSyncExecutionRequest{
// Specify the name of the workflow that you want to execute.
FlowName: tea.String("your_flow_name"),
// Specify a name for the execution.
ExecutionName: tea.String("your_exec_name"),
// Specify the input of the execution.
Input: tea.String("{\"key\":\"value\"}"),
}
// Configure runtime settings.
runtime := &util.RuntimeOptions{}
tryErr := func()(_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// If you copy and run the sample code, write your own code to display the response of the API operation.
_, _err = client.StartSyncExecutionWithOptions(startSyncExecutionRequest, runtime)
if _err != nil {
return _err
}
return nil
}()
if tryErr != nil {
var error = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
error = _t
} else {
error.Message = tea.String(tryErr.Error())
}
// Handle exceptions with caution based on your actual business scenario and do not ignore exceptions in your project. The error messages displayed in this example are for reference only.
// Display error messages.
fmt.Println(tea.StringValue(error.Message))
// Provide the URL for troubleshooting.
var data interface{}
d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
d.Decode(&data)
if m, ok := data.(map[string]interface{}); ok {
recommend, _ := m["Recommend"]
fmt.Println(recommend)
}
_, _err = util.AssertAsString(error.Message)
if _err != nil {
return _err
}
}
return _err
}
func main() {
err := _main(tea.StringSlice(os.Args[1:]))
if err != nil {
panic(err)
}
}SDK call example
You can use the API-level SDK demos in different programming languages for debugging. For sample code, see OpenAPI Explorer.