List message bus enrollments (v1)

List all enrollments for a specified message bus.

Code sample

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.MessageBusName;

public class ListMessageBusEnrollments {

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

  public static void listMessageBusEnrollments(
      String projectId, String locationId, String messageBusId) throws Exception {
    try (EventarcClient client = EventarcClient.create()) {
      String parent = MessageBusName.of(projectId, locationId, messageBusId).toString();
      for (String enrollment : client.listMessageBusEnrollments(parent).iterateAll()) {
        System.out.println("Enrollment: " + enrollment);
      }
    }
  }
}

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 listMessageBusEnrollments method
 *
 * @param {string} projectId - The Google Cloud project ID.
 * @param {string} locationId - The Google Cloud region where the MessageBus is located.
 * @param {string} messageBusId - The ID of the existing MessageBus whose enrollments are to be listed. Sourced from an existing MessageBus.
 */
async function listMessageBusEnrollments(projectId, locationId, messageBusId) {
  try {
    const parent = `projects/${projectId}/locations/${locationId}/messageBuses/${messageBusId}`;
    const request = {
      parent,
    };

    const [enrollments] = await client.listMessageBusEnrollments(request);

    console.log(`Found ${enrollments.length} enrollment(s):`);
    for (const enrollment of enrollments) {
      console.log(`- ${enrollment}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`MessageBus not found: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied: ${err.message}`);
    } else {
      console.error(`Error listing message bus enrollments: ${err.message}`);
    }
  }
}

module.exports = { listMessageBusEnrollments };

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 list_message_bus_enrollments(
    project_id: str, location_id: str, message_bus_id: str
) -> None:
    """List enrollments associated with an Eventarc message bus.

    Args:
        project_id: The Google Cloud project ID containing the message bus.
        location_id: The Google Cloud region where the message bus resides (for
          example, 'us-central1').
        message_bus_id: The caller-chosen identifier of the message bus,
          matching '^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$'.
    """
    parent = (
        f"projects/{project_id}/locations/{location_id}/messageBuses/{message_bus_id}"
    )

    request = eventarc_v1.ListMessageBusEnrollmentsRequest(
        parent=parent,
    )

    try:
        response = client.list_message_bus_enrollments(request=request)
        for enrollment in response:
            print(f"Enrollment: {enrollment}")
    except google.api_core.exceptions.GoogleAPICallError as error:
        print(f"Failed to list message bus enrollments: {error}")

What's next

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