Get workflow (v1)

Retrieve an existing workflow by its resource name.

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"

	workflows "cloud.google.com/go/workflows/apiv1beta"
	workflowspb "cloud.google.com/go/workflows/apiv1beta/workflowspb"
)

// getWorkflow retrieves an existing workflow by its resource name.
func getWorkflow(ctx context.Context, w io.Writer, projectID string, locationID string, workflowID string) error {
	name := fmt.Sprintf("projects/%s/locations/%s/workflows/%s", projectID, locationID, workflowID)

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

	req := &workflowspb.GetWorkflowRequest{
		Name: name,
	}

	workflow, err := client.GetWorkflow(ctx, req)
	if err != nil {
		return fmt.Errorf("GetWorkflow: %w", err)
	}

	fmt.Fprintf(w, "Retrieved workflow: %s\n", workflow.GetName())
	return nil
}

Node.js

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

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

'use strict';

const {WorkflowsClient} = require('@google-cloud/workflows');
const {status} = require('@grpc/grpc-js');

const client = new WorkflowsClient();

/**
 * Use the getWorkflow method
 *
 * @param {string} projectId - The Google Cloud project ID (for example, 'example-project-id')
 * @param {string} locationId - The Google Cloud region where the service is located (for example, 'us-central1')
 * @param {string} workflowId - ID of the Workflow (for example, 'example-workflow-id')
 */
async function getWorkflow(projectId, locationId, workflowId) {
  try {
    const name = client.workflowPath(projectId, locationId, workflowId);
    const [workflow] = await client.getWorkflow({name});

    console.log(`Workflow name: ${workflow.name}`);
    console.log(`State: ${workflow.state}`);
    console.log(`Revision ID: ${workflow.revisionId}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Workflow not found.`);
    } else {
      console.error(`Error executing getWorkflow: ${err.message}`);
    }
  }
}

module.exports = { getWorkflow };

Python

Before trying this sample, follow the Python setup instructions in the Workflows quickstart using client libraries. For more information, see the Workflows Python 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 google.api_core.exceptions
from google.cloud import workflows_v1

client = workflows_v1.WorkflowsClient()


def get_workflow(project_id: str, location_id: str, workflow_id: str) -> None:
    """Use the get_workflow method

    Args:
        project_id: The Google Cloud project ID (for example, 'example-project-id')
        location_id: The Google Cloud region where the service is located (for example, 'us-central1')
        workflow_id: ID of the Workflow (for example, 'example-workflow-id')
    """
    name = client.workflow_path(project_id, location_id, workflow_id)

    request = workflows_v1.GetWorkflowRequest(
        name=name,
    )

    try:
        response = client.get_workflow(request=request)
        print(f"Workflow name: {response.name}")
        print(f"Workflow state: {response.state.name}")
    except google.api_core.exceptions.GoogleAPICallError as e:
        print(f"API error occurred: {e}")

What's next

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