Update workflow (v1)

Update the description of an existing 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"

	workflows "cloud.google.com/go/workflows/apiv1"
	workflowspb "cloud.google.com/go/workflows/apiv1/workflowspb"
	"google.golang.org/protobuf/types/known/fieldmaskpb"
)

// updateWorkflow updates the description of an existing workflow.
func updateWorkflow(ctx context.Context, w io.Writer, projectID string, locationID string, workflowID string) error {
	workflow := &workflowspb.Workflow{
		Name:        fmt.Sprintf("projects/%s/locations/%s/workflows/%s", projectID, locationID, workflowID),
		Description: "Updated workflow description",
	}
	updateMask := &fieldmaskpb.FieldMask{
		Paths: []string{"description"},
	}

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

	req := &workflowspb.UpdateWorkflowRequest{
		Workflow:   workflow,
		UpdateMask: updateMask,
	}

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

	response, err := op.Wait(ctx)
	if err != nil {
		return fmt.Errorf("Wait failed: %w", err)
	}

	fmt.Fprintf(w, "Updated workflow: %s\n", response.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 updateWorkflow method
 *
 * @param {string} projectId - The Google Cloud project ID.
 * @param {string} locationId - The Google Cloud region (for example, 'us-central1').
 * @param {string} workflowId - The ID of the existing workflow to update.
 */
async function updateWorkflow(projectId, locationId, workflowId) {
  try {
    const workflow = {
      name: client.workflowPath(projectId, locationId, workflowId),
      description: 'Updated workflow description',
    };
    const updateMask = {
      paths: ['description'],
    };

    // Initiates the workflow update operation
    const [operation] = await client.updateWorkflow({
      workflow,
      updateMask,
    });

    // Waits for the long-running operation to complete
    const [response] = await operation.promise();

    console.log(`Updated workflow: ${response.name}`);
    console.log(`Description: ${response.description}`);
    console.log(`State: ${response.state}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Workflow ${workflowId} not found.`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied updating workflow: ${err.message}`);
    } else if (err.code === status.INVALID_ARGUMENT) {
      console.error(`Invalid argument provided: ${err.message}`);
    } else {
      console.error(`API error updating workflow: ${err.message}`);
    }
    throw err;
  }
}

module.exports = { updateWorkflow };

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
from google.protobuf import field_mask_pb2

client = workflows_v1.WorkflowsClient()


def update_workflow(project_id: str, location_id: str, workflow_id: str) -> None:
    """Updates an existing workflow's description.

    Args:
        project_id: The Google Cloud project ID where the workflow is hosted.
        location_id: The GCP region hosting the workflow (for example, 'us-central1').
        workflow_id: The identifier of the existing workflow to update.
    """
    workflow_name = (
        f"projects/{project_id}/locations/{location_id}/workflows/{workflow_id}"
    )
    workflow = workflows_v1.Workflow(
        name=workflow_name,
        description="Updated workflow description",
    )
    update_mask = field_mask_pb2.FieldMask(paths=["description"])

    try:
        operation = client.update_workflow(
            workflow=workflow,
            update_mask=update_mask,
        )
        response = operation.result()
        print(f"Updated workflow: {response.name}")
        print(f"Workflow state: {response.state.name}")
    except google.api_core.exceptions.GoogleAPICallError as e:
        print(f"Workflow update failed: {e}")

What's next

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