Update message bus (v1)

Update the display name of an existing Eventarc message bus.

Code sample

Go

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


import (
	"context"
	"fmt"
	"io"

	eventarc "cloud.google.com/go/eventarc/apiv1"
	eventarcpb "cloud.google.com/go/eventarc/apiv1/eventarcpb"
	"google.golang.org/protobuf/types/known/fieldmaskpb"
)

// updateMessageBus updates the display name of a message bus in Eventarc.
func updateMessageBus(ctx context.Context, w io.Writer, projectID string, locationID string, messageBusID string) error {
	client, err := eventarc.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("eventarc.NewClient: %w", err)
	}
	defer client.Close()

	busPath := fmt.Sprintf("projects/%s/locations/%s/messageBuses/%s", projectID, locationID, messageBusID)
	messageBus := &eventarcpb.MessageBus{
		Name:        busPath,
		DisplayName: "Updated Message Bus",
	}
	updateMask := &fieldmaskpb.FieldMask{
		Paths: []string{"display_name"},
	}

	req := &eventarcpb.UpdateMessageBusRequest{
		MessageBus: messageBus,
		UpdateMask: updateMask,
	}

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

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

	fmt.Fprintf(w, "Updated message bus: %s\n", resp.GetName())
	return nil
}

Java

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

import com.google.cloud.eventarc.v1.EventarcClient;
import com.google.cloud.eventarc.v1.MessageBus;
import com.google.cloud.eventarc.v1.MessageBusName;
import com.google.protobuf.FieldMask;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class UpdateMessageBus {

  public static void main(String[] args) throws Exception {
    String projectId = "example-project_id";
    String locationId = "example-location_id";
    String messageBusId = "example-message_bus_id";
    updateMessageBus(projectId, locationId, messageBusId);
  }

  public static void updateMessageBus(String projectId, String locationId, String messageBusId)
      throws Exception {
    try (EventarcClient client = EventarcClient.create()) {
      MessageBus messageBus =
          MessageBus.newBuilder()
              .setName(MessageBusName.of(projectId, locationId, messageBusId).toString())
              .setDisplayName("Updated Message Bus")
              .build();
      FieldMask updateMask = FieldMask.newBuilder().addPaths("display_name").build();

      // Initiate the asynchronous update and wait for the operation to complete
      MessageBus response =
          client.updateMessageBusAsync(messageBus, updateMask).get(3, TimeUnit.MINUTES);
      System.out.println("Updated Message Bus: " + response.getName());
    }
  }
}

Python

To authenticate to Eventarc, 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 eventarc_v1
from google.protobuf import field_mask_pb2

client = eventarc_v1.EventarcClient()


def update_message_bus(project_id: str, location_id: str, message_bus_id: str) -> None:
    """Use the update_message_bus method to update a MessageBus resource.

    Args:
        project_id: The Google Cloud project ID.
        location_id: The Google Cloud region or location where the MessageBus is hosted (for example, 'us-central1').
        message_bus_id: The ID of the MessageBus to update. Must start with a lowercase letter and contain only lowercase letters, digits, or hyphens.
    """
    message_bus = eventarc_v1.MessageBus(
        name=f"projects/{project_id}/locations/{location_id}/messageBuses/{message_bus_id}",
        display_name="Updated Example Message Bus",
    )
    update_mask = field_mask_pb2.FieldMask(paths=["display_name"])

    try:
        operation = client.update_message_bus(
            message_bus=message_bus,
            update_mask=update_mask,
        )
        response = operation.result()
        print(f"Updated message bus: {response.name}")
    except google.api_core.exceptions.GoogleAPICallError as error:
        print(f"Failed to update message bus: {error}")

What's next

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