This topic describes how to install CloudFlow SDK for Go and provides examples on how to use the SDK to call CloudFlow API operations. In the following examples, CloudFlow SDK for Go
is used to call CloudFlow API operations to create a workflow, query the information of a workflow, and asynchronously start the execution of a workflow. This topic also provides the complete integration steps for the sample API operations.
Prerequisites
An AccessKey pair is configured. Make sure that an AccessKey pair is created. For more information, see Create an AccessKey pair. To prevent credential leaks, you can write the credentials into environment variables. For more information, see Best practices for using an access credential to call API operations.
Environment requirements
Go 1.10.x or later is used.
Step 1: Import CloudFlow SDK for Go to your project
Alibaba Cloud SDK V2.0 allows you to initiate generic and specialized calls. For more information, see Generic calls and specialized calls. The SDK that you need to import varies based on the call type.
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 the CloudFlow SDK page in OpenAPI Explorer.
In the All languages section, select the language that you want to use.
In the Installation Method section, select an installation method and then copy the installation code to your project.
Add the dependency to your project.
Run the following command to install CloudFlow SDK for Go:
go get github.com/alibabacloud-go/fnf-20190315/v2
Generic calls
You do not need to install the SDK of a service to initiate generic calls. You need to only import the github.com/alibabacloud-go/darabonba-openapi/v2/client
core package. For information about the latest version of the core package, see darabonba-openapi. Run the following command to install the core package:
go get github.com/alibabacloud-go/darabonba-openapi/v2/client
Step 2: Initialize a request client
Specify the endpoint of CloudFlow based on the region in which you use CloudFlow. For information about the endpoint of CloudFlow in each region, seeRegions.
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.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
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 a client, you can use the client to call CloudFlow API operations.
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 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{}
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.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
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 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 the information of 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 the information of 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.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
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
The StartExecution operation is an asynchronous operation that starts the execution of a workflow. 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.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
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)
}
}
SDK call example
You can use the API-level SDK demos in different programming languages for debugging. To view the sample code, visit OpenAPI Explorer.