List executions (v1)

Lists all executions for a specified workflow.

Code sample

Go

Before trying this sample, follow the Go setup instructions in the Workflows quickstart using client libraries. For more information, see the Workflows Go API reference documentation.

To authenticate to Workflows, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


import (
	"context"
	"fmt"
	"io"

	executions "cloud.google.com/go/workflows/executions/apiv1"
	"cloud.google.com/go/workflows/executions/apiv1/executionspb"
	"google.golang.org/api/iterator"
)

// listExecutions lists executions for a given workflow.
func listExecutions(ctx context.Context, w io.Writer, projectID string, locationID string, workflowID string) error {
	parent := fmt.Sprintf("projects/%s/locations/%s/workflows/%s", projectID, locationID, workflowID)

	client, err := executions.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient failed: %w", err)
	}
	defer client.Close()

	req := &executionspb.ListExecutionsRequest{
		Parent: parent,
	}

	it := client.ListExecutions(ctx, req)
	for {
		resp, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("ListExecutions iteration failed: %w", err)
		}
		fmt.Fprintf(w, "Execution: %s (State: %s)\n", resp.GetName(), resp.GetState())
	}

	return nil
}

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.