Get message bus (v1)

Get the details of an existing 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"
)

// getMessageBus retrieves an Eventarc message bus.
func getMessageBus(ctx context.Context, w io.Writer, projectID string, locationID string, messageBusID string) error {
	name := fmt.Sprintf("projects/%s/locations/%s/messageBuses/%s", projectID, locationID, messageBusID)

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

	req := &eventarcpb.GetMessageBusRequest{
		Name: name,
	}

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

	fmt.Fprintf(w, "Retrieved message bus: %s\n", messageBus.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;

public class GetMessageBus {

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

  public static void getMessageBus(String projectId, String locationId, String messageBusId)
      throws Exception {
    try (EventarcClient client = EventarcClient.create()) {
      String name = MessageBusName.of(projectId, locationId, messageBusId).toString();

      MessageBus messageBus = client.getMessageBus(name);
      System.out.println("Retrieved message bus: " + messageBus.getName());
    }
  }
}

Node.js

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

'use strict';

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

const client = new EventarcClient();

/**
 * Use the getMessageBus method
 *
 * @param {string} projectId - The Google Cloud project ID.
 * @param {string} locationId - The Google Cloud region or location for the message bus.
 * @param {string} messageBusId - The ID of the message bus to retrieve. This is caller-chosen upon creation, conforming to '^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$'.
 */
async function getMessageBus(projectId, locationId, messageBusId) {
  try {
    const name = `projects/${projectId}/locations/${locationId}/messageBuses/${messageBusId}`;

    const request = {
      name,
    };

    const [response] = await client.getMessageBus(request);

    console.log(`Retrieved message bus: ${response.name}`);
    return response;
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Message bus not found: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied while retrieving message bus: ${err.message}`);
    } else {
      console.error(`Failed to retrieve message bus: ${err.message}`);
    }
  }
}

module.exports = { getMessageBus };

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

client = eventarc_v1.EventarcClient()


def get_message_bus(project_id: str, location_id: str, message_bus_id: str) -> None:
    """Retrieves an Eventarc message bus.

    Args:
        project_id: The Google Cloud project ID.
        location_id: The Google Cloud region or location of the MessageBus.
        message_bus_id: The identifier of the MessageBus to retrieve.
    """
    name = f"projects/{project_id}/locations/{location_id}/messageBuses/{message_bus_id}"

    request = eventarc_v1.GetMessageBusRequest(name=name)

    try:
        response = client.get_message_bus(request=request)
        print(f"Retrieved message bus: {response.name}")
    except google.api_core.exceptions.GoogleAPICallError as error:
        print(f"Error retrieving message bus: {error.message}")

What's next

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